Syntax Directed Definition and Syntax directed Translation

Slides:



Advertisements
Similar presentations
SYNTAX DIRECTED TRANSLATION 11CS Types of Attributes There are two types of attributes for non- terminals :- Synthesized Attributes : For a non-terminal.
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.
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)
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.
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.
Syntax-Directed Translation
Chapter 2 A Simple Compiler
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.
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.
Syntax Directed Definitions Synthesized Attributes
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
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 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
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.
UNIT – 5 SYNTAX-DIRECTED TRANSLATION
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.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
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.
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
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.
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.
Semantic analysis Jakub Yaghob
Semantics Analysis.
Syntax-Directed Translation
A Simple Syntax-Directed Translator
Compiler Construction
Chapter 5 Syntax Directed Translation
Syntax-Directed Translation Part I
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
SYNTAX DIRECTED TRANSLATION
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Presentation transcript:

Syntax Directed Definition and Syntax directed Translation

Compilation in a Nutshell 1 Source code (character stream) if (b == 0) a = b; Lexical analysis Token stream if ( b == ) a = b ; Parsing if == = ; Abstract syntax tree (AST) b a b if Semantic Analysis boolean int == = ; Decorated AST int b int 0 int a lvalue int b

Compilation in a Nutshell 2 if boolean int == = ; int b int 0 int a lvalue int b Intermediate Code Generation CJUMP == MEM CONST MOVE NOP Optimization + MEM MEM fp 8 + + fp 4 fp 8 CJUMP == Code generation CX CONST MOVE NOP CMP CX, 0 CMOVZ DX,CX DX CX

Outline Syntax-Directed Definitions Evaluation Orders for SDD’s Applications of Syntax-Directed Definition Syntax-Directed Translation

Definitions Syntax-directed definition (Attribute Grammar) Productions with semantic rules Ex: EE1+T E.code=E1.code |T.code|’+’ More readable Useful for specification Syntax-directed translation (Translation Scheme) Productions with semantic actions Ex: EE1+T { print ’+’ } More efficient Useful for implementation

Syntax-Directed Definitions SDD: a context-free grammar with attributes and rules Attributes: for grammar symbols Rules: for productions Attributes for nonterminals Synthesized attribute: attributes that are passed up a parse tree, i.e., the LHS attribute is computed from the RHS attributes in the production rules EE1+T E.val=E1.val+T.val Inherited attribute: attributes that are passed down a parse tree, i.e., the RHS attributes are derived from its LHS attributes or other RHS attributes in the production rules TF T’ T’.inh=F.val T.val=T’.syn Terminals can have synthesized attributes, but not inherited attributes

Example Production Semantic Rules 1) LE n 2) EE1+T 3) ET 4) TT1*F 5) TF 6) F(E) 7) Fdigit L.val=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

Annotated Parse Tree for 3*5+4 n Prod. Semantic Rules L→En 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 :=digit.lexval Annotated Parse Tree for 3*5+4 n L E.val=19 n E.val=15 + T.val=4 T.val=15 F.val=4 T.val=3 F.val=5 digit.lexval=4 * F.val=3 digit.lexval=5 digit.lexval=3

Class Exercise S (L)|a L L,S|S Write an SDD to print the number of parenthesis pair for an input string. Write an SDD to print the maximum parenthesis depth.

Outline Syntax-Directed Definitions Evaluation Orders for SDD’s Applications of Syntax-Directed Definition Syntax-Directed Translation

Example attribute grammar A grammar to evaluate signed binary numbers POS, VAL, and NEG are attributes of the non-terminal (node) they are attached to

Example LIST0 BIT LIST1 Note: pos val LIST0 LIST1 BIT Note: semantic rules define a partial dependency graph structure can be used to derive characteristics of generated total dependency graphs

Attribute grammars The attribute dependency graph Evaluation order nodes represent attributes edges represent the flow of values graph is specific to parse tree size is related to parse tree's size can be built alongside parse tree The dependency graph must be acyclic Evaluation order Topological sort of the dependency graph to order attributes Topological order: a linear ordering of the nodes of a directed acyclic graph such that each node comes before all nodes to which it has outbound edges using this order, evaluate the rules This order depends on both the grammar and the input string

Example attribute grammar Example Parse tree for -101 NUM val SIGN pos val LIST neg pos val pos val LIST BIT pos val LIST BIT pos val pos val BIT - 1 1

Example grammar dependency graph NUM val SIGN pos val LIST0 neg pos val pos val LIST1 BIT2 val and neg are synthesized attributes pos is an inherited attribute LIST0.pos is an inherited attribute with an empty dependency set. pos val pos val LIST2 BIT1 pos val BIT0 - 1 1

Attribute grammars - Evaluate in this order Yields NUM.val: -5 A topological order for the example 1. SIGN.neg 2. LIST0.pos 3. LIST1.pos 4. LIST2.pos 5. BIT0.pos 6. BIT1.pos 7. BIT2.pos 8. BIT0.val 9. LIST2.val 10. BIT1.val 11. LIST1.val 12. BIT2.val 13. LIST0.val 14. NUM.val NUM val SIGN pos val LIST0 neg pos val pos val LIST1 BIT2 pos val pos val LIST2 BIT1 pos val BIT0 - 1 1 Evaluate in this order Yields NUM.val: -5

