Compiler Construction
Recap
Ran Shaham and Ohad ShachamSchool of Computer Science
Tel-Aviv University
22
Test
25/02/2009 at 9:00 חומר פתוח Look at Ran’s webpage for previous tests
Possible questions Extend IC Parsing …
33
Scanning
CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI …
Issues in lexical analysis:
Language changes:
New keywords,
New operators,
New meta-language features, e.g., annotations
// An example programclass Hello { boolean state; static void main(string[] args) { Hello h = new Hello(); boolean s = h.rise(); Library.printb(s); h.setState(false); } boolean rise() { boolean oldState = state; state = true; return oldState; } void setState(boolean newState) { state = newState; }}
44
Parsing and ASTCLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI …
prog
class_list
class
field_method_list
field field_method_list
type ID(state)
BOOLEAN
method
field_method_list
…
…
parser uses stream of tokensand generate derivation tree
Issues in syntax analysis:
Grammars: LL(1), LR(0)
Building LR(0) parsers
Transition diagram
Parse table
Running automaton
Conflict resolution
Factoring
Ambiguity
TA1
55
Parsing and AST
prog
class_list
class
field_method_list
field field_method_list
type ID(state)
BOOLEAN
method
field_method_list
…
…
Syntax tree builtduring parsing
parser uses stream of tokenand generate derivation
tree
CLASS,CLASS_ID(Hello),LB,BOOLEAN,ID(state),SEMI …
ProgAST
ClassAST
classList
FieldAST[0]type:BoolTypename:state
MethodAST[0]
MethodAST[1]
MethodAST[2]
…
…
methodListfieldList
Should know difference between derivation tree and AST
Know how to build AST from input
66
FieldsOrMethods ::=
Field:field FieldsOrMethods:next{: RESULT = next;
RESULT. addField(field); :} |
Method:method FieldsOrMethods:next {: RESULT = next;
RESULT.addMethod(method); :} |
/* empty */{: RESULT = new FieldsMethods(); :};
77
Questions
Is the following grammar in LR(0) ?Build a parser for the grammarRun an input string using your parser
88
Question
Is the following grammar is LR(0)?
S -> B $
B -> id P | id ( E ]
P -> epsilon | ( E )
E -> B | B,E
A grammar with epsilon production is not LR(0)
99
Semantic analysis
ProgAST
ClassAST
classList
FieldAST[0]type:BoolType
MethodAST[0]
MethodAST[1]
MethodAST[2]
…
…
methodListfieldList
Representing scopes Type-checking Semantic checks
SymbolKindType
HelloclassHello
SymbolKindTypeProperties
statefieldbooleaninstance
mainmethodstring[]->voidstatic
risemethodvoid->booleaninstance
setStatemethodboolean->voidinstance
SymbolKindType
newStateparamint
(Program)
(Hello)
(setState)
…
1010
Semantic conditions
What is checked in compile-time and what is checked in runtime?
EventC/R
Program execution halts
Break/continue inside a while statement
Array index within bound
In Java the cast statement(Integer)f is legal
In Java method o.m(…) is illegal since m is private
1111
Semantic conditions
What is checked in compile-time and what is checked in runtime?
EventC/R
Program execution haltsR (undecidable in general)
Break/continue inside a while statement
C
Array index within boundR (undecidable in general)
In Java the cast statement(A)f is legal
Depends: if A is sub-type of f then checked during runtime (raising exception), otherwise flagged as an error during compilation
In Java method o.m(…) is illegal since m is private
C
1212
Question – extend IC
Support Java override annotation inside comments // @Override Annotation is written above method to indicate it overrides a
method in superclass Describe the phases in the compiler affected by the
change and the changes themselves
class A { void rise() {…}}class B extends A { // @Override void rise() {…}}
class A { void rise() {…}}class B extends A { // @Override void ris() {…}}
Legal program Illegal program
1313
Answer
The change affects the lexical analysis, syntax analysis and semantic analysis
Does not affect later phasesUser semantic condition
1414
Changes to scanner
Add pattern for @Override inside comment state patterns
Add Java code to action to comments – instead of not returning any token, we now return a token for the annotation
What if we want to support multiple annotations in comments?
boolean override=false;%%<INITIAL> // { override=false; yybegin(comment); }<comment> @Override { override=true; }<comment> \n { if (override) return new Token(…,override,…) }
1515
Changes to parser and AST
method static type name params ‘{‘ mbody ‘}’
| type name params ‘{‘ mbody ‘}’
| OVERRIDE type name params ‘{‘ mbody ‘}’
Add a Boolean flag to the method AST node to indicate that the method is annotated
1616
Changes to semantic analysis
Suppose we have an override annotation above a method m in class A
We check the following semantic conditions1. class A extends a superclass (otherwise it does not make
sense to override a method)2. Traverse the superclasses of A by going up the class
hierarchy until we find the first method m and check that it has the same signature as A.mIf we fail to find such a method we report an error
1717
3מועד ב, שאלה 2006
class ByRefExample { static void main(string [] args) { int x = 5; increment(x); Library.println(x); }
static void increment(int & y) { y = y + 1; }}
1818
3מועד ב, שאלה 2006
Treat by reference as address Lexer
Add token for &
Parser Add reference type
Semantic analysis Compare the actual parameter type and the reference type Treat the scope checking
1919
3מועד ב, שאלה 2006
IR Add support for by reference
Code generation Caller
Add the pointer of the actual parameter to the stuck instead of the parameter
Callee Treat the parameter as a pointer
2020
Question
Add constructors to IC
Solution: treat the constructor as a function and call when object allocatedMark the constructorCheck that every object has a constructorCall the constructor on allocation
IRCode generation
2121
Question
Add break(exp) exp is from type int break should exit exp loop Cannot perform break(n) can be perform only inside n nested
loops!!!
int x=2;while(true) {
while(true) {break(x);
}}
Can we generally check this in compile time?
2222
Translation to IR Accept annotated AST and translate functions into lists of
instructions Compute offsets for fields and virtual functions
Issues: dispatch tables, weighted register allocation Question: give the method tables for Rectangle and Square
class Shape { boolean isShape() {return true;} boolean isRectangle() {return false;} boolean isSquare() {return false;} double surfaceArea() {…}}class Rectangle extends Shape { double surfaceArea() {…} boolean isRectangle() {return true;}}class Square extends Rectangle { boolean isSquare() {return true;}}
2323
Answer
Shape_isShape
Rectangle_isRectangle
Shape_isSqaure
Rectangle_surfaceArea
Shape_isShape
Rectangle_isRectangle
Sqaure_isSqaure
Rectangle_surfaceArea
Method table for rectangle Method table for square
2424
Possible question
Suppose we wish to provide type information during runtime like instanceof in Java
x instanceof A returns true iff x is exactly of type A (in Java it can also be subtype of A)
Describe the changes in runtime organization needed to support this operator and the translation to IR
2525
Answer Use the pointers to the dispatch table as the type
indicators
Translate x instanceof A asMove x,R0MoveField R0.0,R0Compare R0,_DV_A
If we want to support the Java operator represent the type hierarchy during runtime and generate code to
search up the hierarchy Keep info for each type for its ancestors and check in constant
time
2626
Question – Activation record
Run the program on the input and show the stack at after …
Add a “new” function that allocates on the stack How to do it? Disadvantages
Stack size is limited Live until the method returns
2727
“To Callee-save or toCaller-save?”
Callee-saved registers need only be saved when callee modifies their value
Some conventions exist (cdecl)%eax, %ecx, %edx – caller save%ebx, %esi, %edi – callee save%esp – stack pointer%ebp – frame pointerUse %eax for return value
2828
Register allocation
Sethi Ullman can only handle expressions without side effect Can change the execution order without hurt the program
semantics Possible question
Extend Sethi Ullman to ternary trees
Global register allocation Is it optimal?
IR registers are treated as local variables
When we have an actual spill we use the stuck