2/20/2008Prof. Hilfinger CS164 Lecture 121 Earley’s Algorithm: General Context-Free Parsing Lecture 12 P. N. Hilfinger.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
CS5371 Theory of Computation
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Parsing III (Eliminating left recursion, recursive descent parsing)
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
COP4020 Programming Languages
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
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.
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
Bottom-up parsing Goal of parser : build a derivation
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Top-Down Parsing - recursive descent - predictive parsing
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
Introduction to Parsing
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Lecture 3: Parsing CS 540 George Mason University.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Top-Down Parsing.
4. Bottom-up Parsing Chih-Hung Wang
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
COMPILER CONSTRUCTION
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.
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 404 Introduction to Compiler Design
Fall Compiler Principles Lecture 4: Parsing part 3
Implementing a LR parser engine
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax-Directed Translation
Lecture (From slides by G. Necula & R. Bodik)
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Ambiguity, Precedence, Associativity & Top-Down Parsing
LR Parsing. Parser Generators.
Kanat Bolazar February 16, 2010
Compiler Construction
Presentation transcript:

2/20/2008Prof. Hilfinger CS164 Lecture 121 Earley’s Algorithm: General Context-Free Parsing Lecture 12 P. N. Hilfinger

2/20/2008Prof. Hilfinger CS164 Lecture 122 Parsing General Context-Free Grammars Shift-reduce parsing can work for most practical applications. However, one must sometimes munge the grammar, though not as much as LL(1). Cannot handle ambiguity, nor situations where resolving ambiguities requires looking far ahead. Today, we’ll look at a method that can: Earley’s Algorithm. In fact, shift-reduce parsing is a highly optimized special case of this algorithm.

2/20/2008Prof. Hilfinger CS164 Lecture 123 Earley’s Algorithm: Basic Idea Scan tokens left-to-right. At each point, keep track of all possible subtrees that could include the current point in the input, based on everthing seen so far. At the end of the input, if there is a tree that is rooted at the start symbol, we’ve found a parse (possibly many).

2/20/2008Prof. Hilfinger CS164 Lecture 124 Some Notation If input is s=s 1 s 2 …s n then “position k’’ in the input is just after s k and before s k+1, with position 0 at the beginning and position n at the end. At each input position, k, compute a set of items, where each item has the form A    , m where A    is a production and 0≤m≤k. Together, the items in the set describe all subtrees of possible parse trees that begin or end at position k or have a child that does.

2/20/2008Prof. Hilfinger CS164 Lecture 125 Meaning of an Item An item A    , m at position k means: 1.The input between positions m and k matches . 2.Depending on what s k+1 …s n is, there might be a subtree formed from production A    in the (or a) parse tree for the entire string. 3.So when  is empty, means that there is a possible handle for A   that ends at k. So that leaves the problem of figuring out what items to put in each set.

2/20/2008Prof. Hilfinger CS164 Lecture 126 Example Grammar: E  E + T E  T T  T * int T  int Input: 0 int int 3 * 4 int 5 At position 0, we expect to see an E to our right, formed from one of E’s productions. Plus, since an E can start with a T, we won’t be surprised by a T formed from one of its productions.

2/20/2008Prof. Hilfinger CS164 Lecture 127 Example: Getting Started E   T, 0 E   E + T, 0 int 0 1 and (since E can start with T), also add items for T + T   int, 0 T   T * int, 0 Start with items for start symbol E

2/20/2008Prof. Hilfinger CS164 Lecture 128 Closure Items Whenever we have an item B   A , j in item set m, it indicates that a substring producing A might start at this position. That’s what the item A   , m means, so we also add those items (for each production A   ) to item set m. These are called closure items. Other items are kernel items.

2/20/2008Prof. Hilfinger CS164 Lecture 129 Example: Computing next item set E   T, 0 E   E + T, 0 T   int, 0 T   T * int, 0 int 0 1 T  int , 0 T  T  * int, 0 E  T , 0 E  E  + T, 0 +

2/20/2008Prof. Hilfinger CS164 Lecture 1210 Computing next item set For each item of the form A   c , k in item set m, where c=s m+1 is the next input symbol, insert A  c  , k in item set m+1. For each complete item, A  , k in item set m+1, and each item B   A , j back in item set k, add item B  A  , j to item set m+1. (When creating a parse tree, the A in this new item will have have children , as denoted by dashed red arrows in our examples).