Example grammar final result NUM val :-5 pos: 0 val: 5 SIGN LIST0 neg : T LIST1 pos: 1 val: 4 pos: 0 val: 1 BIT2 pos: 2 val: 4 LIST2 BIT1 pos: 1 val: 0 BIT0 pos: 2 val: 4 The evaluation process is also called decorating the parse tree - 1 1

S-Attributed SDD An SDD is S-attributed if every attribute is synthesized We can evaluate its attributes in any bottom-up order of the nodes of the parse tree A postorder traversal Postorder(N) { for (each child C of N, from the left) postorder(C); evaluate the attributes associated with node N; }

L-Attributed SDD L: dependency-graph edges can go from left to right, but not from right to left Each attribute must be either: Synthesized, or Inherited, but with the rules limited as follows. Suppose production AX1X2…Xn and inherited attribute Xi.a computed by a rule which may only use: Inherited attributes associated with head A Either inherited or synthesized attributes associated with X1X2…Xi-1 Either inherited or synthesized attributes associated with Xi, in a way that there’s no cycle

L-Attributed example Production Semantic Rules 1) TF T’ 2) T’*F T1’ 3) T’ 4) Fdigit T’.inh=F.val T.val=T’.syn T1’.inh=T.inh*F.val T’.syn=T1’.syn T’.syn=T’.inh F.val=digit.lexval

Example: Annotated parse tree for 3*5 T.val=15 F.Val=3 T’.inh=3 T’.syn=15 digit.lexval=3 T1’.inh=15 T1’.syn=15 * F.val=5 digit.lexval=5 

L-Attributed example Any SDD containing the following production and rules cannot be L-attributed. A  B C A.s = B.b; B.i = f(C.c, A.s)

Outline Syntax-Directed Definitions Evaluation Orders for SDD’s Applications of Syntax-Directed Definitions Syntax-Directed Translation

Applications of Syntax-Directed Definitions Type checking Intermediate-code generation Construction of abstract syntax trees

Abstract Syntax Tree An abstract syntax tree is the procedure’s parse tree with the nodes for most non-terminal symbols removed E.g., “a + 3 * b”

Creating the AST (1 + 2 + (3 + 4)) + 5 S E + S ( S ) E + + 5 E + S 5 1

Construction of AST -- Example: S-Attributed Grammar Production Semantic Rules 1) EE1+T 2) EE1-T 3) ET 4) T(E) 5) Tid 6) Tnum E.node=new Node(‘+’, E1.node, T.node) E.node=new Node(‘-’, E1.node, T.node) E.node=T.node T.node=E.node T.node=new Leaf(id, id.entry) T.node=new Leaf(num, num.val) Ex: a-4+c

a-4+c AST construction E nptr E nptr T nptr T nptr E - id T nptr id + To entry for c id num 4 id To entry for a

Class Exercise Build the parse tree and abstract parse tree for ((a)+(b))

Outline Syntax-Directed Definitions Evaluation Orders for SDD’s Applications of Syntax-Directed Definition Syntax-Directed Translation

Syntax-Directed Translation SDT: a context-free grammar with semantic actions embedded with production bodies Complementary to SDD Actions performed by a preorder traversal of the parse tree Can be used to implement two classes of SDD’s LR-parsable grammar: S-attributed SDD LL-parsable grammar: L-attributed SDD

Postfix Translation Postfix SDT’s: SDT’s with all actions at the right ends of the production bodies Ex: 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=digit.lexval; }

Parser-Stack Implementation of Postfix SDT’s Postfix SDT’s can be implemented during LR parsing by executing the actions when reductions occur To place the attributes along with the grammar symbols on the stack (Fig. 5.19)

5.4.2 Parser-Stack Implementation of Postfix SDT’s Example 5.15: Rewrite the actions of the desk-calculator so that they manipulate the parser stack explicitly.

SDT’s with Actions inside Productions Actions may be placed at any position in the body of productions They are performed immediately after all symbols to its left are processed BX {a} Y a is done after we have recognized X In bottom-up parse, perform a as soon as X appears on top of the stack In top-down parse, perform a just before we expand Y

SDT’s for L-Attributed SDD Rules for turning L-attributed SDD into SDT Embed the actions that computes the inherited attributes for nonterminal A immediately before A in the body of production Place the actions that computes a synthesized attribute for the head of a production at the end of the body

Example Ex: grammar for boxes in typesetting language Eqn BB1B2|B1 sub B2|(B1)|text

SDD for Typesetting Boxes Production Semantic Rules 1) SB 2) BB1B2 3) BB1 sub B2 4) B(B1) 5) B text B.ps=10 B1.ps=B.ps,B2.ps=B.ps B.ht=max(B1.ht,B2.ht) B.dp=max(B1.dp,B2.dp) B1.ps=B.ps,B2.ps=0.7*B.ps B.ht=max(B1.ht,B2.ht-0.25*B.ps) B.dp=max(B1.dp,B2.dp+0.25*B.ps) B1.ps=B.ps, B.ht=B1.ht, B.dp=B1.dp B.ht=getHt(B.ps,text.lexval) B.dp=getDp(B.ps,text.lecval)

SDT for Typesetting Boxes