A New Look at LR(k) Bill McKeeman, MathWorks Fellow for Worcester Polytechnic Computer Science Colloquium February 20, 2004.

Slides:



Advertisements
Similar presentations
Compiler Designs and Constructions
Advertisements

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
LR Parsing Table Costruction
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Bottom Up Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
Normal forms for Context-Free Grammars
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Compilation 2007 Context-Free Languages Parsers and Scanners Michael I. Schwartzbach BRICS, University of Aarhus.
MATLAB objects using nested functions MathWorks Compiler Course – Day 2.
Bottom-up parsing Goal of parser : build a derivation
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
1 Languages and Compilers (SProg og Oversættere) Parsing.
Syntax and Semantics Structure of programming languages.
LR Parsing Compiler Baojian Hua
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Introduction to Parsing
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
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.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
COMPILER CONSTRUCTION
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
50/50 rule You need to get 50% from tests, AND
Bottom-Up Parsing.
Lexical and Syntax Analysis
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Table-driven parsing Parsing performed by a finite state machine.
Bottom-Up Syntax Analysis
4 (c) parsing.
Syntax Analysis Sections :.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
A New Look at LR(k) Bill McKeeman, MathWorks Fellow for
LR(1) grammars The Chinese University of Hong Kong Fall 2010
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Compiler Construction
Chapter Fifteen: Stack Machine Applications
LR(1) grammars The Chinese University of Hong Kong Fall 2011
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

A New Look at LR(k) Bill McKeeman, MathWorks Fellow for Worcester Polytechnic Computer Science Colloquium February 20, 2004

Abstract Knuth offered LR(k) as an algorithm for parsing computer languages (Information and Control, Nov. 1965). Most work since has concentrated on parser usability and space/time optimization. This talk presents LR(k) from first principles, teasing apart some complex formalisms applied by Knuth into simpler components. Assumptions The audience is comfortable with mathematical notation and computer programming.

Points to Ponder A context-free grammar can describe meaning in addition to syntax. The set of reducible parse stacks is a regular language. Any finite automata can be described by a context-free grammar. The LR(0) machine can be constructed in two stages, first an NFA, then a DFA. In most cases, the LALR(1) lookahead can be teased out of the LR(0) machine.

A context-free grammar is a four-tuple describing a language L. N is a set of non-terminal symbols (naming syntactic structure like expression). T is a set of terminal symbols (naming the written symbols like +, for, int …) V = N  T, N  T = { }. G  N, is a set of symbols describing the language (usually just sentence or translation-unit). R is a set of rewriting rules, R  N  V*. Given an input text from language L, we repeatedly substitute the LHS of a rewriting rule for its RHS, leading eventually to something in G. The sequence of substitutions is called the parse.

Why Use Context-free Grammars? N = {P D C B}, T = {  | & t f }, G = {P}. 1.P→D  2.D→D | C 3.D→C 4.C→C & B 5.C→B 6.B→ t 7.B→ f t&t|f&f  B &t|f&f  C &t|f&f  C & B| f&f  C |f&f  D |f&f  D | B &f  D | C &f  D | C & B  D | C  D  P tt&ff&|tt&ff&| 7 rewriting rules:rewritingsruleconsumed terminals tt&ff&|  pcode Grammar for boolean expressions

How to Get a Parse? Recursive Descent (top down) –Write a C function for each nonterminal –Report rule applications –Pro: no tools needed, Con: can be buggy YACC (bottom up) –Write a CFG –Implement rule applications –Pro: reliable, Con: deal with YACC diagnostics

Parse StackInputS/R sequence t&t|f&f  start t&t|f&f  shift t B &t|f&f  rule 6 C &t|f&f  rule 5 C &t|f&f  shift & C & t |f&f  shift t C & B |f&f  rule 6 C |f&f  rule 4 D |f&f  rule 3 D |f&f  shift | D |f&f  shift f D | B &f  rule 7 D | C &f  rule 5 D | C &f  shift & D | C &f  shift f D | C & B  rule 5 D | C  rule 4 D  rule 2 D  shift  Prule 1 (quit) The red parse stacks are reducible S/R is useful for languages where a lexer is needed.

