Attribute Grammars Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
Compiler construction in4020 – lecture 7 Koen Langendoen Delft University of Technology The Netherlands.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Annotating Abstract Syntax Tree Professor Yihjia Tsai Tamkang University.
Attribute Grammars Recall the yacc program Parse a given expression
Compiler construction in4020 – lecture 5 Koen Langendoen Delft University of Technology The Netherlands.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Context-sensitive Analysis. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee() { int f[3],g[0],
Compiler construction 2002
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Compiler Construction Sohail Aslam Lecture Beyond Syntax  These questions are part of context-sensitive analysis  Answers depend on values, not.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
CS 363 Comparative Programming Languages Semantics.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
3. Annotating the Abstract Syntax Tree ― the Context From: Chapter 3, Modern Compiler Design, by Dick Grun et al.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax Directed Definition and Syntax directed Translation
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Syntax-Directed Translation Part I
Chapter 2: A Simple One Pass Compiler
Thinking about grammars
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
Thinking about grammars
Presentation transcript:

Attribute Grammars Professor Yihjia Tsai Tamkang University

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 ;

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

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); 

Attribute grammars dependency graph old symbol tablenew symbol table Constant_definition name Defined_identifier symbol tabletypevalue Expression inheritedsynthesized

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); 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);

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’

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) ATTRIBUTE RULES 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’ ATTRIBUTE RULES SET value TO Checked digit value(base, DIGIT.repr[0] – ‘0’); Base_Tag(SYN base)  ‘O’ ATTRIBUTE RULES SET base TO 8; Base_Tag(SYN base)  ‘D’ ATTRIBUTE RULES SET base TO 10;

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 value Digit_Seq base Base_Tag base

Exercise (5 min.) draw the other dependency graphs for the integral-number attribute grammar

Answers

value Digit_Seq value Digit_SeqDigit basevalue base value Digit_Seq Digit value base Base_Tag ‘O’ value Digit DIGIT repr base Base_Tag ‘D’

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

Attribute evaluation input: “13O” Number Digit_Seq Base_Tag Digit_SeqDigit ‘O’ DIGIT value base valuebasevaluebase valuebase repr

Attribute evaluation input: “13O” Number Digit_Seq Base_Tag Digit_SeqDigit ‘O’ DIGIT value base valuebase valuebase repr base value base value base dataflow order

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

Attribute evaluation by tree walking walk number(node) evaluate number(node) walk digit_seq(node. digit_seq) walk base_tag(node. base_tag) 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 Number Digit_Seq Base_Tag value base

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);

Answers

any integral number can be evaluated with two walks

Cycle handling dynamic cycle detection #walks > #attributes static cycle detection transitive closure of IS-SI graphs, see book i1i1 i2i2 i3i3 s1s1 s2s2 N N

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: (IN i,SN i ) i = 1..n i1i1 i2i2 i3i3 s1s1 s2s2 N ({i 1,i 3 },{s 1 }), ({i 2 },{s 2 })

Ordered attribute grammars late evaluation partitioning heuristic work backwards find (IN last,SN last ) of the last visit SN last has no outgoing edges in IS-SI graph IN last is the set of attributes SN last depends on remove IN last and SN last from the IS-SI graph repeat until the IS-SI graph is empty i1i1 i2i2 i3i3 s1s1 s2s2 N s2s2 ({i 2, i 3 },{s 2 }) i3i3 i2i2

Ordered attribute grammars late evaluation partitioning heuristic work backwards find (IN last,SN last ) of the last visit SN last has no outgoing edges in IS-SI graph IN last is the set of attributes SN last depends on remove IN last and SN last from the IS-SI graph repeat until the IS-SI graph is empty i1i1 s1s1 N ({i 1 },{s 1 }) s1s1 i1i1 ({i 2, i 3 },{s 2 })

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 valuebase value Number base Base_Tag

Answers IN 1 SN 1 IN 2 SN 2 Number Digit_Seq Digit Base_Tag

Answers IN 1 SN 1 IN 2 SN 2 Number value Digit_Seq basevalue Digit basevalue Base_Tag base 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

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 B3B3 B2B2 B1B1 B4B4 C3C3 C2C2 C1C1

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;} ; 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

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);}

S-attributed grammars combine attribute grammars and bottom-up parsing only synthesized attributes may be used A B3B3 B2B2 B1B1 B4B4 C3C3 C2C2 C1C1 L-attributed

S-attributed grammars combine attribute grammars and bottom-up parsing only synthesized attributes may be used A B3B3 B2B2 B1B1 B4B4 C3C3 C2C2 C1C1

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