Syntax-Directed Translation Part II

Similar documents
Syntax-Directed Translation Part I

Syntax Directed Translation

CSE302: Compiler Design

Syntax-Directed Translation

Principles of Programming Languages

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation

Syntax-Directed Translation

Syntax-Directed Translation

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Principles of Programming Languages

Syntax-Directed Translation. Introduction

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Semantic Analysis Attribute Grammars

Syntactic Directed Translation

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

COP5621 Exam 3 - Spring 2005

Abstract Syntax Trees Synthetic and Inherited Attributes

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

Lecture 14 Sections Mon, Mar 2, 2009

Summary: Semantic Analysis

Abstract Syntax Tree

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Syntax Analysis Part I

Intermediate Code Generation

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax Directed Translation

Syntax-Directed Translation

Abstract Syntax Trees & Top-Down Parsing

A programming language requires two major definitions A simple one pass compiler

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Intermediate Code Generation

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CS2210: Compiler Construction. Code Generation

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Chapter 4. Action Routines

Chapter 4 :: Semantic Analysis

Syntax Directed Translation

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Syntax-Directed Translation. Lecture 14

CS 406: Syntax Directed Translation

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Formal Languages and Compilers Lecture X Intermediate Code Generation

Programming Languages

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

CSCI Compiler Design

Static and Dynamic Semantics

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

UNIT IV INTERMEDIATE CODE GENERATION

A Simple Syntax-Directed Translator

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Semantic Analysis with Attribute Grammars Part 3

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Attribute Grammars. Attribute Grammars

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

COP4020 Spring 2011 Midterm Exam

5. Syntax-Directed Definitions & Type Analysis

CSCI Compiler Design

More On Syntax Directed Translation

LR Parsing LALR Parser Generators

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Program Assignment 2 Due date: 10/20 12:30pm

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

CS502: Compilers & Programming Systems

Time : 1 Hour Max Marks : 30

Exercises II. Exercise: Lexical Analysis

Compilers. Compiler Construction Tutorial The Front-end

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Semantic Analysis. Role of Semantic Analysis

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Chapter 4. Lexical and Syntax Analysis

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

Syntax Analysis Part IV

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

CS606- compiler instruction Solved MCQS From Midterm Papers

4. Lexical and Syntax Analysis

CSCE 531, Spring 2015 Final Exam Answer Key

shift-reduce parsing

Compiler Construction Assignment 3 Spring 2018

THEORY OF COMPILATION

4. Lexical and Syntax Analysis

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Transcription:

Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed translation schemes are more implementation oriented than SDD Syntax-directed translation schemes indicate the order in which semantic rules and attributes are to be evaluated Yacc/Bison use syntax-directed translation schemes internally 1

Syntax-Directed Translation Schemes A syntax-directed translation scheme (SDT) is an attribute grammar embedded with semantic actions rest + term { print( + ) } rest Embedded semantic action rest + term { print( + ) } rest Example SDT expr expr + term expr expr - term expr term term 0 term 1 term 9 { print( + ) } { print( - ) } { print( 0 ) } { print( 1 ) } { print( 9 ) } 2

Example SDT (cont d) expr term 9 expr { print( + ) } expr + term { print( 2 ) } { print( - ) } - term 2 { print( 5 ) } 5 { print( 9 ) } Translates 9-5+2 into postfix 95-2+ Transforming SDD into SDT SDT are useful to implement S-attributed / L-attributed SDD, even if SDT are a more general mechanisms We (later) provide several algorithms for the transformation from SDD to SDT 3

SDT Implementation SDT can be implemented by Building a parse tree Visit the tree in pre-order, i.e., left-to-right, depth-first order, executing actions as soon as they are encountered SDT Implementation SDT can also be implemented without building the parse tree if The underlying grammar is LL / LR and the SDD is S-attributed / L-attributed We (later) provide several algorithms for the implementation of SDT 4

Postfix SDT SDD is S-attributed and underlying grammar is LR Production L E n E E 1 + T E T T T 1 * F T F F ( E ) F digit Semantic Rule print(e.val) E.val = E 1.val + T.val E.val = T.val T.val = T 1.val * F.val T.val = F.val F.val = E.val F.val = digit.lexval Postfix SDT Add all semantic actions at the end of the production right-hand side L E n {print(e.val)} E E 1 + T {E.val = E 1.val + T.val} E T {E.val = T.val} T T 1 * F {T.val = T 1.val * F.val} T F {T.val = F.val} F ( E ) {F.val = E.val} F digit {F.val = digit.lexval} 5

