20-Bottom-Up Parsing - PII-2
20-Bottom-Up Parsing - PII-2
Arles Rodríguez
[email protected]
Facultad de Ciencias
Departamento de Matemáticas
Universidad Nacional de Colombia
Handles
• Bottom-up parsing performs two actions:
Shift
Reduce
Handles
• How do we decide when to shift or reduce?
• Given the grammar:
Unambiguous CFGs
LR(k) CFGs
LALR(k) CFGs
SLR(k)
CFGs
Recognizing handles: viable prefixes
• It is not obvious how to detect handles.
• At each step the parser sees only the stack,
not the entire input
– We start with information we can get from the
stack.
• Definition: is a viable prefix if there is an
such that | is a state of a shift-reduce parser.
Recognizing handles: viable prefix
• Definition: is a viable prefix if there is an such
that | is a state of a shift-reduce parser.
– A viable prefix does not extend past the right end
of the handle.
– It is a viable prefix because is a prefix of the
handle.
– If the parser has viable prefixes on the stack no
parsing error has been detected.
Recognizing handles: viable prefixes
′
𝑆 →.𝐸
Recognizing viable prefixes: example
′
𝑆 →𝐸.
E
′
𝑆 →.𝐸
𝜀
𝐸 →.𝑇
Recognizing viable prefixes: example
• As a NFA we cannot worry about
which production to use.
′
𝑆 →𝐸.
E 𝜀 𝐸→.𝑇+𝐸
′
𝑆 →.𝐸
𝜀
• When we reach a state where the
𝐸 →.𝑇 dot is all the way to the right
hand side this could be a handle
𝑇 that you might want to reduce.
𝐸 →𝑇 .
Recognizing viable prefixes: example
′
𝑆 →𝐸.
E 𝜀 𝐸→.𝑇+𝐸
′
𝑆 →.𝐸
𝜀
𝜀 𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
𝑇 →.(𝐸)
′
𝑆 →𝐸.
𝜀 E 𝜀 𝐸→.𝑇+𝐸
′
𝑆 →.𝐸 𝑇 → . 𝑖𝑛𝑡
𝜀 𝜀
𝐸 →.𝑇
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
𝑇 →.(𝐸)
′
𝑆 →𝐸.
𝜀 E 𝜀 𝐸→.𝑇+𝐸
′
𝑆 →.𝐸 𝑇 → . 𝑖𝑛𝑡
𝜀 𝜀
𝐸 →.𝑇 𝜀 𝑇 →.𝑖𝑛𝑡∗𝑇
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
𝑇 →.(𝐸)
′
𝑆 →𝐸. 𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝑆 →.𝐸
𝜀 𝜀 𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇 • We can have T as input
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
𝑇 →.(𝐸)
𝜀
′
𝑆 →𝐸. 𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇 • We can have all derivations of T
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸)
𝜀 𝑇 →(. 𝐸)
′
𝑆 →𝐸. 𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .)
′
𝑆 →𝐸. 𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀 • We can have E as input
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .)
′ 𝜀
𝑆 →𝐸. 𝜀
𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀 • We can have all derivations of E
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
′ 𝜀
𝑆 →𝐸. 𝜀
𝑇
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
′ 𝜀
𝑆 →𝐸. 𝜀
𝑇 +¿
𝜀 E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E
𝜀
′
𝜀
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
′ 𝜀
𝑆 →𝐸. 𝜀
𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
′
𝑆 →.𝐸 𝜀
𝜀
E→𝑇 + 𝐸.
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀 • We can have E as input
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
′
𝑆 →.𝐸 𝜀
𝜀
𝜀
E→𝑇 + 𝐸.
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀 • We can have all derivations of E
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡 ∗ 𝑇 →𝑖𝑛𝑡∗.𝑇
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇
𝜀
𝑇
𝐸 →𝑇 .
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡 ∗ 𝑇 →𝑖𝑛𝑡∗.𝑇
𝐸 →.𝑇 𝑇 →.𝑖𝑛𝑡∗𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇 𝑇
𝜀
𝑇 𝑇 →𝑖𝑛𝑡∗𝑇 .
𝐸 →𝑇 .
• We can have T as input
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇
𝜀 𝑇 →.𝑖𝑛𝑡∗𝑇 ∗
𝜀 𝜀
𝑇 𝐸 →𝑇 . 𝑇 → 𝑖𝑛𝑡 ∗ . 𝑇 𝑇
𝜀 𝑇 →𝑖𝑛𝑡∗𝑇 .
• Or derivations of T
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇
𝜀 𝑇 →.𝑖𝑛𝑡∗𝑇 ∗
𝜀 𝜀
𝑇 𝐸 →𝑇 . 𝑇 → 𝑖𝑛𝑡 ∗ . 𝑇 𝑇
𝜀 𝑇 →𝑖𝑛𝑡∗𝑇 .
Exercise
Valid items
Valid items: motivation
• Until now, we have the complete NFA for
recognizing viable prefixes of a grammar.
• Using the standard subset of states
construction we can build a DFA automaton
equivalent to the NFA.
Recognizing viable prefixes: example
¿
𝑇 →.(𝐸) 𝐸 ¿
𝜀 𝑇 →(. 𝐸) 𝑇 →( 𝐸 .) 𝑇 → ( 𝐸) .
𝜀 𝜀
′ 𝜀
𝑆 →𝐸. 𝑇 +¿
E 𝐸→.𝑇+𝐸 𝐸→𝑇 .+𝐸 E→𝑇 +.𝐸 𝐸
𝜀 𝜀
𝜀
′
𝜀 𝑇 → 𝑖𝑛𝑡 . E→𝑇 + 𝐸.
𝑆 →.𝐸 𝜀
𝜀 𝜀
𝑇 → . 𝑖𝑛𝑡 𝑖𝑛𝑡
𝑖𝑛𝑡
𝐸 →.𝑇 𝑇 →𝑖𝑛𝑡.∗𝑇
𝜀 𝑇 →.𝑖𝑛𝑡∗𝑇 ∗
𝜀 𝜀
𝑇 𝐸 →𝑇 . 𝑇 → 𝑖𝑛𝑡 ∗ . 𝑇 𝑇
𝜀 𝑇 →𝑖𝑛𝑡∗𝑇 .
Valid items: Deterministic Automata
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
This is int * T
the . 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
start
state int
by a right-most derivation.
• After parsing , the valid items are the possible
tops of the stack of items.
Valid items intuition
• An item I is valid for a viable prefix α, if the
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
Given start and look ( and see that sequences are possible
Valid items: Deterministic Automata
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
Given start and look ( and see that sequences are possible
Valid items: Deterministic Automata
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
Given start and look ( and see that sequences are possible
Valid items: Deterministic Automata
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
Given start and look ( and see that sequences are possible
Exercise: valid items
Observation