Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
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.
CPSC 388 – Compiler Design and Construction
Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Top-Down Parsing - recursive descent - predictive parsing
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
# 1 CMPS 450 Syntax-Directed Translations CMPS 450 J. Moloney.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Parsing Top-Down.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
More Parsing CPSC 388 Ellen Walker Hiram College.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Syntax and Semantics Structure of programming languages.
A Simple Syntax-Directed Translator
Introduction to Parsing
Programming Languages Translator
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Regular Grammar - Finite Automaton
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Syntax-Directed Translation
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist Party came to power 51 years ago. It may take a bit of Hogwart's magic to make those books disappear. testing Babelfish: English to German to French to English to French to German to English Toepfer Harry arrived to China and is here the largest unit of magnetic cards for a work of the invention met, since the communist party came, in order to begin 51 years. It can take few magic Hogwart, in order to make, around to disappear these books.

CS 536 Fall Motivation: parser as a translator syntax-directed translation parser syntax + translation rules (typically hardcoded in the parser) stream of tokens ASTs, or assembly code

CS 536 Fall Mechanism of syntax-directed translation syntax-directed translation is done by extending the CFG –a translation rule is defined for each production given X  d A B c the translation of X is defined in terms of translation of nonterminals A, B values of attributes of terminals d, c constants

CS 536 Fall To translate an input string: 1.Build the parse tree. 2.Working bottom-up Use the translation rules to compute the translation of each nonterminal in the tree Result: the translation of the string is the translation of the parse tree's root nonterminal. Why bottom up? a nonterminal's value may depend on the value of the symbols on the right-hand side, so translate a non-terminal node only after children translations are available.

CS 536 Fall Example 1: arith expr to its value Syntax-directed translation: the CFG translation rules E  E + T E 1.trans = E 2.trans + T.trans E  T E.trans = T.trans T  T * F T 1.trans = T 2.trans * F.trans T  F T.trans = F.trans F  int F.trans = int.value F  ( E ) F.trans = E.trans

