CSE302: Compiler Design

Similar documents
Syntax-Directed Translation Part II

Syntax-Directed Translation

Syntax Directed Translation

Principles of Programming Languages

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

Abstract Syntax Trees Synthetic and Inherited Attributes

CSE302: Compiler Design

CSE302: Compiler Design

Recursive Descent Parsers

CSE302: Compiler Design

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntactic Directed Translation

Chapter 4. Action Routines

Syntax-Directed Translation. Introduction

[Syntax Directed Translation] Bikash Balami

CSE302: Compiler Design

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

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Principles of Programming Languages

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax-Directed Translation

COP4020 Spring 2011 Midterm Exam

CSE302: Compiler Design

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

Lexical and Syntax Analysis. Bottom-Up Parsing

Lecture 14 Sections Mon, Mar 2, 2009

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

Building Compilers with Phoenix

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

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

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

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

Syntax-Directed Translation

Syntax-Directed Translation Part I

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

Programming Languages

Static and Dynamic Semantics

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

COP5621 Exam 3 - Spring 2005

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

Summary: Semantic Analysis

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

Chapter 4. Lexical and Syntax Analysis

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

Semantic Analysis Attribute Grammars

Compiler construction in4303 lecture 3

CSCI312 Principles of Programming Languages

Abstract Syntax Tree

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

Lexical and Syntax Analysis. Top-Down Parsing

COP4020 Programming Assignment 2 Spring 2011

Syntax-Directed Translation. Lecture 14

CSE302: Compiler Design

CS 4120 Introduction to Compilers

Chapter 4: LR Parsing

Syntax-Directed Translation

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

CSCI Compiler Design

Chapter 4 :: Semantic Analysis

CSCI Compiler Design

LR Parsing LALR Parser Generators

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

LR Parsing - The Items

Table-Driven Top-Down Parsers

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Table-Driven Parsing

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

How do LL(1) Parsers Build Syntax Trees?

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

Compiler construction lecture 3

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

Abstract Syntax Trees & Top-Down Parsing

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

Syntax-Directed Translation

LR Parsing LALR Parser Generators

Class Information ANNOUCEMENTS

Syntax Analysis Part I

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

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

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

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


Context-free grammars

CS 406: Syntax Directed Translation

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

CS 314 Principles of Programming Languages

Alternatives for semantic processing

Syntax Directed Translation

Compiler Construction: Parsing

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

The procedure attempts to "match" the right hand side of some production for a nonterminal.

CS502: Compilers & Programming Systems

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

Lexical and Syntax Analysis

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

4. Lexical and Syntax Analysis

MIDTERM EXAM (Solutions)

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

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

Transcription:

CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007

Outline Recap Syntax-directed translation (Chapter 5) Summary and homework

SDD s For synthesized attributes Perform bottom-up tree traversal for attribute evaluation An SDD is S-attributed if every attribute is synthesized For SDD s with both inherited and synthesized attributes Dependency graphs An SDD is L-attributed if in all of its dependency graphs the edges only go from left to right but not from right to left

Syntax-directed Translation Schemes Note that SDD is used for specifications SDD SDT SDT s are implemented during parsing without building a parse tree S-attributed SDD based on LR-parsable grammar L-attributed SDD based on LL-parsable grammar

S-attributed SDD s Based on LRparsable Grammars SDD SDT Construct an SDT where actions are placed at the end of the productions corresponding to semantic rules Postfix SDT s SDD Production Semantic rules T T 1 * F T.val = T 1.val * F.val T F T.val = F.val F digit F.val = digit.lexval SDT T T 1 * F { T.val = T 1.val * F.val; } T F { T.val = F.val; } F digit { F.val = digit.lexval; } An action is executed along with the reduction of the body to the head of the associated production

Parser-Stack Implementation of Postfix SDT s The attribute(s) of each grammar symbol can be put on the stack along with the symbol T T 1 * F T * F T.val F.val When reduction stack[top-2].val = stack[top].val * stack[top-2].val; top = top 2; T T.val

