CS 3240: Languages and Computation - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CS 3240: Languages and Computation

Description:

Definition: The core of a state of the DFA of LR(1) items is the set of LR(0) ... (s1,X), then there is a transition t2= (s2,X) where t1 and t2 have the same core ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 10
Provided by: ble87
Category:

less

Transcript and Presenter's Notes

Title: CS 3240: Languages and Computation


1
CS 3240 Languages and Computation
  • General LR(1) and LALR(1) Parsing

2
Review
  • LR(0)
  • What are LR(0) items?
  • How is DFA for LR(0) defined?
  • Simple LR(1)
  • How does SLR(1) extends LR(0)?
  • General LR(1)
  • What are LR(1) items?
  • How is DFA for LR(1) items defined?
  • Their corresponding parsing algorithms
  • Conflicts of actions

3
Finite Automata for LR(1)
  • Transition from A??.X?,a to A??X.?,a with
    symbol X
  • Note that this does not cause new lookahead
  • Given LR(1) item A??.B?,a where B is a
    nonterminal, add ?-transition to items B?.?,b
    for every production rule B?? and every token b
    in First(?a)

4
GLR(1) Parsing Algorithm
  • If statecontains any item A??.X?,a and current
    input token is X, then
  • shift X
  • push new state ?(A??.X?,a,X)
  • If state contains any item A??.,a and next
    input token is a, then
  • remove ? and corresponding states from stack
  • If rule is S?S.,, then accept if input token
    is
  • backup the DFA to the state s from which
    construction of ? began
  • push A and new state ?(s,A) onto stack

5
Examples
  • Example 1
  • S ? ( S ) a
  • Parse string ((a))
  • Example 2
  • S ? id V E V ? id E ? V number

6
LALR(1) Parsing
  • LR(1) has too high complexity (too many states)
  • How to reduce number of states?
  • If LR(1) items in two states differ only by
    lookahead variables, then merge the states
  • Definition The core of a state of the DFA of
    LR(1) items is the set of LR(0) items consisting
    of the first components of all LR(1) items in the
    state.

7
Principles of LALR(1) Parsing
  • First principle (observation)
  • The core of a state of the LR(1) DFA is a state
    of the LR(0) DFA
  • Second principle (observation)
  • Given two states s1 and s2 of the LR(1) DFA with
    the same core, if there is a transition
    t1?(s1,X), then there is a transition t2?(s2,X)
    where t1 and t2 have the same core
  • Based on these principles, we merge LR(1) states
    with the same cores with a set of lookahead
    symbols in each item

8
Examples
  • Example 1
  • Consider grammar S ? ( S ) a
  • Parse string ((a))
  • Example 2
  • S ? id V E V ? id E ? V number

9
Remarks
  • One can construct the LALR(1) DFA directly from
    LR(0) DFA by propagating lookaheads
  • LALR(1) grammars is a subset of LR(1)
  • It is possible (bur rarely) that LALR(1)
    introduces conflicts that are not present in
    LR(1)
  • Almost all grammars used by programming languages
    are LALR(1)
Write a Comment
User Comments (0)
About PowerShow.com