1 / 10

Conflicts in Simple LR parsers

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 is not always enough to decide between a shift and a reduce action to decide between two or more reduce actions

Download Presentation

Conflicts in Simple LR parsers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 is not always enough • to decide between a shift and a reduce action • to decide between two or more reduce actions • A shift action is indicated when some item {A ::= α • b γ} in a state has a dot preceding a terminal, and that terminal b is next in the input stream • A reduce action is indicated when some item {A ::= α •} in a state has a dot at extreme right, and that item’s lhs nonterminal A includes the next input token in its FOLLOW set • Shift/Reduce and Reduce/Reduce conflicts may occur. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  2. Origin of reduce/reduce conflicts • There may be productions in a grammar giving rise to different elements in a nonterminal’s FOLLOW set • Simple example: • S ::= a A b • S ::= c A d • A ::= β • A ::= δ • If a set of grammar symbols matching β or δ is found, followed by b, it should be reduced to A only if preceded by a • Similarly, if followed by d, it should be reduced to A only if preceded by c • The notion can be made precise in terms of viable prefixes and their valid items http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  3. Viable Prefix • Defn: A Viable Prefix is a prefix of a right sentential form that does not continue past the right end of the rightmost handle of that sentential form E Consider the right sentential form E + T + id * id E + T + goes too far for a vp E + T E + E are all viable prefixes Viable prefixes are what a SLR parser may have on its symbol stack. It must never shift another symbol beyond a handle, it must first reduce. E + T E + T T * F T F F id F id id id http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  4. Valid items for a viable prefix • An LR(0) item is ‘valid’ for a particular viable prefix if • there is some rightmost derivation involving that prefix • that derivation uses the item’s production as a derivation step • the prefix stops at the point where the item has its dot {A ::= β • γ} is valid for a viable prefix αβ if there is a rightmost derivation S αAw αβγw rm Any sequence of labels along a path in the LR(0) automaton, starting at its start state, is a viable prefix; the set of items reached at the end of that path contains those items valid for that prefix. rm http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  5. Adding lookahead tokens to items • Information can be added to an automaton state that allows certain reductions to be ruled out. • This is done by making ‘items’ more elaborate: • adding one terminal symbol as a 2nd component • making a ‘LR(1)’ item – with one symbol of lookahead • For items of the form { A ::= α • , a } the reduction A ::= α is possible only when the next input token is a. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  6. Simple grammar needing lookahead Grammar 4.49 p255 is not SLR(1) S ::= L = R | R L ::= * R | id R ::= L L and R here allude to notions of Lvalue and Rvalue, seen later … There is no right sentential form with prefix ‘ R = ‘ The reduction R ::= L should not be allowed when the next token is ‘=‘ even though ‘=‘ is in FOLLOW( R ) and ‘L’ is a viable prefix. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  7. Constructing the collection of sets of the LR(0) automaton CLOSURE(I) Add all items in I to J If {A::=α • B β} is in I & B::=γ is a production then add {B::=• γ} to set J Repeat until J stabilises Return J ITEMS(G’) Add CLOSURE({S’::=• S}) to C For all I in C For each grammar symbol X unless GOTO(I,X) is empty add GOTO(I,X) to set C Repeat until C stabilises Return C GOTO(I, X) For all {A::=α • X β} in I add {A::=α X • β} to J Return CLOSURE(J) http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  8. Constructing the collection of sets of the LR(1) automaton Items in LR(1) sets have an extra component, a ‘lookahead symbol’ CLOSURE(I) For all {A::=α • B β, a} in I For all productions B::=γ For all terminals b in FIRST(βa) add {B::=• γ, b} to I Repeat until I stabilises Return I ITEMS(G’) Add CLOSURE({S’::=• S, $}) to C For all I in C For each grammar symbol X unless GOTO(I,X) is empty add GOTO(I,X) to set C Repeat until C stabilises Return C GOTO(I, X) For all {A::=α • X β, a} in I add {A::=α X • β, a} to J Return CLOSURE(J) http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  9. Canonical LR Parser • A “canonical LR Parser” (often just “LR(1) Parser” or even just “LR Parser”) uses an LR(1) automaton. • Its sets of items contain LR(1) items • Reductions are selected if the next input token matches an item’s lookahead token • If shift/reduce conflicts or reduce/reduce conflicts still exist, it is not possible to deterministically parse sentences generated by the grammar left-to-right using only one symbol of lookahead. • The effect of making items more elaborate is to split LR(0) sets into many more LR(1) sets. The automaton will therefore have many more states. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  10. LALR(1) Parser • A simple method of producing a LALR Parser is to construct the LR(1) sets, and then combine sets with a common core • the core of a set of LR(1) items is the part resembling LR(0) items, that is, omitting the lookahead symbols • the resulting sets may have many items with the same production component but different lookahead components • More efficient techniques for constructing LALR Parsers also exist. • The GOTO of a combined set is always the combination of the GOTO’s of its elements • Attempting LALR parser construction may introduce reduce/reduce conflicts but never shift/reduce conflicts – but careful grammar design may avoid this problem • LALR parsers have the same small number of states as SLR parsers. http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

More Related