Syntax-Directed Translation. Lecture 14

Similar documents
Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Introduction to Parsing. Lecture 8

Bottom-Up Parsing. Lecture 11-12

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Bottom-Up Parsing. Lecture 11-12

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

Syntax and Grammars 1 / 21

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

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

Recursive Descent Parsers

Lecture 8: Deterministic Bottom-Up Parsing

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Syntax Errors; Static Semantics

Abstract Syntax Trees & Top-Down Parsing

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

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Lecture 7: Deterministic Bottom-Up Parsing

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Parsing CSCI-400. Principles of Programming Languages.

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

CS 406: Syntax Directed Translation

CMSC 330: Organization of Programming Languages

Chapter 3. Parsing #1

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

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Lexical and Syntax Analysis. Top-Down Parsing

Introduction to Syntax Analysis Recursive-Descent Parsing

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

CSE 401 Midterm Exam Sample Solution 2/11/15

Chapter 4. Lexical and Syntax Analysis

LECTURE 3. Compiler Phases

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

Context-Free Grammars

CS 4120 Introduction to Compilers

Introduction to Parsing. Lecture 5

A Simple Syntax-Directed Translator

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

COP 3402 Systems Software Syntax Analysis (Parser)

Alternatives for semantic processing

Project Compiler. CS031 TA Help Session November 28, 2011

Syntax Analysis. Chapter 4

CS 314 Principles of Programming Languages

Downloaded from Page 1. LR Parsing

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

Last Time. What do we want? When do we want it? An AST. Now!

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Introduction to Parsing. Lecture 5

Syntax Analysis Part I

Syntax-Directed Translation. Introduction

CS 406/534 Compiler Construction Putting It All Together

Lecture 14 Sections Mon, Mar 2, 2009

CSE P 501 Exam 8/5/04

Announcements. Working in pairs is only allowed for programming assignments and not for homework problems. H3 has been posted

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Top down vs. bottom up parsing

CSE 3302 Programming Languages Lecture 2: Syntax

4. Lexical and Syntax Analysis

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Context-free grammars (CFG s)

CSCI312 Principles of Programming Languages

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Lexical and Syntax Analysis

How do LL(1) Parsers Build Syntax Trees?

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Semantic actions for declarations and expressions

4. Lexical and Syntax Analysis

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

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

MIDTERM EXAM (Solutions)

Lexical and Syntax Analysis

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

CS 164, Spring 2010 CS 164: Test #1 Solutions P. N. Hilfinger DO NOT. Copyright (c) 2010 by P. N. Hilfinger. Copying and reproduction prohibited.

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Programming Language Syntax and Analysis

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

Compiler construction lecture 3

Lexical and Syntax Analysis. Bottom-Up Parsing

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

UNIVERSITY OF CALIFORNIA

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

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

A simple syntax-directed

Transcription:

Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1

Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs, or assembly code syntax + translation rules (typically hardcoded in the parser) 9/27/2006 Prof. Hilfinger, Lecture 14 2

Outline Syntax-directed translation: specification translate parse tree to its value, or to an AST type check the parse tree Syntax-directed translation: implementation during LR parsing during recursive-descent parsing 9/27/2006 Prof. Hilfinger, Lecture 14 3

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 recursively using translation of nonterminals A, B values of attributes of terminals d, c constants 9/27/2006 Prof. Hilfinger, Lecture 14 4

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. 9/27/2006 Prof. Hilfinger, Lecture 14 5

Example 1: Arithmetic expression to value Syntax-directed 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 9/27/2006 Prof. Hilfinger, Lecture 14 6

Example 1: Bison/Yacc Notation E : E + T { $$ = $1 + $3; } T : T * F { $$ = $1 * $3; } F : int { $$ = $1; } F : ( E ) { $$ = $2; } KEY: $$ : Semantic value of left-hand side $n : Semantic value of n th symbol on right-hand side 9/27/2006 Prof. Hilfinger, Lecture 14 7

Example 1 (cont): Annotated Parse Tree Input: 2 * (4 + 5) E (18) T (18) T (2) * F (9) F (2) ( E (9) ) int (2) E (4) * T (5) T (4) F (4) F (5) 9/27/2006 Prof. Hilfinger, Lecture 14 8 int (4) int (5)

Example 2: Compute the type of an expression E -> E + E if $1 == INT and $3 == INT: $$ = INT else: $$ = ERROR E -> E and E if $1 == BOOL and $3 == BOOL: $$ = BOOL else: $$ = ERROR E -> E == E if $1 == $3 and $2!= ERROR: $$ = BOOL else: $$ = ERROR E -> true $$ = BOOL E -> false $$ = BOOL E -> int $$ = INT E -> ( E ) $$ = $2 9/27/2006 Prof. Hilfinger, Lecture 14 9

Example 2 (cont) Input: (2 + 2) == 4 E (BOOL) E (INT) == E (INT) ( E (INT) ) int (INT) E (INT) + E (INT) int (INT) int (INT) 9/27/2006 Prof. Hilfinger, Lecture 14 10

Building Abstract Syntax Trees Examples so far, streams of tokens translated into integer values, or types Translating into ASTs is not very different 9/27/2006 Prof. Hilfinger, Lecture 14 11

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 omitted 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. 9/27/2006 Prof. Hilfinger, Lecture 14 12

Example: 2 * (4 + 5) Parse tree vs. AST E T * T * F 2 + F ( E ) 4 5 int (2) E T * T F F int (5) int (4) 9/27/2006 Prof. Hilfinger, Lecture 14 13

