Announcement: Midterm Prep

Similar documents
CS415 Compilers Syntax Analysis Top-down Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

CS 314 Principles of Programming Languages

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

CSE302: Compiler Design

Syntax Analysis Part III

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Creating a Recursive Descent Parse Table

Syntax Analysis Part I

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

Computer Science 160 Translation of Programming Languages

Syntactic Analysis. Top-Down Parsing

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Compiler Principles, PS4

Computing if a token can follow

Compiling Techniques

Compiling Techniques

CS153: Compilers Lecture 5: LL Parsing

Follow sets. LL(1) Parsing Table

INF5110 Compiler Construction

Compiling Techniques

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Parsing -3. A View During TD Parsing

Compiler Construction Lent Term 2015 Lectures (of 16)

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

LL(1) Grammar and parser

CONTEXT FREE GRAMMAR AND

h>p://lara.epfl.ch Compiler Construc/on 2011 CYK Algorithm and Chomsky Normal Form

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Principles, PS7

CYK Algorithm for Parsing General Context-Free Grammars

Syntax Analysis Part I

Syntax Directed Transla1on

INF5110 Compiler Construction

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS415 Compilers Syntax Analysis Bottom-up Parsing

Compiler Construction Lectures 13 16

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

THEORY OF COMPILATION

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

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

Syntax Analysis - Part 1. Syntax Analysis

Course Script INF 5110: Compiler construction

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Compiling Techniques