CS 536 Fall Example 1 (cont) Input: 2 * (4 + 5) Annotated Parse Tree E (18) T (18) F (9) T (2) F (2) E (9) T (5) F (5) E (4) T (4) F (4) * ) * ( int (2) int (4) int (5)

CS 536 Fall Example 2: Compute the type of an expression E -> E + E if ((E 2.trans == INT) and (E 3.trans == INT) then E 1.trans = INT else E 1.trans = ERROR E -> E and E if ((E 2.trans == BOOL) and (E 3.trans == BOOL) then E 1.trans = BOOL else E 1.trans = ERROR E -> E == E if ((E 2.trans == E 3.trans) and (E 2.trans != ERROR)) then E 1.trans = BOOL else E 1.trans = ERROR E -> true E.trans = BOOL E -> false E.trans = BOOL E -> int E.trans = INT E -> ( E ) E 1.trans = E 2.trans

CS 536 Fall Example 2 (cont) Input: (2 + 2) == 4 1.parse tree: 2.annotation:

CS 536 Fall TEST YOURSELF #1 A CFG for the language of binary numbers: B  0  1  B 0  B 1 Define a syntax-directed translation so that the translation of a binary number is its base- 10 value. Draw the parse tree for 1001 and annotate each nonterminal with its translation.

CS 536 Fall Building Abstract Syntax Trees Examples so far, streams of tokens translated into –integer values, or –types Translating into ASTs is not very different

CS 536 Fall AST vs Parse Tree AST is condensed form of a parse tree –operators appear at internal nodes, not at leaves. –"Chains" of single productions are collapsed. –Lists are "flattened". –Syntactic details are ommitted e.g., parentheses, commas, semi-colons AST is a better structure for later compiler stages –omits details having to do with the source language, –only contains information about the essential structure of the program.

CS 536 Fall Example: 2 * (4 + 5) parse tree vs AST E T FT F E T F E T F * ) * ( int (2) int (5) * int (4)

CS 536 Fall Definitions of AST nodes class ExpNode { } class IntLitNode extends ExpNode { public IntLitNode(int val) {...} } class PlusNode extends ExpNode { public PlusNode( ExpNode e1, ExpNode e2 ) {... } } class TimesNode extends ExpNode { public TimesNode( ExpNode e1, ExpNode e2 ) {... } }

CS 536 Fall AST-building translation rules E 1  E 2 + T E 1.trans = new PlusNode(E 2.trans, T.trans) E  T E.trans = T.trans T 1  T 2 * F T 1.trans = new TimesNode(T 2.trans, F.trans) T  F T.trans = F.trans F  int F.trans = new IntLitNode(int.value) F  ( E ) F.trans = E.trans

CS 536 Fall TEST YOURSELF #2 Illustrate the syntax-directed translation defined above by –drawing the parse tree for * 4, and –annotating the parse tree with its translation i.e., each nonterminal X in the parse tree will have a pointer to the root of the AST subtree that is the translation of X.

CS 536 Fall Syntax-Directed Translation and LL Parsing not obvious how to do this, since –predictive parser builds the parse tree top-down, –syntax-directed translation is computed bottom-up. could build the parse tree (inefficient!) Instead, add a semantic stack: –holds nonterminals' translations –when the parse is finished, the semantic stack will hold just one value: the translation of the root nonterminal (which is the translation of the whole input).

CS 536 Fall How does semantic stack work? How to push/pop onto/off the semantic stack? –add actions to the grammar rules. The action for one rule must: –Pop the translations of all rhs nonterminals. –Compute and push the translation of the lhs nonterminal. Actions are represented by action numbers, –action numbers become part of the rhs of the grammar rules. –action numbers pushed onto the (normal) stack along with the terminal and nonterminal symbols. –when an action number is the top-of-stack symbol, it is popped and the action is carried out.

CS 536 Fall Keep in mind action for X  Y 1 Y 2... Y n is pushed onto the (normal) stack when the derivation step X  Y 1 Y 2... Y n is made, but the action is performed only after complete derivations for all of the Y's have been carried out.

CS 536 Fall Example: Counting Parentheses E 1   E 1.trans = 0  ( E 2 ) E 1.trans = E 2.trans + 1  [ E 2 ] E 1.trans = E 2.trans

CS 536 Fall Example: Step 1 replace the translation rules with translation actions. –Each action must: Pop rhs nonterminals' translations from the semantic stack. Compute and push the lhs nonterminal's translation. Here are the translation actions: E   push(0);  ( E ) exp2Trans = pop(); push( exp2Trans + 1 );  [ E ] exp2Trans = pop(); push( exp2Trans );

CS 536 Fall Example: Step 2 each action is represented by a unique action number, –the action numbers become part of the grammar rules: E   #1  ( E ) #2  [ E ] #3 #1: push(0); #2: exp2Trans = pop(); push( exp2Trans + 1 ); #3: exp2Trans = pop(); push( exp2Trans );

CS 536 Fall Example: example input so far stack semantic stack action ( E EOF pop, push "( E ) #2" ( (E) #2 EOF pop, scan ([ E) #2 EOF pop, push "[ E ]" ([ [E] ) #2 EOF pop, scan ([] E] ) #2 EOF pop, push  #1 ([] #1 ] ) #2 EOF pop, do action ([] ] ) #2 EOF 0 pop, scan ([]) ) #2 EOF 0 pop, scan ([]) EOF #2 EOF 0 pop, do action ([]) EOF EOF 1 pop, scan ([]) EOF empty stack: input accepted! translation of input = 1

CS 536 Fall What if the rhs has >1 nonterminal? pop multiple values from the semantic stack: –CFG Rule: methodBody  { varDecls stmts } –Translation Rule: methodBody.trans = varDecls.trans + stmts.trans –Translation Action: stmtsTrans = pop(); declsTrans = pop(); push(stmtsTrans + declsTrans ); –CFG rule with Action: methodBody  { varDecls stmts } #1 #1: stmtsTrans = pop(); declsTrans = pop(); push( stmtsTrans + declsTrans );

CS 536 Fall Terminals Simplification: –we assumed that each rhs contains at most one terminal How to push the value of a terminal? –a terminal’s value is available only when the terminal is the "current token“: put action before the terminal –CFG Rule: F  int –Translation Rule: F.trans = int.value –Translation Action: push( int.value ) –CFG rule with Action: F  #1 int // action BEFORE terminal #1: push( currToken.value )

CS 536 Fall Handling non-LL(1) grammars Recall that to do LL(1) parsing –non-LL(1) grammars must be transformed e.g., left-recursion elimination –the resulting grammar does not reflect the underlying structure of the program E  E + T vs. E  T E' E’   | + T E' How to define syntax directed translation for such grammars?

CS 536 Fall The solution is simple! Treat actions as grammar symbols –define syntax-directed translation on the original grammar: define translation rules convert them to actions that push/pop the semantic stack incorporate the action numbers into the grammar rules –then convert the grammar to LL(1) treat action numbers as regular grammar symbols

CS 536 Fall Example non-LL(1):E  E + T #1  T T  T * F #2  F #1: TTrans = pop(); ETrans = pop(); push Etrans + TTrans; #2: FTrans = pop(); TTrans = pop(); push Ttrans * FTrans; after removing immediate left recursion: E  T E' E‘  + T #1 E'   T  F T' T'  * F #2 T'  

CS 536 Fall TEST YOURSELF #3 For the following grammar, give –translation rules + translation actions, –a CFG with actions so that the translation of an input expression is the value of the expression. Do not worry that the grammar is not LL(1). then convert the grammar (including actions) to LL(1) E  E + T | E – T | T T  T * F | T / F | F F  int | ( E )