Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler construction 2002

Similar presentations


Presentation on theme: "Compiler construction 2002"— Presentation transcript:

0 Compiler construction in4020 – lecture 6
week 6 Compiler construction in4020 – lecture 6 Koen Langendoen Delft University of Technology The Netherlands

1 Compiler construction 2002
week 6 Summary of lecture 5 semantic analysis identification – symbol tables type checking assignment yacc LLgen program text lexical analysis syntax analysis context handling annotated AST tokens AST parser generator language grammar

2 Compiler construction 2002
week 6 Quiz 6.3 The following declarations are given for a language that uses name equivalence. A, B: array [1..10] of int; C : array [1..10] of int; D : array [1..10] of int; Which of these four variables have the same type? A: A+B, since each anonymous type is assigned a unique name

3 Compiler construction 2002
week 6 Overview context handling annotating the AST attribute grammars manual methods program text lexical analysis syntax analysis context handling annotated AST tokens AST expr : expr '+' expr { $$ = $1 + $3;} | expr '*' expr { $$ = $1 * $3;} | '(' expr ')' { $$ = $2;} | DIGIT ; Note: we have already seen a very simple attribute grammar. The attribute associated with each yacc non-terminal is its “value”.

4 Compiler construction 2002
week 6 Attribute grammars formal attributes are associated with each grammar symbol type location context inherited attributes synthesized attributes ‘4’ ‘*’ type: real loc: reg2 type: int loc: const ‘a’ loc: sp+8

5 Compiler construction 2002
week 6 Attribute grammars attribute evaluation rules are associated with each production Constant_definition(INH old symbol table, SYN new symbol table) 'CONST' Defined_identifier '=' Expression ';' ATTRIBUTE RULES: SET Expression . symbol table TO Constant_definition. old symbol table; SET Constant_definition. new symbol table TO Updated symbol table( Constant_definition. old symbol table, Defined_identifier. name, Expression. type, Expression. value); Note that the symbol table is passed as a parameter iso a global variable.

6 Compiler construction 2002
week 6 Attribute grammars dependency graph inherited synthesized old symbol table new symbol table Constant_definition name Defined_identifier symbol table type value Expression

7 Concise evaluation rules
Compiler construction 2002 week 6 Concise evaluation rules Constant_definition(INH old symbol table, SYN new symbol table)  'CONST' Defined_identifier '=' Expression ';' ATTRIBUTE RULES: SET Expression . symbol table TO Constant_definition. old symbol table; SET Constant_definition. new symbol table TO Updated symbol table( Constant_definition. old symbol table, Defined_identifier. name, Expression. type, Expression. value); Note abbreviations and implicit declarations. Constant_definition(INH old symtab, SYN new symtab)  'CONST' Defined_identifier(name) '=' Expression(old symtab, type, value) ';' ATTRIBUTE RULES: SET new symtab TO Updated symbol table(old symtab, name, type, value);

8 Compiler construction 2002
week 6 Running example integral numbers in decimal or octal notation 11D 234O 56O 789D Number  Digit_Seq Base_Tag Digit_Seq  Digit_Seq Digit | Digit Digit  DIGIT // token, ‘0’-’9’ Base_Tag  ‘O’ | ‘D’

9 Attribute grammar for integral numbers
Compiler construction 2002 week 6 Attribute grammar for integral numbers Number(SYN value)  Digit_Seq(base, value) Base_Tag(base) ATTRIBUTE RULES SET Digit_Seq.base TO Base_Tag.base; Digit_Seq(INH base, SYN value)  Digit_Seq(base, value) Digit(base, value) SET value TO Digit_Seq.value * base + Digit.value; Digit_Seq(INH base, SYN value)  Digit(base, value) Digit(INH base, SYN value)  DIGIT // token, ‘0’-’9’ SET value TO Checked digit value(base, DIGIT.repr[0] – ‘0’); Base_Tag(SYN base)  ‘O’ SET base TO 8; Q: what does ‘checked digit value’ do? A: see if the digit is in the range [0:base-1] Base_Tag(SYN base)  ‘D’ ATTRIBUTE RULES SET base TO 10;