Context-Free Grammars (and Languages) Lecture 7

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Syntax Analysis, VI Examples from LR Parsing. Comp 412

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Compila(on* **0368/3133*(Semester*A,*2013/14)*

Exercises. Exercise: Grammar Rewriting

Pushdown Automata (Pre Lecture)

Context free languages

CA Compiler Construction

Intro to Theory of Computation

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

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

MA/CSSE 474 Theory of Computation

CSE 105 THEORY OF COMPUTATION

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

Top-Down Parsing, Part II

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Compiler Design Spring 2017

Lecture 11 Context-Free Languages

2.4 Parsing. Computer Science 332. Compiler Construction. Chapter 2: A Simple One-Pass Compiler : Parsing. Top-Down Parsing

CPS 220 Theory of Computation Pushdown Automata (PDA)

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

CS453 Intro and PA1 1

Compiler construction

Lecture Notes on Inductive Definitions

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Introduction to ANTLR (ANother Tool for Language Recognition)

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

Context- Free Parsing with CKY. October 16, 2014

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

Pushdown Automata: Introduction (2)

Theory of Computation - Module 3

PUSHDOWN AUTOMATA (PDA)

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

CPS 220 Theory of Computation

CISC4090: Theory of Computation

Compiling Techniques

Computability Theory

The Parser. CISC 5920: Compiler Construction Chapter 3 Syntactic Analysis (I) Grammars (cont d) Grammars

CS 406: Bottom-Up Parsing

Foundations of Informatics: a Bridging Course

Lecture Notes on Inductive Definitions

From EBNF to PEG. Roman R. Redziejowski. Concurrency, Specification and Programming Berlin 2012

Introduction to Theory of Computing

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Transcription:

Announcement: Midterm Prep Midterm is Tuesday, 3/13 at 11 in three classrooms - Last name A-C Van Vleck B115 - Last name D-J Van Vleck B135 - Last name K-Z CS1240 List of topics Up to and including lecture on Tuesday s the 6th Length 1hr 10min No extra material allowed just bring a pen Sample midterm available online Recommended that you do this by Thursday We ll review it in class 1

Syntax Directed Transla2on for Top-Down Parsing 2

Last Time: Built LL(1) Predic[ve Parser FIRST and FOLLOW sets define the parse table If the grammar is LL(1), the table is unambiguous If the grammar is not LL(1) we can a`empt a transforma[on sequence: 1. Remove lec recursion 2. Lec-factoring 3

Today Review Parse Table Construc[on 2 examples Show how to do Syntax-Directed Transla[on using an LL(1) parser 4

FIRST(α) for α = Y 1 Y 2 Y k Add FIRST(Y 1 ) - {ε} If ε is in FIRST(Y 1 to i-1 ): add FIRST(Y i ) {ε} If ε is in all RHS symbols, add ε FOLLOW(A) for X α A β If A is the start, add eof Add FIRST(β) {ε} Add FOLLOW(X) if ε in FIRST(β) or β empty Table[X][t] for each production X α for each terminal t in FIRST(α) put α in Table[X][t] if ε is in FIRST(α){ for each terminal t in FOLLOW(X){ put α in Table[X][t] S B D Not LL(1) B c D B a b c S d ε FIRST (S) = { a, c, d } FIRST (B) = { a, c } FIRST (D) = { d, ε } FIRST (B c) = { a, c } FIRST (D B) = { d, a, c } FIRST (a b) = { a } FIRST (c S) = { c } FIRST (d) = { d } FIRST (ε) = { ε } FOLLOW (S) = { eof, c } FOLLOW (B) = { c, eof } FOLLOW (D) = { a, c } S B D a b c d eof B c B c D B D B D B a b ε c S ε d 5

FIRST(α) for α = Y 1 Y 2 Y k Add FIRST(Y 1 ) - {ε} If ε is in FIRST(Y 1 to i-1 ): add FIRST(Y i ) {ε} If ε is in all RHS symbols, add ε FOLLOW(A) for X α A β If A is the start, add eof Add FIRST(β) {ε} Add FOLLOW(X) if ε in FIRST(β) or β empty Table[X][t] for each production X α for each terminal t in FIRST(α) put α in Table[X][t] if ε is in FIRST(α){ for each terminal t in FOLLOW(X){ put α in Table[X][t] S ( S ) { S } ε FIRST (S) = { {, (, ε } FIRST ( ( S ) ) = { ( } FIRST ( { S } ) = { { } FIRST ( ε ) = { ε } FOLLOW ( S ) = { eof, ), } } S ( ) { } eof ( S ) ε { S } ε ε 6

FIRST(α) for α = Y 1 Y 2 Y k Add FIRST(Y 1 ) - {ε} If ε is in FIRST(Y 1 to i-1 ): add FIRST(Y i ) {ε} If ε is in all RHS symbols, add ε FOLLOW(A) for X α A β If A is the start, add eof Add FIRST(β) {ε} Add FOLLOW(X) if ε in FIRST(β) or β empty Table[X][t] for each production X α for each terminal t in FIRST(α) put α in Table[X][t] if ε is in FIRST(α){ for each terminal t in FOLLOW(X){ put α in Table[X][t] S + S ε FIRST (S) = { +, ε } FIRST ( + S ) = { + } FIRST ( ε ) = { ε } FOLLOW ( S ) { eof } = S + eof + S ε 7

How s that Compiler Looking? Scanner Tokens via RegEx table Parser Parse Tree via Recursive Descent Seman2c Anlaysis IR Codegen TODO: SDT on transformed Op2mizer MC Codegen 8

Implemen[ng SDT for LL(1) Parser So far, SDT shown as second (bo`om-up) pass over parse tree The LL(1) parser never needed to explicitly build the parse tree (implicitly tracked via stack) Naïve approach: build the parse tree explicitly 9

Seman[c Stack Instead of building the parse tree, give parser second, seman&c stack Holds nonterminals transla[ons SDT rules converted to ac[ons on seman[c stack Pop transla[ons of RHS nonterms off Push computed transla[on of LHS nonterm on SDT Rules SDT AcJons Expr ε ( Expr ) [ Expr ] Expr.trans = 0 Expr.trans = Expr 2.trans + 1 Expr.trans = Expr 2.trans push 0 Expr 2.trans = pop; push Expr 2.trans + 1 Expr 2.trans = pop; push Expr 2.trans

Ac[on Numbers Need to define when to fire the SDT Ac[on Not immediately obvious since SDT is bo`om-up Solu[on Number ac[ons and put them on the symbol stack! Add ac[on number symbols at end of the produc[ons Expr ε #1 ( Expr ) [ Expr ] #3 #1 #3 SDT AcJons push 0 Expr 2.trans = pop; push Expr 2.trans + 1 Expr 2.trans = pop; push Expr 2.trans

Ac[on Numbers: Example 1 Expr ε #1 ( Expr ) [ Expr ] #3 #1 #3 SDT AcJons: CounJng Max Parens Depth push 0 Expr 2.trans = pop; push(expr 2.trans + 1) Expr 2.trans = pop; push(expr 2.trans) ( [ ] ) EOF [ Expr ( Expr ) [ Expr ] #3 ε #1 ε #1 ε #1 Expr #1 ]( Expr #3 ) Expr Root transla2on ( [ ] ) eof eof Work Stack 01 current current current current current Seman2cStack 12

No-op SDT Ac[ons Expr ε #1 ( Expr ) [ Expr ] #3 #1 #3 SDT AcJons: CounJng Max Parens Depth push 0 Expr 2.trans = pop; push(expr 2.trans + 1) Expr 2.trans = pop; push(expr 2.trans) Useless rule Expr ε #1 ( Expr ) [ Expr ] #1 SDT AcJons: CounJng Max Parens Depth push 0 Expr 2.trans = pop; push(expr 2.trans + 1) 13

Placing Ac[on Numbers Ac[on numbers go acer their corresponding nonterminals, before their corresponding terminal Transla[ons popped right to lec in ac[on Expr Expr + Term #1 Term Term Term * Factor Factor Factor #3 intlit SDT AcJons #1 ttrans = pop ; etrans = pop ; push(ttrans + etrans) ftrans = pop; ttrans = pop ; push(ftrans * ttrans) #3 push(intlit.value) 14

Placing Ac[on Numbers: Example Write SDT Ac[ons and place ac[on numbers to get the product of a ValList (i.e. mul[ply all elements) List Val List #1 List Val List ε #3 Val #4 intlit SDT AcJons #1 LTrans = pop ; vtrans = pop ; push(ltrans * vtrans) LTrans = pop; vtrans = pop ; push(ltrans * vtrans) #3 push(1) #4 push(intlit.value) 15

Ac[on Numbers: Benefits Plans SDT ac[ons using the work stack Robust to previously introduced grammar transforma[ons Expr Expr + Term #1 Term Term Term * Factor Factor Factor #3 intlit Expr Term Expr Expr + Term #1 Expr ε Term Factor Term Term * Factor Term ε Factor #3 intlit #1 #3 SDT AcJons ttrans = pop ; etrans = pop ; push(ttrans + etrans) ftrans = pop; ttrans = pop ; push(ftrans * ttrans) push(intlit.value) 16

Example: SDT on Transformed Grammar Expr Term Expr Expr + Term #1 Expr ε Term Factor Term Term * Factor Term ε Factor #3 intlit #1 #3 SDT AcJons ttrans = pop ; etrans = pop ; push(ttrans + etrans) ftrans = pop; ttrans = pop ; push(ftrans * ttrans) push(intlit.value) On board: 5 + 3 * 2 17

What about ASTs? Push and pop AST nodes on the stack Keep field references to nodes that we pop Expr Expr + Term #1 Term Term intlit #1 EvaluaJon SDT AcJons ttrans = pop ; etrans = pop ; push(etrans + ttrans) push(intlit.value) Transformed Expr Term Expr Expr + Term #1 Expr ε Term intlit #1 AST SDT AcJons ttrans = pop ; etrans = pop ; push(new PlusNode(tTrans, etrans)) push(new IntLitNode(intlit.value))

AST Example intlit + EOF Transformed AST SDT AcJons E T E #1 ttrans = pop ; E T E etrans = pop ; E + T #1 E E push(new PlusNode(tTrans, etrans)) ε T intlit push(new IntLitNode(intlit.value)) T intlit + T #1 E ε 1 + 2 + 3 eof PlusNode + current current current current current intlit T intlit #1 T E eof Work Stack Seman2c Stack PlusNode IntLitNode Value: 1 IntLitNode Value: 2 IntLitNode Value: 3 19

We now have an AST At this point, we have completed the frontend for (a) compiler Only recognize LL(1) LL(1) is not a great class of languages if (e1) stmt1 if (e2) stmt2 else stmt3 Grammar Snippet IfStmt -> if lparens Exp rparens Stmts if lparens Exp rparens Stmts else Stmts 20