Abstract Syntax Tree (AST)

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Compilation (Semester A, 2013/14) Lecture 6b: Context Analysis (aka Semantic Analysis) Noam Rinetzky 1 Slides credit: Mooly Sagiv and Eran Yahav.
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.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
Syntax Directed Translation
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],
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Bottom Up Translation (revisited) Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road,
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
CH4.1 CSE244 Bottom Up Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155.
Syntax-Directed Translation
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Context-sensitive Analysis or Semantic Elaboration Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
1 Semantic Analysis. 2 Semantic Analyzer Attribute Grammars Syntax Tree Construction Top-Down Translators Bottom-Up Translators Recursive Evaluators Type.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
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.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Syntax-Directed Translation We associate information with the programming language constructs by attaching attributes to grammar symbols. 2.Values.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Syntax Directed Definition and Syntax directed Translation
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
SDTs used to implement SDDs A non-cyclic SDD (having definitions of attributes) can always be implemented by a SDT (having actions that assign values to.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
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.
Semantics Analysis.
Context-Sensitive Analysis
Chapter 5 Syntax Directed Translation
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
CS 3304 Comparative Languages
Chapter 5. Syntax-Directed Translation
Lecture 11: Context Sensitive Analysis
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Context-sensitive Analysis
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Abstract Syntax Tree (AST) The parse tree contains too much detail e.g. unnecessary terminals such as parentheses depends heavily on the structure of the grammar e.g. intermediate non-terminals Idea: strip the unnecessary parts of the tree, simplify it. keep track only of important information AST Conveys the syntactic structure of the program while providing abstraction. Can be easily annotated with semantic information (attributes) such as type, numerical value, etc. Can be used as the IR.

Abstract Syntax Tree + * if-statement if-statement can become IF cond THEN statement cond statement E add_expr can become E + E mul_expr id id E * E id num id num

Where are we? Ultimate goal: generate machine code. Before we generate code, we must collect information about the program: Front end scanning (recognizing words) CHECK parsing (recognizing syntax) CHECK semantic analysis (recognizing meaning) There are issues deeper than structure. Consider: int func (int x, int y); int main () { int list[5], i, j; char *str; j = 10 + 'b'; str = 8; m = func("aa", j, list[12]); return 0; }

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 type of arguments does it take? Is x declared before being used? Where can x be stored? Is the expression x+y type-consistent? Semantic analysis is the phase where we collect information about the types of expressions and check for type related errors. The more information we can collect at compile time, the less overhead we have at run time.

Semantic analysis Collecting type information may involve "computations" What is the type of x+y given the types of x and y? Tool: attribute grammars CFG Each grammar symbol has associated attributes The grammar is augmented by rules (semantic actions) that specify how the values of attributes are computed from other attributes. The process of using semantic actions to evaluate attributes is called syntax-directed translation. Examples: Grammar of declarations. Grammar of signed binary numbers.

Attribute grammars Example 1: Grammar of declarations Production Semantic rule D  T L L.in = T.type T  int T.type = integer T  char T.type = character L  L1, id L1.in = L.in addtype (id.index, L.in) L  id addtype (id.index, L.in)

Attribute grammars Example 2: Grammar of signed binary numbers Production Semantic rule N  S L if (S.neg) print('-'); else print('+'); print(L.val); S  + S.neg = 0 S  – S.neg = 1 L  L1, B L.val = 2*L1.val+B.val L  B L.val = B.val B  0 B.val = 0*20 B  1 B.val = 1*20

Attributes Attributed parse tree = parse tree annotated with attribute rules Each rule implicitly defines a set of dependences Each attribute's value depends on the values of other attributes. These dependences form an attribute-dependence graph. Note: Some dependences flow upward The attributes of a node depend on those of its children We call those synthesized attributes. Some dependences flow downward The attributes of a node depend on those of its parent or siblings. We call those inherited attributes. How do we handle non-local information? Use copy rules to "transfer" information to other parts of the tree.

Attribute grammars attribute-dependence graph Production Semantic rule E  E1+E2 E.val = E1.val+E2.val E  num E.val = num.yylval E  (E1) E.val = E1.val attribute-dependence graph E 12 E + E num 2 E 10 ) ( + E E num 7 num 3

Attribute grammars Production Semantic rule We can use an attribute grammar to construct an AST The attribute for each non-terminal is a node of the tree. Example: Notes: yylval is assumed to be a node (leaf) created during scanning. The production E  (E1) does not create a new node as it is not needed. Production Semantic rule E  E1+E2 E.node = new PlusNode(E1.node,E2.node) E  num E.node = num.yylval E  (E1) E.node = E1.node

Evaluating attributes Evaluation methods Method 1: Dynamic, dependence-based At compile time Build dependence graph Topsort the dependence graph Evaluate attributes in topological order This can only work when attribute dependencies are not circular. It is possible to test for that. Circular dependencies show up in data flow analysis (optimization) or may appear due to features such as goto

Evaluating attributes Evaluation methods Method 2: Oblivious Ignore rules and parse tree Determine an order at design time Method 3: Static, rule-based At compiler construction time Analyze rules Determine ordering based on grammatical structure (parse tree)

Attribute grammars We are interested in two kinds of attribute grammars: S-attributed grammars All attributes are synthesized L-attributed grammars Attributes may be synthesized or inherited, AND Inherited attributes of a non-terminal only depend on the parent or the siblings to the left of that non-terminal. This way it is easy to evaluate the attributes by doing a depth-first traversal of the parse tree. Idea (useful for rule-based evaluation) Embed the semantic actions within the productions to impose an evaluation order.

Embedding rules in productions Synthesized attributes depend on the children of a non-terminal, so they should be evaluated after the children have been parsed. Inherited attributes that depend on the left siblings of a non-terminal should be evaluated right after the siblings have been parsed. Inherited attributes that depend on the parent of a non-terminal are typically passed along through copy rules (more later). L.in is inherited and evaluated after parsing T but before L T.type is synthesized and evaluated after parsing int D  T {L.in = T.type} L T  int {T.type = integer} T  char {T.type = character} L  {L1.in = L.in} L1, id {L.action = addtype (id.index, L.in)} L  id {L.action = addtype (id.index, L.in)}

Rule evaluation in top-down parsing Recall that a predictive parser is implemented as follows: There is a routine to recognize each lhs. This contains calls to routines that recognize the non-terminals or match the terminals on the rhs of a production. We can pass the attributes as parameters (for inherited) or return values (for synthesized). Example: D  T {L.in = T.type} L T  int {T.type = integer} The routine for T will return the value T.type The routine for L, will have a parameter L.in The routine for D will call T(), get its value and pass it into L()

Rule evaluation in bottom-up parsing S-attributed grammars All attributes are synthesized Rules can be evaluated bottom-up Keep the values in the stack Whenever a reduction is made, pop corresponding attributes, compute new ones, push them onto the stack Example: Implement a desk calculator using an LR parser Grammar: Production Semantic rule L  E \n print(E.val) E  E1 + T E.val = E1.val+T.val E  T E.val = T.val T  T1* F T.val = T1.val*F.val T  F T.val = F.val F  (E) F.val = E.val F  digit F.val = yylval

Rule evaluation in bottom-up parsing Production Semantic rule Stack operation L  E \n print(E.val) E  E1 + T E.val = E1.val+T.val val[newtop]=val[top-2]+val[top] E  T E.val = T.val T  T1* F T.val = T1.val+F.val val[newtop]=val[top-2]*val[top] T  F T.val = F.val F  (E) F.val = E.val val[ntop]=val[top-1] F  digit F.val = yylval

Rule evaluation in bottom-up parsing How can we inherit attributes on the stack? (L-attributed only) Use copy rules Consider AXY where X has a synthesized attribute s. Parse X. X.s will be on the stack before we go on to parse Y. Y can "inherit" X.s using copy rule Y.i = X.s where i is an inherited attribute of Y. Actually, we can just use X.s wherever we need Y.i, since X.s is already on the stack. Example: back to the type declaration grammar: Production Semantic rule Stack operation D  T L L.in = T.type T  int T.type = integer val[ntop]=integer T  char T.type = character val[ntop]=character L  L1, id L1.in = L.in addtype (id.index, L.in) addtype(val[top], val[top-3]) L  id addtype (id.index, L.in) addtype(val[top], val[top-1])

Rule evaluation in bottom-up parsing Problem w/ inherited attributes: What if we cannot predict the position of an attribute on the stack? For example: case1: S aAC After we parse A, we have A.s at the top of the stack. Then, we parse C. Since C.i=A.s, we could just use the top of the stack when we need C.i case2: S aABC After we parse AB, we have B's attribute at the top of the stack and A.s below that. Then, we parse C. But now, A.s is not at the top of the stack. A.s is not always at the same place! Production Semantic rule S  aAC C.i = A.s S  bABC C.i = A.s C  c C.s = f(C.i)

Rule evaluation in bottom-up parsing Solution: Modify the grammar. We want C.i to be found at the same place every time Insert a new non-terminal and copy C.i again: Now, by the time we parse C, A.s will always be two slots down in the stack. So we can compute C.s by using stack[top-1] Production Semantic rule S  aAC C.i = A.s S  bABMC M.i=A.s, C.i = M.s C  c C.s = f(C.i) M   M.s = M.i

Attribute grammars Attribute grammars have several problems Non-local information needs to be explicitly passed down with copy rules, which makes the process more complex In practice there are large numbers of attributes and often the attributes themselves are large. Storage management becomes an important issue then. The compiler must traverse the attribute tree whenever it needs information (e.g. during a later pass) However, our discussion of rule evaluation gives us an idea for a simplified approach: Have actions organized around the structure of the grammar Constrain attribute flow to one direction. Allow only one attribute per grammar symbol. Practical application: BISON

In practice: bison In Bison, $$ is used for the lhs non-terminal, $1, $2, $3, ... are used for the non-terminals on the rhs, (left-to-right order) Example: Expr : Expr TPLUS Expr { $$ = $1+$3;} Expr: Expr TPLUS Expr {$$ = new ExprNode($1, $3);}