USING AMBIGUOUS GRAMMARS

Slides:



Advertisements
Similar presentations
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Advertisements

 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
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.
Mooly Sagiv and Roman Manevich School of Computer Science
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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)
Amirkabir University of Technology Computer Engineering Faculty AILAB Efficient Parsing Ahmad Abdollahzadeh Barfouroush Aban 1381 Natural Language Processing.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
Lecture #8, Feb. 7, 2007 Shift-reduce 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.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
LALR Parsing Canonical sets of LR(1) items
Syntax and Semantics Structure of programming languages.
1 Natural Language Processing Lecture 11 Efficient Parsing Reading: James Allen NLU (Chapter 6)
CS 321 Programming Languages and Compilers Bottom Up Parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Syntax and Semantics Structure of programming languages.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
CSE 3302 Programming Languages
SLR Parser Problems.
Parsing & Context-Free Grammars
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Unit-3 Bottom-Up-Parsing.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Table-driven parsing Parsing performed by a finite state machine.
Chapter 4 Syntax Analysis.
Syntax Analysis Chapter 4.
Compiler design Bottom-up parsing: Canonical LR and LALR
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
CSE 3302 Programming Languages
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Ambiguity in Grammar, Error Recovery
Compiler Construction
Chapter 4. Syntax Analysis (2)
Bottom-Up Parsing “Shift-Reduce” Parsing
Kanat Bolazar February 16, 2010
Compiler Construction
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up LR Table Construction.
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
Compiler design Bottom-up parsing: Canonical LR and LALR
Faculty of Computer Science and Information System
Presentation transcript:

USING AMBIGUOUS GRAMMARS Every ambiguous grammar fails to be LR

Uses of Ambiguous Grammars For language constructs like expressions an ambiguous grammar provides a shorter, more natural specification than equivalent unambiguous grammar. We can isolate commonly occurring syntactic constructs for special case optimization. We can specify the special case constructs by carefully adding new productions to the grammar.

Note We should emphasize that although the grammars we use are ambiguous, in all cases we specify disambiguating rules that allow only one parse tree for each sentence. In this way, the overall language specification still remains unambiguous.

Using Precedence and Associativity to Resolve Parsing Action Conflicts The following grammar for arithmetic expressions with operators + and * E -> E + E | E * E | (E) | id --(1) is ambiguous because it does not specify the associativity or precedence of the operators + and *. The unambiguous grammar E -> E + T | T T -> T * F | F F -> (E) | id --(2) generates the same language, but gives + a lower precedence than *, and makes both operators left associative.

Why we prefer grammar (1) over grammar (2) We can easily change the associativities and precedence levels of the operators + and * without disturbing the productions of (1) or the number of states in the resulting parser. The parser for (2) will spend a substantial fraction of its time reducing the productions E -> T and T -> F, whose sole function is to enforce associativity and precedence. The parser for (1) will not waste time reducing by these single productions, as they are called.

So, Build LR parsing table with S/R conflicts, and then Inspect each conflict individually and decide as to the precedence rule. E’ -> E E -> E + E E -> E * E E -> (E) E -> id

Items I0 : E’  .E , E  .E + E , E  .E * E , E  .( E ) , E  .id

I3 : E  id. I4 : E  E + .E , E  .E + E , E  .E * E , E  .( E ) , E  .id I5 : E  E * .E , E  .E + E , E  .E * E E  .( E ) , E  .id

I6 : E  ( E. ) , E  E. + E , E  E . * E I7 : E  E + E. , E  E. + E , E  E. * E I8 : E  E * E. , E  E. + E , E  E.* E I9 : E  ( E ).

First(E’) – { ( , id } First(E) – { ( , id } Follow(E’) – { $ } Follow(E) – { + , * , ) , $ }

Ambiguous parsing table for grammar (1) State Id + * ( ) $ goto 0 S3 S2 1 1 S4 S5 Acc 2 S3 S2 6 3 R4 R4 R4 R4 4 S3 S2 8 5 S3 S2 8 6 S4 S5 s9 7 S4/R1 S5/R1 R1 R1 8 S4/R2 S5/R2 R2 R2 9 R3 R3 R3 R3

Since the grammar is ambiguous parsing actions conflict will occur Since the grammar is ambiguous parsing actions conflict will occur. The states corresponding to items I7 and I8 will generate these conflicts. These conflicts can be resolved using the precedence and associativity information for + and *. Consider the input id + id * id which causes a parser based upon above states to enter state 7 after processing id + id; in particular parser reaches configuration

Stack Input 0 E 1 + 4 E 7 * id $ Assuming that * takes precedence over +, we know that the parser should shift * on to the stack, preparing to reduce the * and its surrounding id’s to an expression. On the other hand, if + takes the precedence over *, we know that parser should reduce E + E to E. Thus the relative precedence of + followed by * uniquely determines how the parsing conflict between reducing E  E + E and shifting on * in state 7 should be resolved.

Similarly if the input has been id + id +id the parser would still reach the configuration in which it had stack 0 E 1 + 4 E 7 after processing input id + id . On input + there is again shift/reduce conflict in state 7. Now, however, the associativity of the + operator determines how this conflict should be resolved. If + is left associative, the correct action should to reduce by E  E + E i.e., the id’s surrounding the first + must be grouped first.

So, we can choose from the conflict which rule should be given preference according to our requirements. Proceeding in this way we get the unambiguous parsing table from the ambiguous parsing table as follows

Unambiguous parsing table for grammar (1) State Id + * ( ) $ goto 0 S3 S2 1 1 S4 S5 Acc 2 S3 S2 6 3 R4 R4 R4 R4 4 S3 S2 8 5 S3 S2 8 6 S4 S5 s9 7 R1 S5 R1 R1 8 R2 R2 R2 R2 9 R3 R3 R3 R3

Thank You!