Csc3205-Semantic Analysis and Intermediate RepresentationFile
Csc3205-Semantic Analysis and Intermediate RepresentationFile
Marriette Katarahweire
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
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
Expression well-formed
7 + 3.14 Okay in C, not okay in some more strict languages
6 + ”abc” Not okay in C, okay in some other languages
Same name must appear two or more times
Example: defining a block:
Begin B
End B
Function calls
Types of arguments match definitions
Number of arguments match definitions
Return type match definitions
Flow-of-control checks
“break” only within loop
“return” only within function
“default” or “error” statement for “switch” in C
Why?
it provides increased abstraction, cleaner separation between
the front and back ends, and adds possibilities for retargeting/
cross-compilation.
To break the difficult problem of translation into two simpler,
more manageable pieces.
supports advanced compiler optimizations and most
optimization is done on this form of the code
while (x < 4 * y) {
x = y / 3 >> x;
if (y) print x - 3;
}
goto L2
L1:
load y
load_constant 3
load x
shr
div
store x
load y
jump_if_zero L3
load x
load_constant 3
sub
print
L3:
L2:
load x
load_constant 4
load y
mul
less_than
jump_if_not_zero L1