Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(CS-460)
1
Outline
1. Semantic Analysis
2. Attributes & Attribute Grammars
3. The Symbol Table
1. The Structure of the Symbol Table
2. Declarations
3. Scope Rules and Block Structure
4. Interaction of Same Level Declarations
4. Data Types & Type Checking
5. Summary
2
Semantic Analysis
Lecture: 19-20
3
The Compiler So Far
Semantic analysis
Last “front end” analysis phase
Catches all remaining errors
4
Semantic Analysis
Source code
lexical errors
Lexical Analysis
tokens
syntax
Syntactic Analysis errors
AST
semantic
Semantic Analysis errors
AST’
5
Beyond Syntax
What’s wrong with this
code?
foo(int a, char * s){ … }
int bar() {
(Note: it parses perfectly)
int f[3];
int i, j, k;
char *p;
float k;
foo(f[6], 10, j);
break;
i->val = 5;
j = i + k;
printf(“%s,%s.\n”,p,q);
goto label23;
}
Semantic Analysis 6
Semantic Analysis
7
Semantic Analysis (Continue…)
9
Attributes (Continue…)
10
Attribute Grammars
14
Example – Attribute Grammars
15
Example – Attribute Grammars
(Continue…)
16
Example – Attribute Grammars
(Continue…)
17
Example – Attribute Grammars
(Continue…)
18
Exercise
19
Example 2 – Attribute Grammars
20
The Symbol Table
22
The Structure of the Symbol Table
24
The Structure of the Symbol Table
(Continue…)
27
The Structure of the Symbol Table
(Continue…)
28
Declarations
29
Declarations (Continue…)
30
Declarations (Continue…)
31
Scope Rules and Block Structure
32
Scope Rules and Block Structure
(Continue…)
Block structure
It is a common property of programming languages
A language is block structured if it permits the nesting of
blocks inside other blocks
If the scope of declarations in a block are limited to that
block and other blocks contained in that block, subject to
the most closely nested rule
Given several different declarations for same name, the declaration
that applies to a reference is the one in the most closely nested
block to the reference
33
Scope Rules and Block Structure
(Continue…)
35
Interaction of Same Level
Declarations
One main issue that relates to scope is the
interactions among declarations at the same level
One typical requirement in many languages is that
there can be no reuse of the same name in the
declaration at the same level
To check this requirement, a compiler must perform a
lookup before each insert and determine by some
mechanism whether any preexisting declaration with the
same name are at the same level or not
Somewhat more difficult is the question of how
much information the declaration in a sequence at
the same level have available about each other
36
Interaction of Same Level
Declarations (Continue…)
Consider the following code;
int a = 1;
void f(void)
{ int a = 2, j = a+1;
…
} //which ‘a’ will be used to assign value to ‘j’?
If each declaration is added to the symbol table as it
is processed, it is called sequential declaration
If all the declarations are processed simultaneously
and added at once to symbol table at the end of a
section, then it is called collateral declaration
37
Interaction of Same Level
Declarations (Continue…)
For each recursive declaration of function or
procedure, the compiler must insert the name of
function or procedure as it finds its declaration,
otherwise compiler may consider recursive call as
an error
Error of use before declaration
38
Data Types & Type Checking
Any Questions?
40