Another SDD to SDT Example SDD of while to generate 3-address code S while ( C ) S while(i<a) i=i*2; j=j*i; label L5: if i<a goto L6 goto L7 label L6: i=i*2 goto L5 label L7: j=j*i S while ( C ) S L1=new(); L2=new(); S1.next=L1; C.true=L2; C.false=S.next; S.code= label L1 C.code label L2 S1.code

Another SDD to SDT Example For synthesized attribute computations End of the production body For computing inherited attributes of a nonterminal Immediately before the nonterminal occurrence S while ( { L1=new(); L2=new(); C.true=L2; C.false=S.next; } C ) S1.next=L1; S1 { S.code= label L1 C.code label L2 S1.code; }

SDT s With Actions Inside Productions L-attributed SDD s SDT s with actions inside productions When remove left-recursions in an S- attributed SDD for LL parsing SDT s with actions inside production

Define A Language and Syntax- Directed Translation (1/23) expr expr + term expr - term term term 0 1 9 Syntax-directed translation based on semantic actions expr expr + term { print( + ) } expr - term { print( - ) } term term 0 { print( 0 ) } 1 { print( 1 ) } 9 { print( 9 ) }

SDT s With Actions Inside Productions A A α β A βr R αr ε Left recursion removal for top-down parsing expr term rest rest + term { print( + ) } rest - term { print( - ) } rest ε term 0 { print( 0 ) } 1 { print( 1 ) } 9 { print( 9 ) }

SDT s With Actions Inside Productions Any SDT can be implemented as follows Ignore the actions, parse the input and produce a parse tree Add actions into the parser tree corresponding to the SDT Perform a preorder traversal of the tree

SDT s With Actions Inside Productions Use a recursive-descent parser with one function for each nonterminal Generate code on the fly using a recursivedescent parser Implement an SDT in conjunction with an LLparser Implement an SDT in conjunction with an LRparser (in April 19 th s lecture)

Recursive-descent Parser: One Function For Each Nonterminal Function arguments: inherited attributes Function return: synthesized attributes The while example

Generate Code on the Fly Using a Recursive-descent Parser Incrementally generate pieces of the code into an array or output file Avoid copying or moving long strings The while example

Implement An SDT in Conjunction with An LL-parse If L-attributed SDD is based on an LLgrammar Extend the parser stack to hold actions and items for attribute evaluations

Non-recursive Predictive Parsing (2/27) A stack storing symbols, initialized with $S An input buffer with an input pointer ip A parsing table M for grammar G Stack Point ip to the 1 st input symbol Set A to the top stack symbol while(a $) { if (A is a) pop stack; advance ip else if (A is a terminal) error(); } else if (M[A,a] is an error entry) error(); else if (M[A,a] = A X 1 X 2 X k ) { output the production or other actions; pop the stack; push X k,,x 2, X 1 onto the stack with X 1 on top; } X Y Z $ a + b $ Predictive Parsing Program Parsing Table M[A,a] Input Output Set A to the top stack symbol;

Implement An SDT in Conjunction with An LL-parse If L-attributed SDD is based on an LLgrammar Extend the parser stack to hold actions and items for attribute evaluations Action record for inherited attribute computation What should be in the record? Placed above or below the nonterminal? The SDD for while to generate code on the Fly

Implement An SDT in Conjunction with An LL-parse If L-attributed SDD is based on an LLgrammar Extend the parser stack to hold actions and items for attribute evaluations Action record for inherited attribute computation Synthesize record for synthesized attribute computation Placed above or below the nonterminal?

Outline Recap Syntax-directed translation Summary and homework

Final Exam Reminder THURSDAY, MAY 03, 2007, 08:00-11:00AM

Homework (Due on 04/02) 10.1. (a) Exercise 5.2.4 (page 317); (b) Exercise 5.2.5 (Page 317).