The set L of all reducible parse stacks for L is a regular language (recognizable with a finite automaton). L has a grammar that can be derived from the CFG of the original language L. The terminal symbols of L are the things than can appear on parse stacks (that is, all of T and N). The nonterminal symbols of L are all the partial rules of L (that is, leave off zero or more symbols on the end of a rule of L and get a nonterminal of L). The resulting rules R are all of the forms A→Bb A→B A→ Using finite automata terminology, rules A→Bb are read as “in state B if you see b, eat it and go to state A”. Rules A→B are the same but no b. Rule A→ says “create A from nothing”, which is how we get started. This definition leaves undefined the situation if an unacceptable symbol appears in the input. One can make the NFA 1-1 by adding an error state E and rules of the form E→Bb to fill out the tables. In practice the error state triggers a syntax diagnostic. It is too verbose for use here.

LR(0) NFA Construction notation: ,   V* L defined by T = V // anything on the parse stack N = {A →  | A →   R} // any partial rule G = R // any completed rule R = {A →  a → A →  a | A →  a   R}  {B → → A →  | A →  B   R  A  N}  {A → → | A  G}

P→D  D→D | C D→C C→C & B C→B B→ t B→ f R for LN for L 0P→ 1P→D 2P→D  3D→ 4D→D 5D→D | 6D→D | C 7D→C 8C→ 9C→C 10C→C & 11C→C & B 12C→B 13B→ 14B→ t 15B→ f R for L 0→ 1→0D 2→1  4→3D 5→4 | 6→5C 7→3C 9→8C 10→9 & 11→10B 12→8B 14→13 t 15→13 f 3→0 8→3 8→5 13→8 13→10 G for L Apply LR(0) NFA D|C & B 0D | C & B 3D | C & B 4 | C & B 5C & B 8C & B 9 & B 10B 11 For rule 11 we rewrite C→C & B and start over: D | C elements of N renamed for readability

NFA to DFA The NFA is not efficient. So we apply the (textbook) NFA-to-DFA transformation, getting yet another context-free grammar. In this case the nonterminals are sets of partial rules, and the terminals are the same as the for the NFA (since they describe the same language).

LR(0) DFA Construction E(M) = M  {X | Y  E(M)  X→Y  R} // tx on empty S(M, b) = E({X | Y  M  X→Yb  R}) // tx on b from M 0 = E({0}) // start state N = {0}  {S(M,b) | M  N  b  T} G = { M | M  N  M  G ≠ {} } // contains a rule P = {0→}  {N→Mb | M  N  b  T  N = S(M,b)} Note: this construction gives just the states reachable from 0. The textbook formalism is simpler but computes useless states

The LR(0) DFA T for L P D C B  | & t f N for L T for L P D C B  | & t f N for L R for L 0→ 1→0D 2→1  3→1 | 4→3C 5→4 & 6→5B 7→5t 8→5f 10→3B 7→3 t 8→3 f 9→0C 10→0B 7→0 t 8→0 f N renamed for readability D|C&B0D|C&B1|C&B3C&B4&B5B6D|C&B0D|C&B1|C&B3C&B4&B5B6 Apply LR(0) DFA G for L

The LR(0) DFA D C B t f  | & C B t f & B t f D|C&B0D|C&B1|C&B3C&B4&B5B6D|C&B0D|C&B1|C&B3C&B4&B5B6 Apply LR(0) DFA States 2, 6, 7, 8, 10 are obviously in G (no place to go). States 4 and 9 are in G but also can shift. States 4 and 9 show LR(0) shift/reduce conflicts. For this CFG, shift if you can resolves the conflicts. Note that is what we did for state 4 above. C→C D→D | C C→C D→C

The LALR(1) Lookahead D C B t f  | & C B t f & B t f The LALR(1) lookahead for D→D | C in state 4 is found by stepping back from 4, over the RHS of the rule (D | C), then stepping forward (from 0 in this case) over the LHS of the rule (D) and collecting the transition symbols (  | ). Since the transition symbols out of 1 are different from the transition symbols out of 4 ( & ), there is no LALR(1) shift/reduce conflict. If we erroneously did the reduction D→D | C while looking at something other than  or |, we would immediately fail on the next step.

PDCB  |&tf R R R4 7R6 8R7 9R3 5 10R5 Conventional LALR(1) parsing table in state: see:

NQLALR vs LALR The simple trick for LALR(1) lookahead worked because the grammar for L had no erasing rules (rules with empty RHS). One way to insure the no-erasing property is to transform the user’s grammar, removing the erasing rules. The trick is to remove each erasing rule, say F→, and everywhere in the grammar that F is used, create a new rule with F erased. F→ F →i H→hFj F→i H→hFj H→hj

Putting it all together (1) D C B t f  | & C B t f & B t f 0 t&t|f&f  2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B reduce states parse stackinput action start

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (2) D C B t f  | & C B t f & B t f 7 t 0 &t|f&f  reduce states parse stackinput action shift t

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (3) D C B t f  | & C B t f & B t f B0 B0 &t|f&f  reduce states parse stackinput action reduce B→ t

10 B 0 2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (4) D C B t f  | & C B t f & B t f &t|f&f  reduce states parse stackinput action shift B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (5) D C B t f  | & C B t f & B t f C0 C0 &t|f&f  reduce states parse stackinput action reduce C→B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (6) D C B t f  | & C B t f & B t f 9 C09 C0 &t|f&f  reduce states parse stackinput action shift C

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (7) D C B t f  | & C B t f & B t f 5 & 9 C 0 t|f&f  reduce states parse stackinput action shift &

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (8) D C B t f  | & C B t f & B t f 7 t 5 & 9 C 0 |f&f  reduce states parse stackinput action shift t

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (9) D C B t f  | & C B t f & B t f B 5 & 9 C 0 |f&f  reduce states parse stackinput action reduce B→ t Note: we do not have to go all the way back to state 0.

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (10) D C B t f  | & C B t f & B t f 6 B 5 & 9 C 0 |f&f  reduce states parse stackinput action shift B Note: we do not have to go all the way back to state 0.

C0 C0 2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (11) D C B t f  | & C B t f & B t f |f&f  reduce states parse stackinput action reduce C→C & B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (12) D C B t f  | & C B t f & B t f 9 C09 C0 |f&f  reduce states parse stackinput action shift C

D0 D0 2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (13) D C B t f  | & C B t f & B t f |f&f  reduce states parse stackinput action reduce D→C

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (14) D C B t f  | & C B t f & B t f 1 D01 D0 |f&f  reduce states parse stackinput action shift D

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (15) D C B t f  | & C B t f & B t f 3 | 1 D 0 f&f  reduce states parse stackinput action shift |

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (16) D C B t f  | & C B t f & B t f f 3 | 1 D 0 &f  reduce states parse stackinput action shift f

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (17) D C B t f  | & C B t f & B t f B 3 | 1 D 0 &f  reduce states parse stackinput action reduce B→ f

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (18) D C B t f  | & C B t f & B t f 10 B 3 | 1 D 0 &f  reduce states parse stackinput action shift B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (19) D C B t f  | & C B t f & B t f C 3 | 1 D 0 &f  reduce states parse stackinput action reduce C→B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (20) D C B t f  | & C B t f & B t f 4 C 3 | 1 D 0 &f  reduce states parse stackinput action shift C

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (21) D C B t f  | & C B t f & B t f 5 & 4 C 3 | 1 ff reduce states parse stackinput action shift &

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (22) D C B t f  | & C B t f & B t f f 5 & 4 C 3  reduce states parse stackinput action shift f

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (23) D C B t f  | & C B t f & B t f B 5 & 4 C 3 |  reduce states parse stackinput action reduce B→ f

6 B 5 & 4 C 3 2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (24) D C B t f  | & C B t f & B t f  reduce states parse stackinput action shift B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (25) D C B t f  | & C B t f & B t f C 3 | 1 D 0  reduce states parse stackinput action reduce C→C & B

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (26) D C B t f  | & C B t f & B t f 4 C 3 | 1 D 0  reduce states parse stackinput action shift C

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (27) D C B t f  | & C B t f & B t f D0 D0  reduce states parse stackinput action reduce D→D | C

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (28) D C B t f  | & C B t f & B t f 1 D01 D0  reduce states parse stackinput action shift D

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (29) D C B t f  | & C B t f & B t f 2  1 D 0 reduce states parse stackinput action shift 

2 P→D  4 D→D | C 6 C→C & B 7 B→ t 8 B→ f 9 D→C 10 C→B Putting it all together (30) D C B t f  | & C B t f & B t f P0 P0 reduce states parse stackinput action reduce P→D  quit