10 Dependency graphs for integral numbers
Compiler construction 2002 week 6 Dependency graphs for integral numbers Number(SYN value)  Digit_Seq(base, value) Base_Tag(base) ATTRIBUTE RULES SET Digit_Seq.base TO Base_Tag.base; value Number Digit_Seq base Base_Tag Q+A style when drawing the dependency graph on the blackboard.

11 Compiler construction 2002
week 6 Exercise (5 min.) draw the other dependency graphs for the integral-number attribute grammar

12 Compiler construction 2002
week 6 Answers

13 Compiler construction 2002
week 6 Answers value Digit_Seq Digit base value Digit DIGIT repr base base Base_Tag ‘O’ base Base_Tag ‘D’

14 Compiler construction 2002
week 6 Attribute evaluation allocate space for attributes in the nodes of the AST fill the attributes of the terminals in the AST (leaf nodes) execute the evaluation rules to assign values to attributes (interior nodes) a rule may fire when all input attributes are defined loop until no new values can be assigned

15 Compiler construction 2002
week 6 Attribute evaluation value base repr input: “13O” Number Digit_Seq Base_Tag ‘O’ Digit_Seq Digit DIGIT Digit DIGIT

16 Compiler construction 2002
week 6 Attribute evaluation input: “13O” Number value value base base Digit_Seq value value Base_Tag base base ‘O’ base base value base Digit_Seq value value base Digit value repr DIGIT repr base Digit value value dataflow order DIGIT repr

17 Attribute evaluation by tree walking
Compiler construction 2002 week 6 Attribute evaluation by tree walking at each node: try to perform all the assignments in the evaluation rules for that node visit all children again try to perform the attribute assignments repeat walking until top-level attributes are assigned Q: why do we AGAIN evaluate the rules? A: efficiency, we harvest synthesized attributes on exit. Without it the alg still works, but a simple attribute flowing up N levels would require (at least) N visits.

18 Attribute evaluation by tree walking
Compiler construction 2002 week 6 Attribute evaluation by tree walking walk number(node) evaluate number(node) walk digit_seq(node.digit_seq) walk base_tag(node.base_tag) Number Digit_Seq Base_Tag value base evaluate number(node) IF node.value is not set AND node.digit_seq.value is set THEN SET node.value TO node.digit_seq.value IF node.digit_seq.base is not set AND node.base_tag.base is set THEN SET node.digit_seq.base TO node.base_tag.base Q: does it matter in which order we evaluate the rules? A: yes, occasionally.

19 Compiler construction 2002
week 6 Exercise (4 min.) how many tree walks are necessary to evaluate the attributes of the AST representing the octal number ‘13O’ ? how many for ‘1234D’ ? WHILE Number.value is not set: walk number(Number); Write parse tree on the blackboard

20 Compiler construction 2002
week 6 Answers

21 Compiler construction 2002
week 6 Answers any integral number can be evaluated with two walks

22 Compiler construction 2002
week 6 Break

23 Compiler construction 2002
week 6 Cycle handling dynamic cycle detection #walks > #attributes static cycle detection transitive closure of IS-SI graphs, see book N Just hint at the static solution. i1 i2 i3 s1 s2 N

24 Multi-visit attribute grammars
Compiler construction 2002 week 6 Multi-visit attribute grammars avoid interpretation overhead generate code for each visit that “knows” what attributes to assign and which children to visit static attribute partition: (INi,SNi)i = 1..n i1 i2 i3 s1 s2 N Q: how many visits for N? A: two Q: what is the other possible partition? A: ({i1},{s1}), ({i2, i3},{s2}) ({i1 ,i3},{s1}), ({i2},{s2})

25 Ordered attribute grammars
Compiler construction 2002 week 6 Ordered attribute grammars late evaluation partitioning heuristic work backwards find (INlast,SNlast) of the last visit SNlast has no outgoing edges in IS-SI graph INlast is the set of attributes SNlast depends on remove INlast and SNlast from the IS-SI graph repeat until the IS-SI graph is empty i1 i2 i3 i2 i3 N s1 s2 s2 ({i2, i3},{s2})