Postfix SDT Postfix SDT can be implemented during LR parsing by executing actions at reduction time Extra fields are added to the stack to hold the values of synthesized attributes Stack : X X.s Y Y.s Z Z.s top Example: Postfix SDT L E n {print(stack[top 1].val); top = top 1;} E E 1 + T {stack[top 2].val = stack[top 2].val + stack[top].val; top = top 2;} E T T T 1 * F {stack[top 2].val = stack[top 2].val * stack[top].val; top = top 2;} T F F ( E ) F digit {stack[top 2].val = stack[top 1].val; top = top 2;} 6

Example: Postfix SDT Stack $ $ 3 $ F $ T $ T * $ T * 5 $ T * F $ T $ E $ E + $ E + 4 $ E + F $ E + T $ E $ E n $ L value _ 3 3 3 3 _ 3 _ 5 3 _ 5 15 15 15 _ 15 _ 4 15 _ 4 15 _ 4 19 19 _ 19 Input 3*5+4n$ *5+4n$ *5+4n$ *5+4n$ 5+4n$ +4n$ +4n$ +4n$ +4n$ 4n$ n$ n$ n$ n$ $ $ Action shift reduce F digit reduce T F shift shift reduce F digit reduce T T * F reduce E T shift shift reduce F digit reduce T F reduce E E + T shift reduce L E n accept SDT for L-Attributed SDD SDD is L-attributed and underlying grammar is LL SDD to SDT transformation for rule A α B β Embed action for computation of inherited attributes for B immediately before B Place action for computation of synthesized attributes for A at the end of α B β 7

Example: SDT for L-Attributed Production D T L T int T real L L 1, id L id Semantic Rule L.in = T.type T.type = integer T.type = real L 1.in = L.in; addtype(id.entry, L.in) addtype(id.entry, L.in) Translation Scheme D T { L.in = T.type } L T int { T.type = integer } T real { T.type = real } L { L 1.in = L.in } L 1, id { addtype(id.entry, L.in) } L id { addtype(id.entry, L.in) } Example: SDT for L-Attributed Generate code for while statement S while ( C ) S 1 S generates statements (while and others) C generates Boolean expression We assume code for C and S 1 is syntesized by some other productions not shown 8

Example: SDT for L-Attributed Use the following attributes : Inherited S.next : label of code that follows S Synthesized S.code : intermediate code for S, ending with a jump to S.next Inherited C.true : label of code that must be executed if C is true Inherited C.false : label of code that must be executed if C is false Synthesized C.code : intermediate code for C, ending with a jump to either C.true or C.false Example: SDT for L-Attributed SDD for while statement S while ( C ) S 1 L1 = new(); L2 = new(); S 1.next = L1; C.false = S.next; C.true = L2; S.code = label L1 C.code label L2 S 1.code ; 9

Example: SDT for L-Attributed label L1 C intermediate code S.code if result = true goto L2 ( = C.true ) if result = false goto LL ( = C.false ) label L2 S 1 intermediate code Goto L1 ( = S 1.next ) label LL ( = S.next ) C.code S 1.code Example: SDT for L-Attributed SDT for while statement S while ( C ) S 1 { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; } { S 1.next = L1; } { S.code = label L1 C.code label L2 S 1.code ; } 10

Implementation of L-Attributed SDD / SDT Assume underlying grammar is LL, we have three methods Recursive descent parser augmented with argument passing SDT in conjunction with LL parser SDT in conjunction with LR parser L-Attributed SDD and Recursive Descent Parsers Parser has a function A() for each nonterminal A The arguments of A() are the inherited attributes for nonterminal A The return value of A() is the collection of the synthesized attributes for nonterminal A 11

Example: Descent Parsers SDT D T { L.in = T.type } L T int { T.type = integer } T real { T.type = real } L { L 1.in = L.in } L 1, id { addtype(id.entry, L.in) } L id { addtype(id.entry, L.in) } Example: Descent Parsers D T { L.in := T.type } L T int { T.type := integer } T real { T.type := real } Input: inherited attribute Output: synthesized attribute void D() { Type Ttype = T(); Type Lin = Ttype; L(Lin); } Type T() { Type Ttype; if (current input == INT) { Ttype = TYPE_INT; advance input; } else if (current input == REAL) { Ttype = TYPE_REAL; advance input; } else error(); return Ttype; } void L(Type Lin) { } 12

