Curs 8. LR(k) parsing. S.Motogna - FL&CD

Similar documents
CS 406: Bottom-Up Parsing

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Compiler Design Spring 2017

Compiler Construction

Syntax Analysis (Part 2)

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.

CA Compiler Construction

Why augment the grammar?

Compiler Construction Lent Term 2015 Lectures (of 16)

Parsing -3. A View During TD Parsing

Bottom-Up Syntax Analysis

Compiler Construction Lent Term 2015 Lectures (of 16)

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

Bottom-Up Syntax Analysis

Compiler Construction Lectures 13 16

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

THEORY OF COMPILATION

CS20a: summary (Oct 24, 2002)

Computer Science 160 Translation of Programming Languages

Bottom-Up Syntax Analysis

Chapter 4: Bottom-up Analysis 106 / 338

Everything You Always Wanted to Know About Parsing

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Bottom-Up Syntax Analysis

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

CS415 Compilers Syntax Analysis Bottom-up Parsing

Context free languages

INF5110 Compiler Construction

Top-Down Parsing and Intro to Bottom-Up Parsing

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Translator Design Lecture 16 Constructing SLR Parsing Tables

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

On LR(k)-parsers of polynomial size

Introduction to Bottom-Up Parsing

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Introduction to Bottom-Up Parsing

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

PARSING AND TRANSLATION November 2010 prof. Ing. Bořivoj Melichar, DrSc. doc. Ing. Jan Janoušek, Ph.D. Ing. Ladislav Vagner, Ph.D.

Introduction to Bottom-Up Parsing

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Introduction to Bottom-Up Parsing

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Syntax Analysis Part I

Syntax Analysis Part I

Briefly on Bottom-up

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Parsing VI LR(1) Parsers

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

CS5371 Theory of Computation. Lecture 14: Computability V (Prove by Reduction)

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Theory of Computation (VI) Yijia Chen Fudan University

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

MA/CSSE 474 Theory of Computation

Theory of Computation

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Properties of Context-Free Languages. Closure Properties Decision Properties

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Syntax Analysis - Part 1. Syntax Analysis

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix.

CS481F01 Prelim 2 Solutions

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.

V Honors Theory of Computation

Syntax Analysis, VI Examples from LR Parsing. Comp 412

MA/CSSE 474 Theory of Computation

Computing if a token can follow

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Context-free Languages and Pushdown Automata

Computability Theory

CISC4090: Theory of Computation

Computational Models - Lecture 4

(b) If G=({S}, {a}, {S SS}, S) find the language generated by G. [8+8] 2. Convert the following grammar to Greibach Normal Form G = ({A1, A2, A3},

NPDA, CFG equivalence

Context-Free Grammars (and Languages) Lecture 7

Pushdown Automata (2015/11/23)

Pushdown Automata: Introduction (2)

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.

Compiling Techniques

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

Transcription:

Curs 8 LR(k) parsing

Terms Reminder: rhp = right handside of production lhp = left handside of production Prediction see LL(1) Handle = symbols from the head of the working stack that form (in order) a rhp Shift reduce parser: shift symbols to form a handle When a rhp is formed reduce to the corresponding lhp

LR(k) L = left sequence is read from left to right R = right use rightmost derivations k = length of prediction Enhanced grammar G = (N, Σ,P,S) G =(N {S },Σ,P {Sʹ S},Sʹ), S N S does NOT appear in any rhp

Definition 1: If in a gfg G = (N, Σ, P, S) we have S => * r αaw r αβw, where α (N Σ),A N,w Σ, then any prefix of sequence αβ is called live prefix in G. Definition 2: LR(k) item is defined as [A α.β,u], where A αβ is a production, u Σ k and describe the moment in which, considering the production A αβ, α was detected(α is in head of stack) and it is expected to detect β. Definition 3: LR(k) item is valid for the live prefix γα if: * S r γaw r γαβw u = FIRST k (w)

Definition 4: A cfg G = (N, Σ, P, S) is LR(k), for k>=0, if 1. * S r αaw r αβw 2. * S r γbx r αβy => α = γ AND A =B AND x=y 3. FIRST k (w) = FIRST k (y) γ

[A αβ.,u] rhp detected - apply reduce [A α.β,u] - shift state decide ÞWorking stack: $s init X 1 s 1... X m s m action where: $ - mark empty stack X i N s i - states

LR(k) principle Current state Current symbol prediction uniquely determines: Action to be applied Move to a new state => LR(k) table 2 parts: action part + goto part

States What a state contains? LR items closure How many states? How to go from one state to another state? goto

[A α.bβ,u] valid for live prefix γα => plică: S dr Aw dr ÆBØw ) ) ( ) valabil p u = FIRST k (w) Dacă în gramatică B δ P => S ) Aw ) dr ÆBØw ) dr ƱØw. Această observaţie sugerează faptu => [B.δ,u] valid for live prefix γα