AST-building translation rules E E + T $$ = new PlusNode($1, $3) E T $$ = $1 T T * F $$ = new TimesNode($1, $3) T F $$ = $1 F int $$ = new IntLitNode($1) F ( E ) $$ = $2 9/27/2006 Prof. Hilfinger, Lecture 14 14

Example: 2 * (4 + 5): Steps in Creating AST E * T 2 4 + 5 2 T F * ( F E ) 4 + 5 (Only some of the semantic values are shown) int (2) E T * T F F int (5) int (4) 9/27/2006 Prof. Hilfinger, Lecture 14 15

Syntax-Directed Translation and LR Parsing add semantic stack, parallel to the parsing stack: each symbol (terminal or non-terminal) on the parsing stack stores its value on the semantic stack holds terminals attributes, and holds nonterminals' translations when the parse is finished, the semantic stack will hold just one value: the translation of the root non-terminal (which is the translation of the whole input). 9/27/2006 Prof. Hilfinger, Lecture 14 16

Semantic actions during parsing when shifting push the value of the terminal on the semantic stack when reducing pop k values from the semantic stack, where k is the number of symbols on production s RHS push the production s value on the semantic stack 9/27/2006 Prof. Hilfinger, Lecture 14 17

An LR example Grammar + translation rules: E E + ( E ) $$ = $1 + $4 E int $$ = $1 Input: 2 + ( 3 ) + ( 4 ) 9/27/2006 Prof. Hilfinger, Lecture 14 18

Shift-Reduce Example with evaluations parsing stack I int + (int) + (int)$ shift semantic stack I 9/27/2006 Prof. Hilfinger, Lecture 14 19

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift int I + (int) + (int)$ red. E int I 2 I 9/27/2006 Prof. Hilfinger, Lecture 14 20

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I 9/27/2006 Prof. Hilfinger, Lecture 14 21

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I 9/27/2006 Prof. Hilfinger, Lecture 14 22

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I 9/27/2006 Prof. Hilfinger, Lecture 14 23

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I 9/27/2006 Prof. Hilfinger, Lecture 14 24

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I E I + (int)$ shift 3 times 5 I 9/27/2006 Prof. Hilfinger, Lecture 14 25

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E int 5 + ( 4 I 9/27/2006 Prof. Hilfinger, Lecture 14 26

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E int 5 + ( 4 I E + (E I )$ shift 5 + ( 4 I 9/27/2006 Prof. Hilfinger, Lecture 14 27

Shift-Reduce Example with Evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E int 5 + ( 4 I E + (E I )$ shift 5 + ( 4 I E + (E) I $ red. E E + (E) 5 + ( 4 ) I 9/27/2006 Prof. Hilfinger, Lecture 14 28

Shift-Reduce Example with evaluations I int + (int) + (int)$ shift I int I + (int) + (int)$ red. E int 2 I E I + (int) + (int)$ shift 3 times 2 I E + (int I ) + (int)$ red. E int 2 + ( 3 I E + (E I ) + (int)$ shift 2 + ( 3 I E + (E) I + (int)$ red. E E + (E) 2 + ( 3 ) I E I + (int)$ shift 3 times 5 I E + (int I )$ red. E int 5 + ( 4 I E + (E I )$ shift 5 + ( 4 I E + (E) I $ red. E E + (E) 5 + ( 4 ) I E I $ accept 9 I 9/27/2006 Prof. Hilfinger, Lecture 14 29

Taking Advantage of Derivation Order So far, rules have been functional; no side effects except to define (once) value of LHS. LR parsing produces reverse rightmost derivation. Can use the ordering to do control semantic actions with side effects. 9/27/2006 Prof. Hilfinger, Lecture 14 30

Example of Actions with Side Effects E E + T print +, E T pass T T * F print *, T F pass F int print $1, F ( E ) pass We know that reduction taken after all the reductions that form the nonterminals on right-hand side. So what does this print for 3+4*(7+1)? 3 4 7 1 + * + 9/27/2006 Prof. Hilfinger, Lecture 14 31

Recursive-Descent Translation Translating with recursive descent is also easy. The semantic values (what Bison calls $$, $1, etc.), become return values of the parsing functions We ll also assume that the lexer has a way to return lexical values (e.g., the scan function introduced in Lecture 9 might do so). 9/27/2006 Prof. Hilfinger, Lecture 14 32

Example of Recursive-Descent Translation E T E+T T P T*P P int ( E ) def E(): T () while next() == + : scan( + ); T() def T(): P() while next() == * : scan( * ); P() def P(): if next()==int: scan (int) elif next()== ( : scan( ( ) E() scan( ) ) else: ERROR() (we ve cheated and used loops; see end of lecture 9) 9/27/2006 Prof. Hilfinger, Lecture 14 33

Example contd.: Add Semantic Values E T E+T T P T*P P int ( E ) def E(): v = T () while next() == + : scan( + ); v += T() return v def T(): v = P() while next() == * : scan( * ); v *= P() return v def P(): if next()==int: v = scan (int) elif next()== ( : scan( ( ) v = E() scan( ) ) else: ERROR() return v 9/27/2006 Prof. Hilfinger, Lecture 14 34

Table-Driven LL(1) We can automate all this, and add to the LL(1) parser method from Lecture 9. However, this gets a little involved, and I m not sure it s worth it. (That is, let s leave it to the LL(1) parser generators for now!) 9/27/2006 Prof. Hilfinger, Lecture 14 35