LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
Mooly Sagiv and Roman Manevich School of Computer Science
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
Syntax and Semantics Structure of programming languages.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Syntax and Semantics Structure of programming languages.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Compilation /15a Lecture 5 1 * Syntax Analysis: Bottom-Up parsing Noam Rinetzky.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
LR Parser: LR parsing is a bottom up syntax analysis technique that can be applied to a large class of context free grammars. L is for left –to –right.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
Operator precedence parser Lecturer: Noor Dhia
Syntax and Semantics Structure of programming languages.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Announcements/Reading
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Chapter 4 Syntax Analysis.
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Lecture 4: Syntax Analysis: Botom-Up Parsing Noam Rinetzky
Lecture (From slides by G. Necula & R. Bodik)
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Compiler Construction
Compiler Construction
USING AMBIGUOUS GRAMMARS
Chapter 4. Syntax Analysis (2)
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Lecture 11 LR Parse Table Construction
Presentation transcript:

LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005

The LR(0) Parsing Tables There are two tables that we will construct The action table Contains shift and reduce actions to be taken upon processing terminals. The goto table Contains changes of state upon matching productions.

The Action Table The action table contains one row for each state in the PDA and one column for each terminal and EOF ($). The entries are Shift n Push the current token and move to state n. Reduce n Pop the symbols of the right side of production n and then push the nonterminal of the left side. Then change state according to the goto table.

Building the Action Table The action table is built from items of the form [A    a] and [A    ]. Items [A    a] produce shift operations. Items [A    ] produce reduce operations. The goto table is built from items of the form [A    B].

Building the Action Table If [A    a] is in state Ii and the PDA transition on a is from state Ii to state Ij, then set the (i, a) entry to “shift j.” In the table, we write “sj,” where j is the number of the destination state. Thus, every transition on a terminal becomes a shift entry in the action table. Ii Ij a

Building the Action Table If [A   ] is in state Ii and A  S', then set the (i, a) entry to “reduce A  ” for all a in FOLLOW(A). In the table, we write “rk,” where k is the number of the production A  . If [S'  S ] is in state Ii, then the (i, $) entry is “accept.” In the table, we write “acc”.

Building the Action Table Thus, each item with the  at the end becomes a reduce entry in the action table. All empty cells are labeled “error.”

Shift/Reduce Conflicts It is possible that a cell will contain both a shift operation and a reduce operation. This is called a shift/reduce conflict. To choose between “shift” and “reduce,” each case must be considered on its own merit. Consider the case of E  E + E | E * E and the inputs a + b * c and a * b + c.

Reduce/Reduce Conflicts It is possible that a cell will contain two different reduce operations. This is called a reduce/reduce conflict. This occurs when a sequence of tokens matches the right-hand sides of two different productions at the same time. For each such conflict in the table, we must choose which reduction to apply.

Example: FIRST and FOLLOW To find the action table for our example, we need to know nullability, FIRST, and FOLLOW. E'  E E  E + T | T T  T * F | F F  (E) | id | num

Example: FIRST and FOLLOW Nullable FIRST FOLLOW E' No {(, id, num} {$} E {$, +, )} T {$, +, ), *} F

Example: The Action Table Then number the productions, starting with 0 for the special production: 0. E'  E 1. E  E + T 2. E  T 3. T  T * F 4. T  F 5. F  (E) 6. F  id 7. F  num

Example: The Action Table + * ( ) id num $ s4 s5 s6 1 s7 acc 2 r2 s8 3 r4 4 5 r6 6 r7 7 8 9 s12 10 r1 11 r3 12 r5

The Goto Table The goto table has one row for each state in the PDA and one column for each nonterminal except S'. The entries are states of the PDA.

Building the Goto Table If [A    B] is in state Ii and the PDA transition is Ii Ij B then set the (i, B) entry to “goto j.” In the goto table, we write “j.” Thus, every transition on a nonterminal becomes an entry in the goto table.

Example: The Goto Table F 1 2 3 4 9 5 6 7 10 8 11 12

Example: LR Parsing Parse (id + num)*id. ( id + num ) * id $ 0 ( 4 ( id + num ) * id $ 0 ( 4 id + num ) * id $ 0 ( 4 id 5 + num ) * id $ 0 ( 4 F 3 0 ( 4 T 2 0 ( 4 E 9 0 ( 4 E 9 + 7 num ) * id $ 0 ( 4 E 9 + 7 num 6 ) * id $ 0 ( 4 E 9 + 7 F 3 0 ( 4 E 9 + 7 T 10

Example: LR Parsing 0 ( 4 E 9 ) 12 * id $ 0 F 3 0 T 2 0 T 2 * 8 id $ 0 T 2 * 8 F 11 0 E 1 Accept

Example: A Simplified Grammar We may simplify our grammar to E  E + E E  E * E E  (E) E  id E  num In this form, the precedence rules for + and * are not implicit. They must be incorporated into the tables.

The Action and Goto Tables + * ( ) id num $ E s2 s3 s4 1 s5 s6 acc 2 7 3 r4 4 r5 5 8 6 9 s10 s5/r1 s6/r1 r1 s5/r2 s6/r2 r2 10 r3

Shift/Reduce Conflicts and Associativity The shift/reduce conflict in cell (8, +) is between shifting a + and reducing by E  E + E If we choose “shift,” then we will make addition right associative. If we choose “reduce,” then we will make addition left associative. The case is similar in cell (9, *) regarding multiplication.

Shift/Reduce Conflicts and Precedence The shift/reduce conflict in cell (9, +) is between shifting a + and reducing by E  E * E If we choose “shift,” then we will give multiplication a higher precedence than addition. If we choose “reduce,” then we will give addition a higher precedence than multiplication. The case is similar in cell (8, *).

The Action and Goto Tables + * ( ) id num $ E s2 s3 s4 1 s5 s6 acc 2 7 3 r4 4 r5 5 8 6 9 s10 r1 r2 10 r3

Exercise The grammar R  R  R | RR | R* | (R) | a | b generates all regular expressions on the alphabet {a, b}. Determine nullability, FIRST, and FOLLOW for this grammar. Find the items Ii. Build the action and goto tables. Parse the expression ab*(a | ).