Example: Descent Parsers SDT S while ( { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; } C ) { S 1.next = L1; } S 1 { S.code = label L1 C.code label L2 S 1.code ; } Example: Descent Parsers S while ( { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; } C ) { S 1.next = L1; } S 1 { S.code = label L1 C.code label L2 S 1.code ; } string S(label next) { string Scode, Ccode; label L1, L2; } // local vars // local labels if (current input == while ) { advance input; check ( and advance L1 = new(); L2 = new(); Ccode = C(next, L2); check ) and advance Scode = S(L1); return ( "label" L1 Ccode "label" L2 Scode); } else /* other statement types */ 13

L-Attributed SDD and On-The-Fly Code Generation Storing generated code in synthesized attributes is inefficient, copying long strings is time consuming More efficient to incrementally generate code into some output buffer L-Attributed SDD and On-The-Fly Code Generation Assumptions Code generated in a string-valued, synthesized attribute Code produced by concatenation of code at children preserving the order, with possibly additional symbols We can incrementally emit the string components that are not main attributes 14

Example S while ( { L1 = new(); L2 = new(); C.false = S.next; C.true = L2; } C ) { S 1.next = L1; } S 1 { S.code = label L1 C.code label L2 S 1.code ; } void S(label next) { label L1, L2; } // local labels if (current input == while ) { advance input; check ( and advance L1 = new(); L2 = new(); print( "label", L1 ); C(next, L2); check ) and advance print( "label", L2 ); S(L1); } else /* other statement types */ L-Attributed SDD and LL Parsers When nonterminal A is at the top of the stack, its parent and left siblings are no longer there Impossible to transfer inherited attributes to A When nonterminal A is expanded, it disappears from the stack Impossible to collect synthesized attributes for A 15

L-Attributed SDD and LL Parsers Extend LL stack to include Nonterminal record for A along with inherited attributes for A Action record representing actions for A, placed above A s nonterminal record Synthesized record for A, representing synthesized attributes and copy actions, placed below A s nonterminal record L-Attributed SDD and LL Parsers Consider CFG production A B C Action record for C also used to store copies of inherited attributes, since A, B are no longer in the stack when C is processed Synthesized record for A is placed below A, and survives when A (and B and C) are removed from the stack 16

L-Attributed SDD and LL Parsers Rule: A B C A action B record Attributes from A s parent and A s left siblings A record A synth C record Attributes from B and C A synth Example top S next = x 17

top Example while ( Action snext = x L1 =? L2 =? C false =? true =? ) Action al1 =? al2 =? S 1 next = x L1 = new(); L2 = new(); stack[top 1].false = snext; stack[top 1].true = L2; stack[top 3].al1 = L1; stack[top 3].al2 = L2; print( "label", L1 ); stack[top 1].next = al1; print( "label", al2 ); L-Attributed SDD and LR Parsers Assume L-attributed SDD over LL grammar We adapt the grammar to compute the translation during LR parsing Convert SDD into SDT Replace each embedded action {a} with marker nonterminal M a Add new production M a ε {a }, with action a adapted from a 18

L-Attributed SDD and LR Parsers For rules A α M a β and M a ε {a }, modified action a defined as Attributes of A or attributes of symbols in α that are used by a are copied as inherited attributes of M a Action a defined as a, but newly computed attributes become synthesized attributes of M a L-Attributed SDD and LR Parsers Extra fields are added to the stack to hold the values of synthesized / inherited attributes top stack : val_s : X X.s Y Y.s Z Z.s val_i : X.i Y.i Z.i 19

Example SDT: S a A { C.i = f(a.s) } C S b A B { C.i = f(a.s) } C C c { C.s = g(c.i) } Transformed SDT: S a A M 1 C S b A B M 2 C M 1 ε {val_s[top + 1] = f(val_s[top]); top = top +1 } M 2 ε {val_s[top + 1] = f(val_s[top - 1]); top = top +1 } C c {val_i[top] = g(val_i[top - 1]) } L-Attributed SDD and LR Parsers If underlying grammars is strictly LR, we cannot do L-attributed SDD Consider production A B C Inherited attributes of B depend on inherited attributes of A But in LR parsing, when B is processed A is not known 20

Summary of Presented Algorithms SDD > SDT transformation S-attributed SDD > Postfix SDT L-attributed SDD > SDT SDT implementation Postfix SDT via LR L-attributed, LL SDT via recursive descent L-attributed, LL SDT via LL parsing L-attributed, LL SDT via LR parsing 21