[go: up one dir, main page]

0% found this document useful (0 votes)
4K views24 pages

Semantic Report

Semantic analysis is the last front-end analysis phase of the compiler. It catches any remaining errors by performing type checking and scope checking. Type checking verifies that each operation respects the language's type system by checking the types involved in each operation. Scope checking determines if an identifier is accessible at a given point by analyzing static scoping rules. Semantic analysis finds errors like undefined variables, type mismatches, and wrong number/type of arguments.

Uploaded by

Mark A. Soliva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views24 pages

Semantic Report

Semantic analysis is the last front-end analysis phase of the compiler. It catches any remaining errors by performing type checking and scope checking. Type checking verifies that each operation respects the language's type system by checking the types involved in each operation. Scope checking determines if an identifier is accessible at a given point by analyzing static scoping rules. Semantic analysis finds errors like undefined variables, type mismatches, and wrong number/type of arguments.

Uploaded by

Mark A. Soliva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

SEMANTIC

ANALYSIS
The Compiler So Far
Lexical analysis
Detects inputs with illegal tokens
e.g.: main$ ();
Syntactic analysis
Detects inputs with ill-formed parse
trees
e.g.: missing semicolons
Semantic analysis
Last front end analysis phase
Catches all remaining errors 2
Semantic Analysis
Source code

lexical
Lexical Analysis errors
tokens
syntax
Syntactic Analysis errors
AST
semantic
Semantic Analysis errors
AST

Intermediate Code Gen

3
Goals of a Semantic Analyzer
Compiler must do more than recognize whether a sentence
belongs to the language

Find remaining errors that would make program


invalid
undefined variables, types
type errors that can be caught statically
Figure out useful information for later phases
types of all expressions
data layout
Terminology
Static checks done by the compiler
4
Dynamic checks done at run time
Kinds of Checks
Uniqueness checks
Certain names must be unique
Many languages require variable declarations
Flow-of-control checks
Match control-flow operators with structures
Example: break applies to innermost loop/switch
Type checks
Check compatibility of operators and operands
Logical checks
Program is syntactically and semantically correct,
but does not do the correct thing

5
Typical Semantic Errors

Multiple declarations: a variable should be


declared (in the same scope) at most once
Undeclared variable: a variable should not
be used before being declared
Type mismatch: type of the LHS of an
assignment should match the type of the RHS
Wrong arguments: methods should be called
with the right number and types of arguments

6
Examples of Reported Errors
Undeclared identifier
Multiply declared identifier
Index out of bounds
Wrong number or types of args to call
Incompatible types for operation
Break statement outside switch/loop
Goto with no label

7
A Sample Semantic Analyzer
Works in two phases traverses the AST created by the parser

1. For each scope in the program


process the declarations
add new entries to the symbol table and
report any variables that are multiply declared

process the statements


find uses of undeclared variables, and
update the "ID" nodes of the AST to point to the appropriate
symbol-table entry.

2. Process all of the statements in the program again


use the symbol-table information to determine the type of each
expression, and to find type errors.
8
Symbol Tables
Purpose:
keep track of names declared in the program
Symbol table entry:
associates a name with a set of attributes, e.g.:
kind of name (variable, class, field, method, )
type (int, float, )
nesting level
mem location (where will it be found at runtime)
Functions:
Type Lookup(String id)
Void Add(String id, Type binding)
Bindings: name type pairs {a string, b int}
How Symbol Tables Work (1)
int x;
char y;
void p(void)
{ double x;

{ int y[10];

}

}
void q(void)
{ int y;

}
main()
{ char x;

10 }
How Symbol Tables Work (2)
int x;
char y;
void p(void)
{ double x;

{ int y[10];

}

}
void q(void)
{ int y;

}
main()
{ char x;

}
11
How Symbol Tables Work (3)
int x;
char y;
void p(void)
{ double x;

{ int y[10];

}

}
void q(void)
{ int y;

}
main()
{ char x;

12
}
How Symbol Tables Work (4)
int x;
char y;
void p(void)
{ double x;

{ int y[10];

}

}

void q(void)
{ int y;

}
main()
{ char x;

13 }
How Symbol Tables Work (6)
int x;
char y;
void p(void)
{ double x;

{ int y[10];

}

}
void q(void)
{ int y;

}

main()
{ char x;

14 }
Techniques used in
Semantic Analysis

1. Type Checking
2. Scope Checking

15
Type Checking Overview

A type is a set of values and a set of operations


operating on those values.

3 CATEGORIES:
Base types - int, float, double, char, bool, etc.
Compound types - arrays, pointers, records,
structs, unions, classes, and so on.
Complex types - lists, stacks, queues, trees,
heaps, tables, etc.
16
1. Type Checking
Type checking is the process of verifying that
each operation executed in a program
respects the type system of the language.

Type Error

Static type checking


Dynamic type checking
Static type checking
Is done at compile-time.
The information the type checker needs is obtained via
declarations and stored in a master symbol table.
After this information is collected, the types involved in
each operation are checked.

18
Dynamic type checking
Is done at runtime.

Is implemented by including type


information for each data location.

19
Implicit type conversion or
Coercion
It is when a compiler finds a type error and then
changes the type of the variable to an appropriate
type.

In a language like C++, the space of possible


automatic conversions can be enormous, which
makes the compiler run more slowly and
sometimes gives surprising results.

20
2. Scope Checking
A scope is a section of program text enclosed by
basic program delimiters, e.g., {} in C.

A simple shell scripting language might require all


variables to be declared at the top- level so they are
visible everywhere. But that throws all identifiers
into one big pot and prevents names from ever being
used again

Many languages allow nested scopes that are scopes


defined within other scopes. The scope defined by
the innermost such unit is called the current scope.
The scopes defined by the current scope and by any
enclosing program units are known as open scopes.
Any other scope is a closed scope. 21
We need to determine if the identifier is
accessible at that point in the program.

2 Scoping Rules

Static scoping - a function is called in the


environment of its definition (i.e., its lexical
placement in the source text)

Dynamic scoping - a function is called in the


environment of its caller (i.e., using the runtime
stack of function calls).
Dynamic Scoping

Not all languages use static scoping


Lisp, APL, and Snobol use dynamic scoping

Dynamic scoping:
A use of a variable that has no
corresponding declaration in the
same function corresponds to the
declaration in the most-recently-
called still active function
23
Semantic Analysis Summary
Compiler must do more than recognize whether
a sentence belongs to the language.

Checks of all kinds


undefined variables, types
type errors that can be caught
statically
Store useful information for later phases
types of all expressions
24

You might also like