Section 4.8 Aggelos Kiayias Computer Science & Engineering Department

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Advertisements

CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
LR Parsing Table Costruction
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)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
1 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
ML-YACC David Walker COS 320. Outline Last Week –Introduction to Lexing, CFGs, and Parsing Today: –More parsing: automatic parser generation via ML-Yacc.
Compiler Construction LR Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Chapter 4 - Part 3: Bottom-Up Parsing
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CH4.1 CSE244 Bottom Up Translation (revisited) Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road,
Bottom Up 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.
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
LEX and YACC work as a team
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
–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.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
4. Bottom-up Parsing Chih-Hung Wang
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.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing SLR Parsing Muhammed Al-Mulhem March 1, 2009.
CH4.1 CSE244 Midterm Subjects Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
LR Parsing. LR Parsers The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. left to right right-mostk lookhead scanning derivation(k.
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.
Introduction to LR Parsing
Chapter 4 Syntax Analysis.
Compiler design Bottom-up parsing: Canonical LR and LALR
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Chapter 4. Syntax Analysis (2)
Bottom-Up Parsing “Shift-Reduce” Parsing
USING AMBIGUOUS GRAMMARS
Chapter 4. Syntax Analysis (2)
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Lecture 11 LR Parse Table Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Section 4.8 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs, CT 06269-1155 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

Ambiguous Grammars Ambiguous Grammars produce “conflicts” Shift/reduce, or reduce/reduce. Three “typical examples” The dangling else The “expression” grammar Eqn pre-processor of Troff

Dangling Else Ambiguity Recall Grammar Can be rewritten as: S  iSeS | iS | a We compute LR(0) items + Goto and the SLR Parsing table. stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement)

Canonical sets of LR(0) items I0 I3 S’  .S S  a. S  .iSeS S  .iS I4 S  .a S  iS.eS S  iS. I1 S’ S. I5 S  iSe.S I2 S  .iSeS S  i.SeS S  .iS S  i.S S  .a S  .iS I6 S  .a S  iSeS. Follow(S)= $,e

Parsing Table i e a $ S 0 s2 s3 1 1 acc 2 s2 s3 4 3 r3 r3 4 s5/r2 r2 1. S iSeS 2. S iS 3. S a i e a $ S 0 s2 s3 1 1 acc 2 s2 s3 4 3 r3 r3 4 s5/r2 r2 5 s2 s3 6 6 r1 r1 Resolve: choose s5 e.g. parse iiaea to understand the conflict resolution.

Tracing $0 iiaea$ STACK INPUT Remark

Expressions Recall grammar: Originally written as: E  E + E | E *E | (E) | id In fact we can try to construct the SLR parsing table and see what will happen E’  E E  E + T | T T  T * F | F F  ( E ) | id

Canonical Sets I0 I2 I5 I8 I1 I3 I6 I9 Follow(E’)=$ Follow(E)=+*)$ E’  .E E  .( E ) E  E * . E E  E * E . E  .E + E E  .E + E E  .E + E E  E . + E E  .E * E E  .E * E E  .E * E E  E . * E E  .( E ) E  .( E ) E  .( E ) E  .id E  .id E  .id I1 I3 I6 I9 E’  E. E  id. E  ( E . ) E  ( E ) . E  E . + E E  E . + E E  E . * E I4 E  E . * E E  E + . E E  .E + E I7 E  .E * E E  E + E . E  .( E ) E  E . + E E  .id E  E . * E Follow(E’)=$ Follow(E)=+*)$

The Parsing Table id + * ( ) $ E 0 s3 s2 1 1 s4 s5 acc 2 s3 s2 6 1. E E+E 2. E E*E 3. E (E) 4. E id id + * ( ) $ E 0 s3 s2 1 1 s4 s5 acc 2 s3 s2 6 3 r4 r4 r4 r4 4 s3 s2 7 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

Resolving Ambiguity If the state contains E  E op E . and we find op on the input we Reduce (left-association) Shift (right-association) If the state contains E  E op E . and we find op’ on the input we Reduce (op’ has lower precedence than op) Shift (op’ has higher precedence than op)

Subscript/Superscript 1. E EsubEsupE 2. E EsubE 3. E EsupE 4. E {E} 5. E c Even if we resolve conflicts using precedence and associations Rule 1 will still produce problems, i.e. when seeing a } or $ Being at the state that contains the item E EsubEsupE. We will have two possibilities for reduce: Either r3 or r1. (reduce/reduce conflict) Resolving for r1 allows a special meaning for it.