Syntactic Directed Translation

Similar documents
Abstract Syntax Tree

Syntax Directed Translation

Syntax-Directed Translation Part II

Syntax-Directed Translation Part I

More On Syntax Directed Translation

Summary: Semantic Analysis

CSE302: Compiler Design

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

Syntax-Directed Translation

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

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

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

[Syntax Directed Translation] Bikash Balami

Syntax-Directed Translation. Introduction

Syntax-Directed Translation

CS 406: Syntax Directed Translation

Syntactic Directed Translation

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

Attribute Grammars. Attribute Grammars

CSCI Compiler Design

Syntax-Directed Translation. Lecture 14

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

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

LR Parsing LALR Parser Generators

Alternatives for semantic processing

CS 4120 Introduction to Compilers

Chapter 4. Action Routines

Lexical Analysis. Introduction

Semantic actions for declarations and expressions

Syntax-Directed Translation

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

LR Parsing LALR Parser Generators

Chapter 4 - Semantic Analysis. June 2, 2015

Semantic Analysis with Attribute Grammars Part 3

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Principles of Programming Languages

Using an LALR(1) Parser Generator

CSCI Compiler Design

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

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

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Building a Parser Part III

Introduction to Bottom-Up Parsing

Chapter 4 :: Semantic Analysis

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

CS 536 Midterm Exam Spring 2013

Syntax-Directed Translation

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Context-free grammars

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

5. Semantic Analysis!

Principles of Programming Languages

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Semantic Analysis Attribute Grammars

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Compiler Construction: Parsing

Context-sensitive Analysis

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Chapter 4. Lexical and Syntax Analysis

COP4020 Spring 2011 Midterm Exam

CSCI Compiler Design

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Intermediate Representations & Symbol Tables

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Intermediate Code Generation

Question Points Score

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CSCI Compiler Design

Semantic actions for expressions

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

UNIT III & IV. Bottom up parsing

QUESTIONS RELATED TO UNIT I, II And III

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

Syntactic Analysis. Top-Down Parsing

Intermediate Code Generation

CMSC 330: Organization of Programming Languages. Context Free Grammars

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiler Lab. Introduction to tools Lex and Yacc

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

Semantic Analysis with Attribute Grammars Part 1

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

Programming Languages

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

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

LECTURE 3. Compiler Phases

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

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

Lecture Bottom-Up Parsing

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Transcription:

Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use. 1

Syntax-Directed Translations Rule-based Evaluation & Syntax-Directed Translation Semantic Actions are Embedded in Productions Evaluation Order and Parsing Top-Down: Associate Actions with Production Function Code Bottom-Up: Execute Actions on a Reduction Single Pass Translation Schemes Fast! No need to Construct the Parse Tree and then do a Topological Sorting to Find out Feasible Order for Evaluation of Issues: Dealing with Embedded Actions May Require Inserting Additional Symbols, Markers Dealing with Inherited Attributes in Bottom-Up Parsing Reach into the Stack for Value of Attribute Position Independence in the Stack 2

Example: Building an AST Assume Constructors for Each Node e.g., malloc in C) Assume Stack holds Pointers to Nodes & Symbols Assume YACC Syntax with Refs to Stack Depth Goal $$ = $1; + $$ = MakeAddNode$1,$3); $$ = MakeSubNode$1,$3); $$ = $1; * $$ = MakeMulNode$1,$3); / $$ = MakeDivNode$1,$3); $$ = $1; ) $$ = $2; ber $$ = MakeNumNode$1.token); id $$ = MakeIdNode$1.token); Semantic Rules Executed on Reduction of corresponding Production During Parsing 3

Stack Symbols & Symbols) + ) * Parser Action Input 4

+ ) * Shift: push onto stack 5

+ ) * Shift: push onto stack 6

Node + ) * Reduce: $$=MakeNumNode$1.token) 7

Node + ) * Reduce: $$ = $1 8

Node + ) * Reduce: $$ = $1 9

+ + Node + ) * Shift: push + onto stack 10

+ + Node + ) * Shift: push onto stack 11

+ + Node Node + ) * Reduce: $$ = MakeNumNode$1.token) 12

+ + Node Node + ) * Reduce: $$ = $1 13

+ + Node Node + ) * 14

add Node + Node Node + ) * Reduce: + $$ = MakeAddNode$1,$3) 15

add Node ) ) + Node Node + ) * Shift: push ) onto stack 16

add Node ) ) + Node Node + ) * 17

) add Node + Node Node + ) * Reduce: ) $$ = $2 18

) add Node + Node Node + ) * Reduce: $$ = $1 19

) add Node + Node Node + ) * Reduce: Reduce $$ = $1 20

* ) add Node + * Node Node + ) * Shift: push * onto stack 21

* add Node ) + * Node Node + ) * Shift: push onto stack 22

* add Node Node ) + * Node Node + ) * Reduce: $$ = MakeNumNode$1.token) 23

* add Node Node ) + * Node Node + ) * Reduce: $$ = $1 24

mult Node * Node ) add Node + Node Node + ) * Reduce: * $$ = MakeMultNode$1,$3) 25

Example: Calculator Revisited Use Parser Stack for Symbols and Attribute Values As Parser Algorithm Places Symbols Semantic Actions use Stack locations for Storing Values Reach Into Stack for Values of Symbols When Reduction, Evaluate Semantic Action and Store Value of Production LHS val val 1 val val 1 val 0 0 1 * { 0.val = 1.val *.val } 26

