Semantic Report
Semantic Report
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
3
Goals of a Semantic Analyzer
Compiler must do more than recognize whether a sentence
belongs to the language
5
Typical Semantic Errors
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
{ 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
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
18
Dynamic type checking
Is done at runtime.
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.
20
2. Scope Checking
A scope is a section of program text enclosed by
basic program delimiters, e.g., {} in C.
2 Scoping Rules
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.