Creating a Recursive Descent Parse Table

Similar documents
CSE302: Compiler Design

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

Exercises. Exercise: Grammar Rewriting

Introduction to Bottom-Up Parsing

CS415 Compilers Syntax Analysis Top-down Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Computer Science 160 Translation of Programming Languages

Syntax Analysis Part III

Syntactic Analysis. Top-Down Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing

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

Syntax Analysis Part I

CS 314 Principles of Programming Languages

LL(1) Grammar and parser

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

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

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

Compiler Principles, PS4

CA Compiler Construction

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

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.

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

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

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Syntax Analysis Part I

Context free languages

Compiler Design Spring 2017

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

INF5110 Compiler Construction

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Syntax Analysis - Part 1. Syntax Analysis

Syntax Analysis (Part 2)

Parsing -3. A View During TD Parsing

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

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

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

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

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

CS Rewriting System - grammars, fa, and PDA

Compiler Principles, PS7

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

Announcement: Midterm Prep

Follow sets. LL(1) Parsing Table

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

Compiler Construction Lectures 13 16

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

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

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

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

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

INF5110 Compiler Construction

Compiling Techniques

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

CS153: Compilers Lecture 5: LL Parsing

Conflict Removal. Less Than, Equals ( <= ) Conflict

CS 406: Bottom-Up Parsing

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Pushdown Automata: Introduction (2)

INF5110 Compiler Construction

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiling Techniques

Context-free Grammars and Languages

Bottom-Up Syntax Analysis

Compiling Techniques

Parsing VI LR(1) Parsers

Top-Down Parsing, Part II

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Computing if a token can follow

Computer Science 160 Translation of Programming Languages

CONTEXT FREE GRAMMAR AND

Compiler construction

Syntax Directed Transla1on

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

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

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia

CS20a: summary (Oct 24, 2002)

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.

Course Script INF 5110: Compiler construction

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

CISC4090: Theory of Computation

Parsing. Left-Corner Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 17

THEORY OF COMPILATION

MA/CSSE 474 Theory of Computation

The Pumping Lemma. for all n 0, u 1 v n u 2 L (i.e. u 1 u 2 L, u 1 vu 2 L [but we knew that anyway], u 1 vvu 2 L, u 1 vvvu 2 L, etc.

Einführung in die Computerlinguistik

Context-Free Grammars and Languages. Reading: Chapter 5

5 Context-Free Languages

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

MA/CSSE 474 Theory of Computation

CPS 220 Theory of Computation Pushdown Automata (PDA)

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

Everything You Always Wanted to Know About Parsing

Parsing. Unger s Parser. Laura Kallmeyer. Winter 2016/17. Heinrich-Heine-Universität Düsseldorf 1 / 21

Transcription:

Creating a Recursive Descent Parse Table Recursive descent parsing is sometimes called LL parsing (Left to right examination of input, Left derivation) Consider the following grammar E TE' E' +TE' T FT' T' *FT' F ( E ) a Compute the First/Follow sets. Rules for forming first: 1. If X is a terminal, then First(X) = {X} 2. First(ε) = {ε} 3. If X ε, then add ε to First(X); 4. If X is a nonterminal and X Y 1 Y 2 Y 2 Y n then place a in First(X) if a is in the first Y i and all earlier Y's have ε in their first sets. If all Y i contain ε in their first sets, then add ε to First(X). Rules for forming follow: 1. place $ in the follow(s) where S is the start symbol and $ is the righthand marker. 2. If there is a production A αbβ then everything in First (β) except for ε is placed in Follow(B). 3. If there is a production A αb or a production A αbβ where FIRST(β) contains ε then everything in Follow(A) is in Follow(B). (Note, α refers to anythng that precedes B on the right hand side of the production and β refers to anything that follows B on the right hand side of the production.) With these rules, cover the answers (below) and compute the First/Follow sets for the above grammar. First E = { a ( } First E' = {+ } First T = { a ( } First T' = {* } First F = { a ( } Follow E = { ) $} Follow E' = { ) $} Follow T = {+ ) $ } Follow T' = {+ ) $ } Follow F = {+ * ) $ }

What do you notice about the grammar? E TE' E' +TE' T FT' T' *FT' F ( E ) a It does the same thing as our favorite expression grammar, E E + T T T T * F F F (E) a but has been massaged so it works better with a recursive descent parser. Notice that the prime versions of the non-terminals (E and T ) have been named to show that they were derived from the original non-terminals. Javacc (which we will experiment with) creates a recursive descent parser, and thus its grammar will need to be massaged. Study the rules for creating the parsing table to see why the original grammar had to be massaged. Constructing a predictive parsing table. Create a table M with rows labeled with nonterminals and columns labeled as terminals plus $ (for no more input). The rows represent the current non terminal you are trying to determine a production for. The columns represent the current input symbol. 1. For each production A α of the grammar (where α is anything on the right hand side of the production), do steps 2 and 3. Note that if α= Y 0 Y 1 Y 2 Y n then First(α) contains the terminals in First(Y 0 ). If First(Y 0 ) contains, then First(α) contains the terminals of First(Y 1 ), and so on. First(α) contains only if all Y i contain in their first sets. 2. For each terminal t in First(α), add A α to M[A,t] 3. If ε is in First(α), add A α to M[A,b] for each terminal b in Follow(A). 4. If ε is in First(α) and $ is in Follow(A), add add A α to M[A,$]. 5. Consider each undefined entry of M to be an ERROR. Using these rules, create a recursive descent table. Cover the table below and then compare your answers. Using the table, show how you could parse (a*a*a) +a$ with only one character look ahead.

Bottom Up Parsing The correct RHS in a given right sentential form to rewrite to get the previous right sentential form, is called a handle. Consider the following grammar: E E + T T T T * F F F (E) a Show the rightmost derivation for a+a*a E E + T E + T*F E + T*a E + F*a E + a*a T + a*a F + a*a a + a*a Now, start at the last step of the rightmost derivation and go backwards. This is what we are trying to do with a bottom up parser. We need to first determine that the a becomes an F, then that the F becomes a T, etc. In order to do bottom up parsing, we will rewrite the grammar only slightly so we can distinguish between the two productions shown on the same line. And we will give the productions numbers so we can refer to them 1. E E + T 2. E T 3. T T * F 4. T F 5. F (E) 6. F a Your text contains the parsing table we will use. The parsing table is generated by a tool like YACC, and the generation is beyond the scope of this course. However, we need to be able to use the parsing table. Take a look at figure 4.3 in which you see a parse stack, the input, and the parsing table. This models what we do. We want to read the input one symbol at a time and make a decision as to what to do, given a history of what we have done in the past. We have a parse stack to help us remember these important details. As we look at an input, there are only two choices of actions we take: 1. We read the input and shift it over to our stack along with a state. Thus, the action S4 means to shift the current symbol to the stack and then push the state 4 to the stack.

2. We look at the input and decide not to delete it from the input. Instead, we look at the top symbols of our stack and remove some and place others on. This step is called a reduce step. The number of the reduce does NOT refer to a state. It refers to the production which you use. For R5 (for example), the steps are as follows: a. Look at production 5 F (E). In our case, there are three symbols on the right hand side. This means for us to remove three sets of symbols from our stack. b. You should see an (, an E, and an ) (along with their states) on the top of the stack. Remove these. c. They want to become an F (going backwards on the production). d. Before you place the F on the stack, look at the exposed state on the stack. Use that exposed state and F to determine the new state (using the Goto part of the table). e. That new state is then placed after F on the stack.