Example: Calculator Revisited Production Actions Line { print.val); } 0 0 + { 0.val = 1.val +.val; } { 0.val =.val; } 0 1 * { 0.val = 1.val *.val; } { 0.val =.val; } ) {.val =.val; } digit {.val = digit.lexval; } 27

Example: Calculator Revisited Production Actions Line { print.val); } 0 0 + { 0.val = 1.val +.val; } { 0.val =.val; } 0 1 * { 0.val = 1.val *.val; } { 0.val =.val; } ) {.val =.val; } digit {.val = digit.lexval; } Line { printstack[top].val); } 0 0 + { stack[top-2].val = stack[top-2].val + stack[top].val; top = top - 2; } { } 0 1 * { stack[top-2].val = stack[top-2].val + stack[top].val; { } top = top - 2; } ) { stack[top-2].val = stack[top-1].val; top = top 2;} digit { stack[top].val = digit.lexval; } 28

Translation Schemes L-Attributed Syntax-Directed Definition where: Embed Semantic Actions in { } Positioned Between Symbols of Production Useful for Specifying Translation During Parsing Attribute Values Must be Available when Actions Refer to it 1. Inherited attributes for a symbol on the RHS of a production must be computed in an action before that Symbol. 2. An action must not refer to a synthesized attribute of a symbol to the right of the action. 3. A synthesized attribute for the symbol on the RHS of a production can only be computed after all attributes it references have been computed. Easily Implemented in Bottom-Up Parsers 29

L-Attributed Definitions A Syntax-Directed Definition is L-Attributed if Each inherited Attribute X j,, for depends only on: The Attributes of the Symbols The Inherited Attributes of A 1 j n A X 1 X 2,..., X n X 1, X 2,..., X j 1 to the left of X j Values Flow from Left-to-Right in the Parse Tree. Still an S-Attributed Definition Restrictions are for Inherited Attributes Only. Can be Evaluated in a Depth-first Traversal In Many Cases even in a Single Pass 30

L-Attributed Def.: Evaluation Order procedure dfvisitn:node) begin foreach child m of n from left to right do evaluate inherited attributes of m; dfvisitm); end evaluate synthesized attributes of n end What to do When? Embedded Actions Inherited Attributes Replacing Inherited Attributes by Synthesized Attributes 31

Embedded Actions Actions are Executed when Parser Reduces a Production After reductions for the RHS have occurred Values for the Symbols available on the stack What to do with Embedded Actions? A X { action } Y Z The action should execute before the actions for Y and Z 32

Embedded Actions Actions are Executed when Parser Reduces a Production After reductions for the RHS have occurred Values for the Symbols available on the stack What to do with Embedded Actions? A X { action } Y Z The action should execute before the actions for Y and Z Transform the Grammar adding a Marker Symbol using an empty RHS production for Marker A X M Y Z M ε { action } 33

Inherited Attributes For the production A X Y when the parser reduces X s production, its attributes will be on the top of the stack If Y uses synthesized attributes X.s from X just needs to copy value from the top of the stack into computation of Y s attributes Observation: Reaching into the Stack Works If you know the position of the symbol s attributes Look at the corresponding grammar s production Production S aac S babc C c Semantic Rule C.i =A.s C.i =A.s C.s =funcc.i) Problem: There maybe a B symbol between A and C and thus the relative position of the synthesized attribute A.s on the stack is not known to compute C.i. Solution? X s A Y i 34

Inherited Attributes Solution: Insert a Marker Symbol just before C in one of the productions Use M to copy inherited to synthesize attributes Redo Actions to use M s synthesize attributes S Production S aac S babmc C c M ε S b A C s i Semantic Rule C.i =A.s M.i =A.s; C.i = M.s C.s =funcc.i) M.s =M.i S b A B C s b A B M C s i s i i 35

Replacing Inherited with Synthesized Attrib. Inherited Attributes are Hard to Handle Alternative: Modify Grammar whenever possible) Example: List of Declaration in Pascal. Type as an Inherited Attribute Change Grammar and Type is Synthesized Decl List : Type Type integer real List List, id id Decl id List List, id List : Type Type integer real 36

Replacing Inherited with Synthesized Attrib. Decl List : Type Type integer real List List, id id Decl id List List, id List : Type Type integer real Decl Decl List : Type i id List List, id, id List id : Type s 37

Example What Does This Scheme Do? 1 0 addop { printaddop.lexeme)} 1 ε { print.val)} Input: 9-5 + 2 9 5 - + 2 ε 38

Example What Does This Scheme Do? 1 0 addop { printaddop.lexeme)} 1 ε { print.val)} Input: 9-5 + 2 9 { print 9 ) } 5 - { print 5 ) } 2 { print - ) } + { print 2 ) } { print + ) } ε 39

Example What Does This Scheme Do? 1 0 addop { printaddop.lexeme)} 1 ε { print.val)} Input: 9-5 + 2 9 depth-first evaluation order { print 9 ) } 5 - { print 5 ) } 2 { print - ) } + { print 2 ) } { print + ) } Output: 9 5-2 + ε 40

Attribute Grammar Summary Augment CFG with Attributes and Rules Inherited and Synthesized Attributes Find Dependence Graph and Evaluation Order Useful for Semantic Analysis Important Class: L-Attributed Grammar Information moves from Left-to-Right Inherited Attributes and Embedded Actions can be Resolved Semantic Actions Executed upon Production Reduce Operations Can be Evaluated Bottom-Up in a Single Pass 41