LR(k) parsing: LR(0), SLR, LR(1), LALR Define item Construct set of states Construct table Executed 1 time Parse sequence based on moves between configurations

LR(0) Parser Prediction of length 0 (ignored) 1. LR(0) item: [A α.β]

2. Construct set of states What a state contains Algorithm closure_lr0 How to move from a state to another Function goto_lr0 Construct set of states Algorithm ColCan_LR0 Canonical collection

Algorithm Closure Algoritmul 3.8 ClosureLR0 INPUT: I-element de analiză; G - gramatica îmbogăţită OUTPUT: C = closure(i); C := {I}; repeat for 8[A! Æ.BØ] 2 C do for 8B! 2 P do if [B!. ] /2 C then C = C [ [B!. ] end if end for end for until C nu se mai modifică

Function goto goto : P(E 0 ) (N Σ) P(E 0 ) where E 0 = set of LR(0) items goto(s, X) = closure({[a αx.β] [A α.xβ] s})

Algorithm ColCan_LR(0) Algoritmul 3.9 Col starilr0 INPUT: G - gramatica îmbogăţită OUTPUT: C - colecţia canonică de stări C := ;; s 0 := closure({[s 0!.S]}) C := C [ {s 0 }; repeat for 8s 2 C do for 8X 2 N [ ß do if goto(s, X) 6= ; and goto(s, X) /2 C then C = C [ goto(s, X) end if end for end for until C nu se mai modifică

3. Construct LR(0) table one line for each state 2 parts: Action: one column (for a state, action is unique because prediction is ignored) Goto: one column for each symbol X N Σ

Rules LR(0) table 1. if [A α.β] s i then action(s i )=shift 2. if [A β.] s i and A Sʹ then action(s i )=reduce l, where l = number of production A β 3. if [Sʹ S.] s i then action(s i )=acc 4. if goto(s i, X) = s j then goto(s i, X) = s j 5. otherwise = error

Remarks 1) Initial state of parser = statea containing [Sʹ.S] 2) No shift from accept state: if s is accept state then goto(s, X) =, X N Σ. 3) If in state s action is reduce then goto(s, X) =, X N Σ. 4) Argument G : Let G = ({S},{a,b,c},{S asbs,s c},s) states [S asbs.] and [S c.] accept / reduce?

Remarks (cont) 5) A grammar is NOT LR(0) if the LR(0) table contains conflicts: shift reduce conflict: a state contains items of the form [A α.β] and [B γ.], yielding to 2 distinct actions for that state reduce reduce conflict: when a state contains items of the form [A αβ.] and [B γ.], in which the action is reduce, but with distinct productions

4. Define configurations and moves INPUT: Grammar G = (NU{S },!, P U {S ->S},S ) LR(0) table Input sequence w =a 1 a n OUTPUT: if (w L(G)) then string of productions else error & location of error

LR(0) configurations (!, ", #) where:! = working stack " = input stack # = output (result) Initial configuration: ($s 0,w$,$) Final configuration: ($s acc, $, #)

Moves 1. Shift if action(s m )= shift AND head(!)=a i AND goto(s m,a i )=S j then ($s 0 x 1...x m s m,a i...a n $, ") ($s 0 x 1...x m s m a i s j,a i+1...a n $, ") 2. Reduce if action(s m ) = reduce l AND (t) A x m p+1...x m AND goto(s m p,a) = s j then ($s 0...x m s m,a i...a n $, ") ($s 0...x m p s m p As j,a i...a n $,l ") 3. Accept if action(s m ) = accept then ($s i,$, ")=acc 4. Error - otherwise

LR(0) Parsing Algorithm INPUT: - LR(0) table conflict free - grammar G : production numbered - sequence = Input sequence w =a 1 a n OUTPUT: if (w L(G)) then string of productions else error & location of error

LR(0) Parsing Algorithm state :=0; alpha := $s0 ; beta := w$ ; phi := ; end:= false Repeat if action(state)= shift then t = pop(beta); state = goto(state,t) push(t, state, alpha); else if action(state) = reduce l then search_prod(t,rhp,lhp); pop(rhp,alpha); state = goto(head(alpha.state),lhp); push(lhp,state,alpha); push(t,out); else if action(state)= accept then write( success, out); end := true; if action(state) = error then write( error ) end := true Until end