Compiler construction 2002

Slides:



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

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.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Compiler construction 2002
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
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.
Attribute Grammars Professor Yihjia Tsai Tamkang University.
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.
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],
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Compiler Summary Mooly Sagiv html://
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
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.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
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.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
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.
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.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Compiler Design (40-414) Main Text Book:
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.
Bison: Parser Generator
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
Presentation transcript:

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

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

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

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”.

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

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.

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

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

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’

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;

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.

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

Compiler construction 2002 week 6 Answers

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

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

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

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

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.

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.

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

Compiler construction 2002 week 6 Answers

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

Compiler construction 2002 week 6 Break

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

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

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

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

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

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

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.

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

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

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)

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

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

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

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