26 Ordered attribute grammars
Compiler construction 2002 week 6 Ordered attribute grammars late evaluation partitioning heuristic work backwards find (INlast,SNlast) of the last visit SNlast has no outgoing edges in IS-SI graph INlast is the set of attributes SNlast depends on remove INlast and SNlast from the IS-SI graph repeat until the IS-SI graph is empty i1 i1 N s1 s1 ({i1},{s1}) ({i2, i3},{s2})

27 Compiler construction 2002
week 6 Exercise (7 min.) derive the late evaluation partitioning of the number attribute grammar using the IS-SI graphs below write down the multi-visit routine(s) for number nodes value Digit_Seq base Digit Number Base_Tag

28 Compiler construction 2002
week 6 Answers IN1 SN1 IN2 SN2 Number Digit_Seq Digit Base_Tag

29 Compiler construction 2002
week 6 Answers IN1 SN1 IN2 SN2 Number value Digit_Seq base Digit Base_Tag visit_1 number(node) visit_1 base_tag(node.base_tag) SET node.digit_seq.base TO node.base_tag.base visit_1 digit_seq(node.digit_seq) SET node.value TO node.digit_seq.value Note that we the multi-visit code only needs one traversal, whereas the generic tree-walking code used two.

30 L-attributed grammars
Compiler construction 2002 week 6 L-attributed grammars combine attribute grammars and top-down parsing attributes may only depend on information from the parent node or siblings to the left A Q: is the integral number grammar an L-attributed grammar? A: NO, parse tree must be constructed, followed by a tree walk => 2-pass grammar B1 B2 B3 B4 C1 C2 C3

31 Compiler construction 2002
week 6 LLgen example main : [line]+ ; line {int e;} : expr(&e) '\n' { printf("%d\n", e);} expr(int *e) {int t;} : term(e) [ '+' term(&t) { *e += t;} ]* term(int *t) {int f;} : factor(t) [ '*' factor(&f) { *t *= f;} factor(int *f) : '(' expr(f) ')' | DIGIT { *f = yylval;} evaluation rules attributes main : [line]+ ; line {int e;} : expr(&e) '\n' { printf("%d\n", e);} expr(int *e) {int t;} : term(e) [ '+' term(&t) { *e += t;} ]* term(int *t) {int f;} : factor(t) [ '*' factor(&f) { *t *= f;} factor(int *f) : '(' expr(f) ')' | DIGIT { *f = yylval;}

32 Bottom-up parsing and attribute grammars
Compiler construction 2002 week 6 Bottom-up parsing and attribute grammars stack of attributes problem with inherited attributes solution: -rules A  B {C.inh_attr := f(B.syn_attr);} C code can only be executed at the end A  B Action1 C Action1   {C.inh_attr := f(B.syn_attr);} Q: what is the problem? A: code must be evaluated in the middle, while bottom-up parsers do so when complete handle has been detected. Q: are epsilon-rules the perfect trick? A: NO, because the transformed grammar may no longer be LALR(1)

33 S-attributed grammars
Compiler construction 2002 week 6 S-attributed grammars combine attribute grammars and bottom-up parsing only synthesized attributes may be used A L-attributed B1 B2 B3 B4 C1 C2 C3

34 S-attributed grammars
Compiler construction 2002 week 6 S-attributed grammars combine attribute grammars and bottom-up parsing only synthesized attributes may be used A B1 B2 B3 B4 C1 C2 C3

35 Compiler construction 2002
week 6 Summary attribute grammars formal attributes per symbol: inherited & synthesized attribute evaluation rule per production dependency graphs cycle detection evaluation order multi-visit grammars S -attributed grammars L -attributed grammars

36 Compiler construction 2002
week 6 Homework study sections: static cycle checking assignment 1: replace yacc with LLgen deadline April 9 08:59 print handout for next week [blackboard]


Download ppt "Compiler construction 2002"

Similar presentations


Ads by Google