KEMBAR78
LR(0) PARSER | PPTX
LR(0) PARSER
Mr.Sameer Prembabu Mamadapure
Information Technology Department
International Institute of Information Technology, I²IT
www.isquareit.edu.in
LR(k) Parsers
• Left-to-right, rightmost derivation with k-token
lookahead.
• Most general parsing technique for deterministic
grammars.
• In general, not practical: tables too large (10^6
states for C++, Ada).
• Common subsets: SLR, LALR (1).
The states of the LR(0)
automaton
• An item is a point within a production, indicating that part
of the production has been recognized:
A a. Bb ,
seen the expansion of a, expect to see expansion of B
• A state is a set of items
• Transition within states are determined by terminals and
non-terminals
• Parsing tables are built from automaton:
action: shift / reduce depending on next symbol
goto: change state depending on synthesized non-terminal
Building LR (0) states
• If a state includes:
A a .Bb
• it also includes every state that is the start of B:
B . X Y Z
• Informally: if I expect to see B next, I expect to see
anything that B can start with, and so on:
X . G H I
• States are built by closure from individual items.
E’ E
E E + T | T; -- left-recursion ok here.
T T * F | F;
F id | (E)
S0 = { E’ .E, E .E + T, E .T,
F .id, F . ( E ) ,
T .T * F, T .F }
A grammar of expressions: initial state
Adding states
• If a state has item A a .a b,
and the next symbol in the input is a, we shift a on the
stack and enter a state that contains item
• A a a.b
(as well as all other items brought in by closure)
• if a state has as item A a. , this indicates the end of
a production: reduce action.
• If a state has an item A a .N b, then after a
reduction that find an N, go to a state with A a N. b
Construction of a predictive parsing
table
• The following rules are used to construct the
predictive parsing table:
– 1. for each terminal a in FIRST(α),
add A → α to matrix M[A,a]
– 2. if λ is in FIRST(α), then
for each terminal b in FOLLOW(A),
add A → α to matrix M[A,b]
The LR (0) states for expressions
• S1 = { E’ E., E E. + T }
• S2 = { E T., T T. * F }
• S3 = { T F. }
• S4 = { F (. E), } + S0 (by closure)
• S5 = { F id. }
• S6 = { E E +. T, T .T * F, T .F, F .id, F .(E)}
• S7 = { T T *. F, F .id, F .(E)}
• S8 = { F (E.), E E.+ T}
• S9 = { E E + T., T T.* F}
• S10 = { T T * F.}, S11 = {F (E).}
LR(0) Automaton
International Institute of Information Technology (I²IT)
P-14, Rajiv Gandhi Infotech Park, MIDC Phase – 1,
Hinjawadi, Pune – 411057, India
Email - info@isquareit.edu.in
Website - http://www.isquareit.edu.in/
THANK-YOU

LR(0) PARSER

  • 1.
    LR(0) PARSER Mr.Sameer PrembabuMamadapure Information Technology Department International Institute of Information Technology, I²IT www.isquareit.edu.in
  • 2.
    LR(k) Parsers • Left-to-right,rightmost derivation with k-token lookahead. • Most general parsing technique for deterministic grammars. • In general, not practical: tables too large (10^6 states for C++, Ada). • Common subsets: SLR, LALR (1).
  • 3.
    The states ofthe LR(0) automaton • An item is a point within a production, indicating that part of the production has been recognized: A a. Bb , seen the expansion of a, expect to see expansion of B • A state is a set of items • Transition within states are determined by terminals and non-terminals • Parsing tables are built from automaton: action: shift / reduce depending on next symbol goto: change state depending on synthesized non-terminal
  • 4.
    Building LR (0)states • If a state includes: A a .Bb • it also includes every state that is the start of B: B . X Y Z • Informally: if I expect to see B next, I expect to see anything that B can start with, and so on: X . G H I • States are built by closure from individual items.
  • 5.
    E’ E E E+ T | T; -- left-recursion ok here. T T * F | F; F id | (E) S0 = { E’ .E, E .E + T, E .T, F .id, F . ( E ) , T .T * F, T .F } A grammar of expressions: initial state
  • 6.
    Adding states • Ifa state has item A a .a b, and the next symbol in the input is a, we shift a on the stack and enter a state that contains item • A a a.b (as well as all other items brought in by closure) • if a state has as item A a. , this indicates the end of a production: reduce action. • If a state has an item A a .N b, then after a reduction that find an N, go to a state with A a N. b
  • 7.
    Construction of apredictive parsing table • The following rules are used to construct the predictive parsing table: – 1. for each terminal a in FIRST(α), add A → α to matrix M[A,a] – 2. if λ is in FIRST(α), then for each terminal b in FOLLOW(A), add A → α to matrix M[A,b]
  • 8.
    The LR (0)states for expressions • S1 = { E’ E., E E. + T } • S2 = { E T., T T. * F } • S3 = { T F. } • S4 = { F (. E), } + S0 (by closure) • S5 = { F id. } • S6 = { E E +. T, T .T * F, T .F, F .id, F .(E)} • S7 = { T T *. F, F .id, F .(E)} • S8 = { F (E.), E E.+ T} • S9 = { E E + T., T T.* F} • S10 = { T T * F.}, S11 = {F (E).}
  • 9.
  • 10.
    International Institute ofInformation Technology (I²IT) P-14, Rajiv Gandhi Infotech Park, MIDC Phase – 1, Hinjawadi, Pune – 411057, India Email - info@isquareit.edu.in Website - http://www.isquareit.edu.in/ THANK-YOU