Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.

Slides:



Advertisements
Similar presentations
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Advertisements

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 # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
Abstract Syntax Tree (AST)
Syntax Directed Translation
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 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Syntax-Directed Translation
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
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.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Topic: Syntax Directed Translations
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
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.
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
UNIT – 5 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.
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.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
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.
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.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
Compiler Construction
Chapter 5 Syntax Directed Translation
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part II
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Syntax-Directed Translation Part II
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part II
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation
Syntax-Directed Translation Part II
Directed Acyclic Graphs (DAG)
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Chapter 5 Syntax-Directed Translation

Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the attributes are computed by semantic rules associated with the grammar productions. Two notations: -- Syntax–directed definitions: Each production has a set of semantic rules associated with it. --Translation Schemes: Embed the semantic actions within the productions. Conceptually we parse the input token stream, build the parse tree and then traverse the tree as needed to evaluate the semantic rules. The rules may generate code, save information in the symbol table, issue error messages, etc.

In some cases we don’t have to follow this outline literally. We may evaluate the rules during parsing. Synthesized attribute: The value of the attribute at a parent node can be found from the attributes of its children. It can be evaluated by traversing the tree bottom-up. Inherited attribute: The value of the attribute at a node can be found from the attributes at its parent node and/or its sibling nodes. Can be evaluated by traversing the tree top-down. S-attributed definition: A syntax-directed definition where all attributes are synthesized (the “S” stands for synthesized).

Example: Simple desk calculator Production L  En E  E1 + T E  T T  T1 * F T  F F  (E) F  digit Semantic Rules print(E.val) E.val := E1.val + T.val E.val := T.val T.val := T1.val * F.val T.val := F.val F.val := E.val F.val := digit.lexval

Example: Annotated parse tree for 3*5+4n L E.Val = 19 n +E.Val = 15 T.Val = 15 * T.Val = 3 F.Val = 3 digit.lexval = 3 F.Val = 5 digit.lexval = 5 T.Val = 4 F.Val = 4 digit.lexval = 4

Example Using inherited attributes to parse a declaration and add type information to the symbol table. ProductionSemantic Rules D  TLL.In := T.type T  intT.Type := integer T  realT.Type := real L  L1, idL1.in := L.in Addtype (id.entry, L.in) L  idAddtype (id.entry, L.in)

Annotated Parse Tree for real, id1, id2, id3 D T.Type = real real L.in = real L.In = real id1,id2,id3

If all attributes are synthesized then a bottom – up parser can evaluate the attributes while it parses. Add attribute fields to the nonterminals on the stack. Before each reproduction apply the semantic rule associated with the production. E.g. Assume the semantic rule associated with the A  XYZ production is : A.a := f (X.x, Y.y, Z.z).

Before the reduction the parser stack looks like: After the reduction the parser stack looks like: …… XX.X YY.y ZZ.Z …… AA.A

Many top-down and bottom-up translation methods can be obtained by applying a depth-first visit procedure to the root of the parse tree: Procedure dfvisit(n : node); Begin for each child m of n, from left to right do begin evaluate inherited attributes of m; dfvisit(m) end; evaluate synthesized attributes of n End.

This method works as long as the inherited attributes of each child node just depend on attributes of the parent node and the children to its left; there is no dependence on attributes of children to the right. A syntax-directed definition is L-attributed if each inherited attribute of Xj on the right side of a production A  x1, X2,…., Xn depends only on the attributes of X1, X2,…, Xj-1 to the left of Xj in the production and the inherited attributes of A. The L stands for left. There are no restrictions on the synthesized attributes of Xj so every S-attributed definition is L-attributed.

Top-Down Translation E.g. E  E1 + T{E.val := E1.val + T.val} E  E1 – T{E.val := E1.val – T.val} E  T{E.val := T.val} T  (E){T.val := E.val} T  num{T.val := num.val} To eliminate the left-recursion we add a nonterminal R to the grammar with an inherited attribute, R.i and a synthesized attribute, R.s. The transformed translation scheme is:

E  T {R.i := T.val} R {E.val := R.s} R  + T {R1.i := R.i + T.val} R1 {R.s := R1.s} R  - T {R1.i := R.i – T.val} R1 {R.s := R1.s} R  ε {T.val := num.val} In general suppose the translation scheme has the folowing left-recursive structure: A  A1 Y {A.a := g (A1.a, Y.y)} A  X {A.a := f (X.x)} f and g are arbitrary functions and A.a, A1.a, X.x and Y.y are synthesized attributes. Add a new nonterminal R, with a synthesized attribute R.s and an inherited attribute, R.i.

To eliminate the left-recursion the translation scheme is transformed to: A  X {R.i := f (X.x)} R {A.a := R.s} R  Y {R1.i := g (R.i, Y.y)} R1 {R.s := R1.s} R  ε {R.s := R.i} E.g. A left recursive translation scheme for generating syntax trees: E  E1 + T {E.nptr := mknode (‘+’, E1.nptr, T.nptr)}

E  E1 - T {E.nptr := mknode (‘-’, E1.nptr, T.nptr)} E  T {E.nptr := T.nptr} T  (E) {T.npte := E.nptr} T  id {T.nptr := mkleaf (id, id.entry)} T  num {T.nptr := mkleaf (num, num.value)}

Eliminating left recursion gives : E  T {R.i := T.nptr} r {E.nptr := R.s} R  + T {R1.i := mknode (‘+’, R.i, T.nptr)} R1 {R.s := R1.s} R  - T {R1.i := mknode (‘-’, R.i, T.nptr)} R1 {R.s := R1.s} R  ε {R.s := R.i} T  (E) {T.nptr := E.nptr} T  id {T.nptr := mkleaf (id, id.entry)} T  num {T.nptr := mkleaf (num, num.value)}

The technique to write predictive parsers using recursive procedures can be modified to include the actions of a translation scheme. Use recursive functions instead of recursive procedures to pass the values of attributes. For each nonterminal write a recursive function. The function accepts the values of the imherited attributes in its formal parameters and returns the values of the synthesized attributes. If there are multiple synthesized attributes their values can be returned in a record or a pointer to a record.

Example Consider three nonterminals E, R and T. Write three recursive functions. E and T have no arguments. Function E handles the production: E  T {R.i := T.nptr} R {E.nptr := R.s} function E : tree_node; begin e := R (T) end; FunctionSynthesized Attributes Inherited Attributes EE.Nptr(none) RR.SR>I TT.Nptr(none)

Function R handles three productions: R  + T {R1.i := mknode (‘+’, R.i, T.nptr)} R1 {R.s := R1.s} R  - T {R1.i := mknode (‘-’, R.i, T.nptr)} R1 {R.s := R1.s} R  ε {R.s := R.i}

function R (I : tree_node) : tree-node; var nptr, i1 : tree_node; begin if lookahead = ‘+’ then begin match (‘+’); nptr := T; i1 := mknode (‘+’, I nptr); R := R (i1) end else if lookahead = ‘-’ then begin match (‘-’); nptr := T; i1 := mknode (‘-’, I, nptr); R := R (i1) end else R := i end;