2/20/2008Prof. Hilfinger CS164 Lecture 1211 Continuing the Example, Set 2 T  int , 0 T  T  * int, 0 E  T , 0 E  E  + T, E  E +  T, 0 T   T * int, 2 T   int, 2 closure items int

2/20/2008Prof. Hilfinger CS164 Lecture 1212 Continuing the Example, Set 3 2 E  E +  T, 0 T   T * int, 2 T   int, 2 int T  int , 2 T  T  * int, 2 E  E + T , 0 3 * E  E  + T, 0 from item set 0

2/20/2008Prof. Hilfinger CS164 Lecture 1213 Continuing the Example, Sets 4 & 5 T  int , 2 T  T  * int, 2 E  E + T , 0 3 * E  E  + T, 0 T  T *  int, 2 4 T  T * int , 2 5 int T  T  * int, 2 E  E + T , 0 E  E  + T, 0 ACCEPT!

2/20/2008Prof. Hilfinger CS164 Lecture 1214 Accepting the String In the last item set, have a completed item for the start symbol that started in set 0. That means “the input between 0 and end matches an entire production for the start symbol,” so the string parses correctly.

2/20/2008Prof. Hilfinger CS164 Lecture 1215 Retrieving a Parse Tree or Derivation Start with a completed item in the last set that produces the whole input (has form S  …,0 for start symbol S). Follow the red arrows to find how to expand that symbol. Work backwards through the sets to find the expansions of the other nonterminals.

2/20/2008Prof. Hilfinger CS164 Lecture 1216 Getting a Tree from our Example (I) T  T * int , 2 5 int T  T  * int, 2 E  E + T , 0 E  E  + T, 0 start here E E + T T * int To find out how to expand this T, go back to chart 3 (before * int)

2/20/2008Prof. Hilfinger CS164 Lecture 1217 Getting a Tree from our Example (II) int T  int , 2 T  T  * int, 2 E  E + T , 0 3 E  E  + T, 0 E E + T T * int int To find out how to expand this E, go back to chart 1 (before +)

2/20/2008Prof. Hilfinger CS164 Lecture 1218 Figuring out Where to Look In the last slide, we had to figure out where to look for the derivation of the E in E + T We used the items T  T  * int, 2 and T  int , 2 to get the T in E + T, both of which tell us that the T started after item set #2. And since + is a terminal, we then have to go back one more.

2/20/2008Prof. Hilfinger CS164 Lecture 1219 Getting a Tree from our Example (III) E E T T * intint 1 T  int , 0 T  T  * int, 0 E  T , 0 E  E  + T, 0 start here T +int

2/20/2008Prof. Hilfinger CS164 Lecture 1220 An Ambiguous Grammar (I) Grammar: E  E + E E  E * E E  int Input: 0 int int 3 * 4 int 5 E   int, 0 E   E + E, 0 E   E * E, 0 E  int , 0 E  E  + E, 0 E  E  * E, 0 0 int 1

2/20/2008Prof. Hilfinger CS164 Lecture 1221 An Ambiguous Grammar (II) E  int , 0 E  E  + E, 0 E  E  * E, int 3 E  E +  E, 0 E   int, 2 E   E + E, 2 E   E * E, 2 E  int , 2 E  E  + E, 2 E  E  * E, 2 E  E + E , 0 E  E  + E, 0 E  E  * E, 0

2/20/2008Prof. Hilfinger CS164 Lecture 1222 An Ambiguous Grammar (III) 3 * 4 int 5 E  int , 2 E  E  + E, 2 E  E  * E, 2 E  E + E , 0 E  E  + E, 0 E  E  * E, 0 E  E *  E, 2 E  E *  E, 0 E   int, 4 E   E + E, 4 E   E * E, 4 E  int , 4 E  E * E , 2 E  E * E , 0 E  E  + E, 4 E  E  * E, 4 E  E + E , 0 There are two ways to produce the E starting at 0, reflecting ambiguity.

2/20/2008Prof. Hilfinger CS164 Lecture 1223 Just for Fun… E  E  E E Grammar is ferociously ambiguous: produces  an infinite number of ways! E  , 0 E   E E, 0 E  E  E, 0 E  E E , 0 ! ! ! 0

2/20/2008Prof. Hilfinger CS164 Lecture 1224 Relationship to LR Shift-Reduce Parsing With an LR(1) grammar, never have item sets where two items have the same production, with the dot in the same place, but different starting positions. So, ignoring the starting positions, there is a finite number of possible item sets. These are the states in the shift-reduce parser.