SlideShare uma empresa Scribd logo
1 de 146
Baixar para ler offline
Challenge the future
Delft
University of
Technology
Course IN4303
Compiler Construction
Eduardo Souza, Guido Wachsmuth, Eelco Visser
LR Parsing
Traditional Parsing Algorithms
lessons learned
LR Parsing
Recap: Traditional Parsing Algorithms
How can we parse context-free languages effectively?
• predictive parsing
Which grammar classes are supported by these algorithms?
• LL(k) grammars, LL(k) languages
How can we generate compiler tools from that?
• implement automaton
• generate parse tables
What are other techniques for implementing top-down parsers?
• Parser Combinators
• PEGs
• ALL(*)
2
today’s lecture
Lexical Analysis
Overview
3
today’s lecture
Lexical Analysis
Overview
efficient parsing algorithms
• LR parsing
• LR parse table generation
• SLR & LALR parse tables
• Generalized LR parsing
• Scannerless Generalized LR parsing
3
LR Parsing
LR Parsing
I
4
idea
LR Parsing
LR parsing
problems with LL parsing
• predicting right rule
• left recursion
LR parsing
• see whole right-hand side of a rule
• look ahead
• shift or reduce
5
example
LR Parsing
LR parsing
6
* 3 $
input
$
stack
7* 37 +
S → E $
E → E * E
E → E + E
E → Num
grammar
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
$ E + E * E $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
$ E + E
$ E + E * E $
$
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
$ E + E
$ E + E * E $
$
$ E $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
$ E + E
$ E + E * E $
$
$ E $
S → E $
E → E * E
E → E + E
E → Num
example
LR Parsing
LR parsing
6
* 3 $$ 7* 37 +
* 3 $7+$ E 3*
* 3 $7+$ E * E
* 3 $7+$ E
$ E + E 3* $
$ E + E
$ E + E * E $
$
$ E $
$ S
S → E $
E → E * E
E → E + E
E → Num
LR Parsing
Grammar classes
7
context-free grammars
LL(k)
LL(1)
LL(0)
LR Parsing
Grammar classes
7
context-free grammars
LR(0)
LL(k)
LL(1)
LL(0)
LR Parsing
Grammar classes
7
context-free grammars
LR(1)
LR(0)
LL(k)
LL(1)
LL(0)
LR Parsing
Grammar classes
7
context-free grammars
LR(k)
LR(1)
LR(0)
LL(k)
LL(1)
LL(0)
LR Parsing
Grammar classes
7
context-free grammars
LR(k)
LR(1)
SLR
LR(0)
LL(k)
LL(1)
LL(0)
LR Parsing
Grammar classes
7
context-free grammars
LR(k)
LR(1)
LALR(1)
SLR
LR(0)
LL(k)
LL(1)
LL(0)
LR Parsing
LR Parse Tables
II
8
parse table
LR Parsing 9
rows
• states of a DFA
columns
• topmost stack symbol
• Σ, N
entries
• reduce, rule number
• shift, goto state
• goto state
• accept state
LR parsing
T1 ... N1 ...
1 s 3
2 g 5
3 r 1
4 r 2 a
5
6 g 1
7 s 1
8
...
items, closure & goto
LR Parsing
LR(0) parse tables
10
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
items, closure & goto
LR Parsing
LR(0) parse tables
10
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
items, closure & goto
LR Parsing
LR(0) parse tables
10
item
S → x
S → ( L )
L → S
L → L , S
closure
• for every item A → α . X β
• for every rule X → γ
• add item X → . γ
S’ → . S $
items, closure & goto
LR Parsing
LR(0) parse tables
10
item
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
closure
• for every item A → α . X β
• for every rule X → γ
• add item X → . γ
items, closure & goto
LR Parsing
LR(0) parse tables
10
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
L → L , . S
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
L → L , . S
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
L → L , . S
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
x
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
L → L , . S
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
x
(
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
L → L , . S
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
x
(
S
L → L , S .
S → x
S → ( L )
L → S
L → L , S
S’ → . S $
S → . x
S → . ( L )
L → L , . S
S → . x
S → . ( L )
items, closure & goto
LR Parsing
LR(0) parse tables
10
S’ → S . $
S
x
S → x .
(
S → ( . L )
L → . S
L → . L , S
S → . x
S → . ( L )
x
(
S
L → S .
L S → ( L . )
L → L . , S
)
S → ( L ) .
,
x
(
S
L → L , S .
S → x
S → ( L )
L → S
L → L , S
1
2
3
4 6 7
5
8
9
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,x(
1
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,x
1
3
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,
1
3
2
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,
1
3
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,
1
3
6
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,
1
3
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x,
1
3
5
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)x
1
3
5
8
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)
1
3
5
8
2
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)
1
3
5
8
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)
1
3
5
8
9
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)
1
3
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$)
1
3
5
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$
1
3
5
7
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$
1
( ) x , $ S L
1 s 3 s 2 g 4
2 r 1 r 1 r 1 r 1 r 1
3 s 3 s 2 g 6 g 5
4 a
5 s 7 s 8
6 r 3 r 3 r 3 r 3 r 3
7 r 2 r 2 r 2 r 2 r 2
8 s 3 s 2 g 9
9 r 4 r 4 r 4 r 4 r 4
result
LR Parsing 11
LR(0) parse tables
S → x
S → ( L )
L → S
L → L , S
$
1
4
LR Parsing
Conflict Resolution
III
12
E → T + . E
E → . T + E
E → . T
T → . x
S → . E $
E → . T + E
E → . T
T → . x
shift-reduce conflicts
LR Parsing
SLR parse tables
13
T → x .
x
E
S → E . $
T
T +
E
E → T . + E
E → T .
E → T + E .
E → T + E
E → T
T → x
x
E → T + . E
E → . T + E
E → . T
T → . x
S → . E $
E → . T + E
E → . T
T → . x
shift-reduce conflicts
LR Parsing
SLR parse tables
13
T → x .
x
E
S → E . $
T
T +
E
E → T . + E
E → T .
E → T + E .
E → T + E
E → T
T → x
1
2
3
5 4 6x
E → T + . E
E → . T + E
E → . T
T → . x
S → . E $
E → . T + E
E → . T
T → . x
shift-reduce conflicts
LR Parsing
SLR parse tables
13
T → x .
x
E
S → E . $
T
T +
E
E → T . + E
E → T .
E → T + E .
E → T + E
E → T
T → x
1
2
3
5 4 6x
x + $ E T
1 s 5 g 2 g 3
2 a
3 r 2 ? r 2
4 s 5 g 6 g 3
5 r 3 r 3 r 3
6 r 1 r 1 r 1
E → T + . E
E → . T + E
E → . T
T → . x
S → . E $
E → . T + E
E → . T
T → . x
shift-reduce conflicts
LR Parsing
SLR parse tables
13
T → x .
x
E
S → E . $
T
T +
E
E → T . + E
E → T .
E → T + E .
E → T + E
E → T
T → x
1
2
3
5 4 6x
x + $ E T
1 s 5 g 2 g 3
2 a
3 s 4 r 2
4 s 5 g 6 g 3
5 r 3 r 3
6 r 1
Reduce a production S → …
on symbols k ∈ Σ, k ∈ Follow(S)
look-ahead
LR Parsing
LR(1) parse tables
14
E → T + E
E → T
T → x
S → . E $
E → . T + E
E → . T
T → . x
?
$
$
+ $
E
S → E . $ ?
T E → T . + E
E → T .
$
$
T → x .
x
+ $
T +
x
E → T + . E
E → . T + E
E → . T
T → . x
$
$
$
+ $ E E → T + E . $
closure
• for every item A → α . X β, z
• for every rule X → γ
• for every w ∈ First(βz)
• add item X → . γ, w
look-ahead
LR Parsing
LR(1) parse tables
14
E → T + E
E → T
T → x
S → . E $
E → . T + E
E → . T
T → . x
?
$
$
+ $
E
S → E . $ ?
T E → T . + E
E → T .
$
$
T → x .
x
+ $
T +
x
E → T + . E
E → . T + E
E → . T
T → . x
$
$
$
+ $ E E → T + E . $
x + $ E T
1 s 5 g 2 g 3
2 a
3 s 4 r 2
4 s 5 g 6 g 3
5 r 3 r 3
6 r 1
closure
• for every item A → α . X β, z
• for every rule X → γ
• for every w ∈ First(βz)
• add item X → . γ, w
state space reduction
LR Parsing
LALR(1) parse tables
unify states
• with same items
• and same outgoing transitions
• but different look-ahead sets
might introduce new conflicts
15
state space reduction
LR Parsing
LALR(1) parse tables
16
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
state space reduction
LR Parsing
LALR(1) parse tables
17
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
a
state space reduction
LR Parsing
LALR(1) parse tables
18
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
a
b
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
state space reduction
LR Parsing
LALR(1) parse tables
19
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
E → e .
F → e .
c
d
a
b e
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
state space reduction
LR Parsing
LALR(1) parse tables
20
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
E → e .
F → e .
c
d
a
b e
e
E → e .
F → e .
d
c
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
state space reduction
LR Parsing
LALR(1) parse tables
21
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
E → e .
F → e .
c
d
a
b e
e
E → e .
F → e .
d
c
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
state space reduction
LR Parsing
LALR(1) parse tables
22
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
E → e .
F → e .
{c, d}
{c, d}
a
b e
e
state space reduction
LR Parsing
LALR(1) parse tables
23
S’ → S $
S → a E c
S → a F d
S → b F c
S → b E d
E → e
F → e
S' → . S $
S → . a E c
S → . a F d
S → . b F c
S → . b E d
?
$
$
$
$
E → e .
F → e .
{c, d}
{c, d}
a
b e
e Reduce/Reduce
conflict!
S → a . E c
S → a . F d
E → . e
F → . e
$
$
c
d
S → b . F c
S → b . E d
E → . e
F → . e
$
$
d
c
LR Parsing
Generalized-LR Parsing
IV
24
Lexical Analysis
Generalized Parsing
• Parse all interpretations of the input, therefore it can handle
ambiguous grammars.
• Parsers split whenever finding an ambiguous interpretation and
act in (pseudo) parallel.
• Multiple parsers can join whenever they finish parsing an
ambiguous fragment of the input.
• Some parsers may "die", if the ambiguity was caused by a lack
of lookahead.
25
Lexical Analysis
Generalized LR
• Multiple parsers are synchronized on shift actions.
• Each parser has its own stack, and as they share states, the
overall structure becomes a graph (GSS).
• If two parsers have the same state on top of their stack, they
are joined into a single parser.
• Reduce actions affect all possible paths from the top of the
stack.
26
Lexical Analysis
Generalized LR
27
S → E $
E → E + E
E → E * E
E → a
SLR table
Lexical Analysis
Generalized LR
28
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
0
SLR table
Lexical Analysis
Generalized LR
29
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E
a
10
2
SLR table
Lexical Analysis
Generalized LR
30
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E → E + . E
E → . E + E
E → . E * E
E → . a
E → E * . E
E → . E + E
E → . E * E
E → . a
S → E $ .E
$
a
*
+
10 3
2
4
5
SLR table
Lexical Analysis
Generalized LR
31
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E → E + . E
E → . E + E
E → . E * E
E → . a
E → E * . E
E → . E + E
E → . E * E
E → . a
E → E * E .
E → E . + E
E → E . * E
S → E $ .E
$
a
*
+ E
a
10 3
2
4
5
6
SLR table
Lexical Analysis
Generalized LR
32
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E → E + . E
E → . E + E
E → . E * E
E → . a
E → E * . E
E → . E + E
E → . E * E
E → . a
E → E + E .
E → E . + E
E → E . * E
E → E * E .
E → E . + E
E → E . * E
S → E $ .E
$
a
*
+ E
E
a
a
10 3
2
4
5
7
6
SLR table
Lexical Analysis
Generalized LR
33
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E → E + . E
E → . E + E
E → . E * E
E → . a
E → E * . E
E → . E + E
E → . E * E
E → . a
E → E + E .
E → E . + E
E → E . * E
E → E * E .
E → E . + E
E → E . * E
S → E $ .E
$
a
*
+ E
*
E
+
a
a
10 3
2
4
5
7
6
SLR table
Lexical Analysis
Generalized LR
34
S → E $
E → E + E
E → E * E
E → a
S → . E $
E → . E + E
E → . E * E
E → . a
S → E . $
E → E . + E
E → E . * E
E → a .
E → E + . E
E → . E + E
E → . E * E
E → . a
E → E * . E
E → . E + E
E → . E * E
E → . a
E → E + E .
E → E . + E
E → E . * E
E → E * E .
E → E . + E
E → E . * E
S → E $ .E
$
a
*
+ E
*
*
+
E
+
a
a
10 3
2
4
5
7
6
SLR table
Lexical Analysis
Generalized LR
35
Nonter
minal
Nullable First Follow
S
E
State
Action Goto
a + * $ S E
0
1
2
3
4
5
6
7
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
SLR table
Lexical Analysis
Generalized LR
36
Nonter
minal
Nullable First Follow
S no a -
E no a +, *, $
State
Action Goto
a + * $ S E
0
1
2
3
4
5
6
7
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
SLR table
Lexical Analysis
Generalized LR
37
Nonter
minal
Nullable First Follow
S no a -
E no a +, *, $
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
SLR table
Lexical Analysis
Generalized LR
38
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
a + a * a
0
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
Lexical Analysis
Generalized LR
39
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
+ a * a
0
2a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
synchronize on shifts
0
2a
Lexical Analysis
Generalized LR
40
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
+ a * a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
Lexical Analysis
Generalized LR
41
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
+ a * a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
2a
1a : E
0
2a
1a : E 4
+
Lexical Analysis
Generalized LR
42
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
a * a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
synchronize
Lexical Analysis
Generalized LR
43
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
a * a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
Lexical Analysis
Generalized LR
44
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
* a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
2
a
synchronize
0
1a : E 4
+
2
a
Lexical Analysis
Generalized LR
45
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
* a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
2
a
a : E
7
Lexical Analysis
Generalized LR
46
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
* a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
Lexical Analysis
Generalized LR
47
Parsing
* a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
2
a
a : E
7
1a + a : E
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
0
1a : E 4
+
2
a
a : E
7
1a + a : E
5
**
Lexical Analysis
Generalized LR
48
Parsing
a
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
synchronize
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
Lexical Analysis
Generalized LR
49
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
2
a
Lexical Analysis
Generalized LR
50
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
synchronize
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
2
a
Lexical Analysis
Generalized LR
51
Parsing
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
Lexical Analysis
Generalized LR
52
Parsing
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
Lexical Analysis
Generalized LR
53
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
Lexical Analysis
Generalized LR
54
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
Lexical Analysis
Generalized LR
55
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
1
[a + a] * a : E
Lexical Analysis
Generalized LR
56
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
1
[a + a] * a : E
Lexical Analysis
Generalized LR
57
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
[a + a] * a : E
or
a + [a * a] : E
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
1
Lexical Analysis
Generalized LR
58
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
[a + a] * a : E
or
a + [a * a] : E
0
1a : E 4
+
a : E
7
1a + a : E
5
*
*
6
a : E
7
a * a : E
1 3
$
Lexical Analysis
Generalized LR
59
Parsing
State
Action Goto
a + * $ S E
0 s2 1
1 s4 s5 s3
2 r3 r3 r3
3 acc
4 s2 7
5 s2 6
6 s4/r2 s5/r2 r2
7 s4/r1 s5/r1 r1
(0) S → E $
(1) E → E + E
(2) E → E * E
(3) E → a
synchronize
accept with trees on the link to initial state
LR Parsing
Scannerless Generalized-LR Parsing
V
60
Lexical Analysis
Scannerless Generalized LR
• Integrates scanning + parsing into a single GLR algorithm.
• Normalization separates lexical and context-free symbols.
• Crucial for avoiding conflicts when composing languages.
• Introduces lexical ambiguities. Solution:
‣Follow Restrictions (implement longest match).
‣Reject Rules (implement reserved keywords).
61
Lexical Analysis
Scannerless Generalized LR
62
Normalization
context-free syntax
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Inc = <<Exp>++>
Exp.ID = ID
lexical syntax
ID = [a-zA-Z] IDRest*
IDRest = [a-zA-Z0-9]
LAYOUT = [ tnr]
ID = "let" {reject}
context-free syntax
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Inc = <<Exp>++>
Exp.ID = ID
lexical syntax
ID = [a-zA-Z] IDRest*
IDRest = [a-zA-Z0-9]
LAYOUT = [ tnr]
ID = "let" {reject}
Lexical Analysis
Scannerless Generalized LR
63
Normalization
Exp-CF.Add = Exp-CF LAYOUT?-CF "+" LAYOUT?-CF Exp-CF {left}
Exp-CF.Inc = Exp-CF LAYOUT?-CF "++"
Exp-CF.ID = ID-CF
ID-LEX = [a-zA-Z] IDRest*-LEX
IDRest*-LEX = [a-zA-Z0-9]
ID-LEX = "let" {reject}
LAYOUT-CF = LAYOUT-LEX
Separate lexical from context-free symbols
and separate symbols in context-free syntax
by optional layout.
Lexical Analysis
Scannerless Generalized LR
64
Normalization
LAYOUT-CF = LAYOUT-LEX
IDRest-CF = IDRest-LEX
IDRest*-CF = IDRest*-LEX
ID-CF = ID-LEX
context-free syntax
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Inc = <<Exp>++>
Exp.ID = ID
lexical syntax
ID = [a-zA-Z] IDRest*
IDRest = [a-zA-Z0-9]
LAYOUT = [ tnr]
ID = "let" {reject}
Create injections from lexical to
context-free symbols
Lexical Analysis
Scannerless Generalized LR
65
Normalization
"+" = [43]
"++" = [43] [43]
"let" = [108] [101] [116]
ID-LEX = [65-9097-122] IDRest*-LEX
IDRest*-LEX = [48-5765-9097-122]
LAYOUT-LEX = [9-101332]
context-free syntax
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Inc = <<Exp>++>
Exp.ID = ID
lexical syntax
ID = [a-zA-Z] IDRest*
IDRest = [a-zA-Z0-9]
LAYOUT = [ tnr]
ID = "let" {reject}
Normalize literals symbols and
character classes.
Lexical Analysis
Scannerless Generalized LR
66
Normalization
LAYOUT?-CF = LAYOUT-CF
LAYOUT?-CF =
IDRest+-LEX = IDRest-LEX
IDRest+-LEX = IDRest+-LEX IDRest-LEX
IDRest*-LEX =
IDRest*-LEX = IDRest+-LEX
IDRest+-CF = IDRest+-LEX
context-free syntax
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Inc = <<Exp>++>
Exp.ID = ID
lexical syntax
ID = [a-zA-Z] IDRest*
IDRest = [a-zA-Z0-9]
LAYOUT = [ tnr]
ID = "let" {reject}
Normalize regular expressions.
Lexical Analysis
Scannerless Generalized LR
67
Normalization
context-free start-symbols
Exp
<START> = LAYOUT?-CF Exp-CF LAYOUT?-CF
<Start> = <START> [256]
Define extra rules for the start symbols.
Lexical Analysis
Scannerless Generalized LR
68
Parse Table Generation
• Generation is based on SLR(1) item-sets.
• Uses character classes instead of tokens.
• Follow sets and goto actions are calculated for productions
instead of non-terminals.
Lexical Analysis
Scannerless Generalized LR
69
Lexical Disambiguation
• A reject rule is of the form:
ID = "let" {reject}
• When SGLR does a reduction with a reject rule, it marks the
link as rejected.
• Further action on a stack is forbidden whenever all links to it
are rejected.
• Follow restrictions are implemented as filters on the follow sets
of productions.
Lexical Analysis
Generalized LR
70
• Priority rules: applied at parse table generation.
E → E * . E
E → . E + E
E → . E * E
E → . a
multiplication has higher
priority than addition!
Context-free Disambiguation
Lexical Analysis
Generalized LR
71
• Priority rules: applied at parse table generation.
multiplication is left
associative!
E → E * . E
E → . E + E
E → . E * E
E → . a
Context-free Disambiguation
Lexical Analysis
Generalized LR
72
• Priority rules: applied at parse table generation.
• Disambiguation filters: applied after parsing (prefer and avoid).
multiplication is left
associative!
E → E * . E
E → . E + E
E → . E * E
E → . a
Context-free Disambiguation
LR Parsing
Summary
VI
73
lessons learned
LR Parsing
Summary
74
lessons learned
LR Parsing
Summary
How can we generate LR parse tables?
• items, closure, goto
74
lessons learned
LR Parsing
Summary
How can we generate LR parse tables?
• items, closure, goto
How can we improve LR(0) parse table generation?
• SLR: consider FOLLOW sets to avoid shift-reduce conflicts
• LR(1): consider look-ahead in states
• LALR(1): unify LR(1) states to reduce state space
74
lessons learned
LR Parsing
Summary
How can we generate LR parse tables?
• items, closure, goto
How can we improve LR(0) parse table generation?
• SLR: consider FOLLOW sets to avoid shift-reduce conflicts
• LR(1): consider look-ahead in states
• LALR(1): unify LR(1) states to reduce state space
How can we handle conflicts in the parse table?
• generalized parsing - supports all class of context free grammars
• scannerless generalized LR - allow for proper language composition.
74
learn more
LR Parsing
Literature
75
learn more
LR Parsing
Literature
LR parsing
Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java,
2nd edition. 2002
Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers:
Principles, Techniques, and Tools, 2nd edition. 2006
75
learn more
LR Parsing
Literature
LR parsing
Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java,
2nd edition. 2002
Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers:
Principles, Techniques, and Tools, 2nd edition. 2006
Generalised LR parsing
Eelco Visser: Syntax Definition for Language Prototyping. PhD thesis 1997
M.G.J. van den Brand, J. Scheerder, J.J. Vinju, and E. Visser: Disambiguation
Filters for Scannerless Generalized LR Parsers. CC 2002
75
LR Parsing
copyrights
76
LR Parsing 77
copyrights
LR Parsing
Pictures
Slide 1:
Book Scanner by Ben Woosley, some rights reserved
Slide 19:
Ostsee by Mario Thiel, some rights reserved
78

Mais conteúdo relacionado

Mais procurados (20)

LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Parsing
ParsingParsing
Parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Time complexity
Time complexityTime complexity
Time complexity
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 

Destaque

Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedGuido Wachsmuth
 
Programming languages
Programming languagesProgramming languages
Programming languagesEelco Visser
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage CollectionEelco Visser
 
Software languages
Software languagesSoftware languages
Software languagesEelco Visser
 
Register Allocation
Register AllocationRegister Allocation
Register AllocationEelco Visser
 

Destaque (11)

Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
 
LL Parsing
LL ParsingLL Parsing
LL Parsing
 
Type analysis
Type analysisType analysis
Type analysis
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Software languages
Software languagesSoftware languages
Software languages
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Register Allocation
Register AllocationRegister Allocation
Register Allocation
 
Language
LanguageLanguage
Language
 

Semelhante a LR Parsing

LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation pptWPVKP.COM
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Iffat Anjum
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxsrilakshmis17
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7myasir16
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.pptBapanKar2
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptxvenkatapranaykumarGa
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.pptAmrita Sharma
 
compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdfRijuMandal11
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRRiazul Islam
 
Zippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesZippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesJay Coskey
 

Semelhante a LR Parsing (20)

Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Presentation mam saima kanwal
Presentation mam saima kanwalPresentation mam saima kanwal
Presentation mam saima kanwal
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
 
Parsing example
Parsing exampleParsing example
Parsing example
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
Flat unit 2
Flat unit 2Flat unit 2
Flat unit 2
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.ppt
 
parsing.pptx
parsing.pptxparsing.pptx
parsing.pptx
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
 
Zippers: Derivatives of Regular Types
Zippers: Derivatives of Regular TypesZippers: Derivatives of Regular Types
Zippers: Derivatives of Regular Types
 

Mais de Eelco Visser

CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingEelco Visser
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesEelco Visser
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingEelco Visser
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionEelco Visser
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionEelco Visser
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesEelco Visser
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with StatixEelco Visser
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionEelco Visser
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Eelco Visser
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementEelco Visser
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersEelco Visser
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationEelco Visser
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesEelco Visser
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksEelco Visser
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisEelco Visser
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionEelco Visser
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsEelco Visser
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingEelco Visser
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisEelco Visser
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingEelco Visser
 

Mais de Eelco Visser (20)

CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 

Último

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Último (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

LR Parsing

  • 1. Challenge the future Delft University of Technology Course IN4303 Compiler Construction Eduardo Souza, Guido Wachsmuth, Eelco Visser LR Parsing Traditional Parsing Algorithms
  • 2. lessons learned LR Parsing Recap: Traditional Parsing Algorithms How can we parse context-free languages effectively? • predictive parsing Which grammar classes are supported by these algorithms? • LL(k) grammars, LL(k) languages How can we generate compiler tools from that? • implement automaton • generate parse tables What are other techniques for implementing top-down parsers? • Parser Combinators • PEGs • ALL(*) 2
  • 4. today’s lecture Lexical Analysis Overview efficient parsing algorithms • LR parsing • LR parse table generation • SLR & LALR parse tables • Generalized LR parsing • Scannerless Generalized LR parsing 3
  • 6. idea LR Parsing LR parsing problems with LL parsing • predicting right rule • left recursion LR parsing • see whole right-hand side of a rule • look ahead • shift or reduce 5
  • 7. example LR Parsing LR parsing 6 * 3 $ input $ stack 7* 37 + S → E $ E → E * E E → E + E E → Num grammar
  • 8. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + S → E $ E → E * E E → E + E E → Num
  • 9. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + S → E $ E → E * E E → E + E E → Num
  • 10. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* S → E $ E → E * E E → E + E E → Num
  • 11. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* S → E $ E → E * E E → E + E E → Num
  • 12. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* S → E $ E → E * E E → E + E E → Num
  • 13. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E S → E $ E → E * E E → E + E E → Num
  • 14. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E S → E $ E → E * E E → E + E E → Num
  • 15. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E S → E $ E → E * E E → E + E E → Num
  • 16. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E S → E $ E → E * E E → E + E E → Num
  • 17. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ S → E $ E → E * E E → E + E E → Num
  • 18. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ S → E $ E → E * E E → E + E E → Num
  • 19. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ S → E $ E → E * E E → E + E E → Num
  • 20. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ $ E + E * E $ S → E $ E → E * E E → E + E E → Num
  • 21. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ $ E + E $ E + E * E $ $ S → E $ E → E * E E → E + E E → Num
  • 22. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ $ E + E $ E + E * E $ $ $ E $ S → E $ E → E * E E → E + E E → Num
  • 23. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ $ E + E $ E + E * E $ $ $ E $ S → E $ E → E * E E → E + E E → Num
  • 24. example LR Parsing LR parsing 6 * 3 $$ 7* 37 + * 3 $7+$ E 3* * 3 $7+$ E * E * 3 $7+$ E $ E + E 3* $ $ E + E $ E + E * E $ $ $ E $ $ S S → E $ E → E * E E → E + E E → Num
  • 25. LR Parsing Grammar classes 7 context-free grammars LL(k) LL(1) LL(0)
  • 26. LR Parsing Grammar classes 7 context-free grammars LR(0) LL(k) LL(1) LL(0)
  • 27. LR Parsing Grammar classes 7 context-free grammars LR(1) LR(0) LL(k) LL(1) LL(0)
  • 28. LR Parsing Grammar classes 7 context-free grammars LR(k) LR(1) LR(0) LL(k) LL(1) LL(0)
  • 29. LR Parsing Grammar classes 7 context-free grammars LR(k) LR(1) SLR LR(0) LL(k) LL(1) LL(0)
  • 30. LR Parsing Grammar classes 7 context-free grammars LR(k) LR(1) LALR(1) SLR LR(0) LL(k) LL(1) LL(0)
  • 31. LR Parsing LR Parse Tables II 8
  • 32. parse table LR Parsing 9 rows • states of a DFA columns • topmost stack symbol • Σ, N entries • reduce, rule number • shift, goto state • goto state • accept state LR parsing T1 ... N1 ... 1 s 3 2 g 5 3 r 1 4 r 2 a 5 6 g 1 7 s 1 8 ...
  • 33. items, closure & goto LR Parsing LR(0) parse tables 10 S → x S → ( L ) L → S L → L , S
  • 34. S’ → . S $ items, closure & goto LR Parsing LR(0) parse tables 10 S → x S → ( L ) L → S L → L , S
  • 35. S’ → . S $ items, closure & goto LR Parsing LR(0) parse tables 10 item S → x S → ( L ) L → S L → L , S
  • 36. closure • for every item A → α . X β • for every rule X → γ • add item X → . γ S’ → . S $ items, closure & goto LR Parsing LR(0) parse tables 10 item S → x S → ( L ) L → S L → L , S
  • 37. S’ → . S $ S → . x S → . ( L ) closure • for every item A → α . X β • for every rule X → γ • add item X → . γ items, closure & goto LR Parsing LR(0) parse tables 10 S → x S → ( L ) L → S L → L , S
  • 38. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S → x S → ( L ) L → S L → L , S
  • 39. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S S → x S → ( L ) L → S L → L , S
  • 40. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . S → x S → ( L ) L → S L → L , S
  • 41. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) S → x S → ( L ) L → S L → L , S
  • 42. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) S → x S → ( L ) L → S L → L , S
  • 43. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x S → x S → ( L ) L → S L → L , S
  • 44. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S → x S → ( L ) L → S L → L , S
  • 45. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . S → x S → ( L ) L → S L → L , S
  • 46. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S S → x S → ( L ) L → S L → L , S
  • 47. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . S → x S → ( L ) L → S L → L , S
  • 48. S’ → . S $ S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , L → L , . S S → x S → ( L ) L → S L → L , S
  • 49. S’ → . S $ S → . x S → . ( L ) L → L , . S S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , S → x S → ( L ) L → S L → L , S
  • 50. S’ → . S $ S → . x S → . ( L ) L → L , . S S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , x S → x S → ( L ) L → S L → L , S
  • 51. S’ → . S $ S → . x S → . ( L ) L → L , . S S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , x ( S → x S → ( L ) L → S L → L , S
  • 52. S’ → . S $ S → . x S → . ( L ) L → L , . S S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , x ( S L → L , S . S → x S → ( L ) L → S L → L , S
  • 53. S’ → . S $ S → . x S → . ( L ) L → L , . S S → . x S → . ( L ) items, closure & goto LR Parsing LR(0) parse tables 10 S’ → S . $ S x S → x . ( S → ( . L ) L → . S L → . L , S S → . x S → . ( L ) x ( S L → S . L S → ( L . ) L → L . , S ) S → ( L ) . , x ( S L → L , S . S → x S → ( L ) L → S L → L , S 1 2 3 4 6 7 5 8 9
  • 54. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S
  • 55. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x,x( 1
  • 56. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x,x 1 3
  • 57. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x, 1 3 2
  • 58. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x, 1 3
  • 59. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x, 1 3 6
  • 60. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x, 1 3
  • 61. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x, 1 3 5
  • 62. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $)x 1 3 5 8
  • 63. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $) 1 3 5 8 2
  • 64. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $) 1 3 5 8
  • 65. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $) 1 3 5 8 9
  • 66. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $) 1 3
  • 67. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $) 1 3 5
  • 68. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $ 1 3 5 7
  • 69. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $ 1
  • 70. ( ) x , $ S L 1 s 3 s 2 g 4 2 r 1 r 1 r 1 r 1 r 1 3 s 3 s 2 g 6 g 5 4 a 5 s 7 s 8 6 r 3 r 3 r 3 r 3 r 3 7 r 2 r 2 r 2 r 2 r 2 8 s 3 s 2 g 9 9 r 4 r 4 r 4 r 4 r 4 result LR Parsing 11 LR(0) parse tables S → x S → ( L ) L → S L → L , S $ 1 4
  • 72. E → T + . E E → . T + E E → . T T → . x S → . E $ E → . T + E E → . T T → . x shift-reduce conflicts LR Parsing SLR parse tables 13 T → x . x E S → E . $ T T + E E → T . + E E → T . E → T + E . E → T + E E → T T → x x
  • 73. E → T + . E E → . T + E E → . T T → . x S → . E $ E → . T + E E → . T T → . x shift-reduce conflicts LR Parsing SLR parse tables 13 T → x . x E S → E . $ T T + E E → T . + E E → T . E → T + E . E → T + E E → T T → x 1 2 3 5 4 6x
  • 74. E → T + . E E → . T + E E → . T T → . x S → . E $ E → . T + E E → . T T → . x shift-reduce conflicts LR Parsing SLR parse tables 13 T → x . x E S → E . $ T T + E E → T . + E E → T . E → T + E . E → T + E E → T T → x 1 2 3 5 4 6x x + $ E T 1 s 5 g 2 g 3 2 a 3 r 2 ? r 2 4 s 5 g 6 g 3 5 r 3 r 3 r 3 6 r 1 r 1 r 1
  • 75. E → T + . E E → . T + E E → . T T → . x S → . E $ E → . T + E E → . T T → . x shift-reduce conflicts LR Parsing SLR parse tables 13 T → x . x E S → E . $ T T + E E → T . + E E → T . E → T + E . E → T + E E → T T → x 1 2 3 5 4 6x x + $ E T 1 s 5 g 2 g 3 2 a 3 s 4 r 2 4 s 5 g 6 g 3 5 r 3 r 3 6 r 1 Reduce a production S → … on symbols k ∈ Σ, k ∈ Follow(S)
  • 76. look-ahead LR Parsing LR(1) parse tables 14 E → T + E E → T T → x S → . E $ E → . T + E E → . T T → . x ? $ $ + $ E S → E . $ ? T E → T . + E E → T . $ $ T → x . x + $ T + x E → T + . E E → . T + E E → . T T → . x $ $ $ + $ E E → T + E . $ closure • for every item A → α . X β, z • for every rule X → γ • for every w ∈ First(βz) • add item X → . γ, w
  • 77. look-ahead LR Parsing LR(1) parse tables 14 E → T + E E → T T → x S → . E $ E → . T + E E → . T T → . x ? $ $ + $ E S → E . $ ? T E → T . + E E → T . $ $ T → x . x + $ T + x E → T + . E E → . T + E E → . T T → . x $ $ $ + $ E E → T + E . $ x + $ E T 1 s 5 g 2 g 3 2 a 3 s 4 r 2 4 s 5 g 6 g 3 5 r 3 r 3 6 r 1 closure • for every item A → α . X β, z • for every rule X → γ • for every w ∈ First(βz) • add item X → . γ, w
  • 78. state space reduction LR Parsing LALR(1) parse tables unify states • with same items • and same outgoing transitions • but different look-ahead sets might introduce new conflicts 15
  • 79. state space reduction LR Parsing LALR(1) parse tables 16 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $
  • 80. state space reduction LR Parsing LALR(1) parse tables 17 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ S → a . E c S → a . F d E → . e F → . e $ $ c d a
  • 81. state space reduction LR Parsing LALR(1) parse tables 18 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ S → b . F c S → b . E d E → . e F → . e $ $ d c a b S → a . E c S → a . F d E → . e F → . e $ $ c d
  • 82. state space reduction LR Parsing LALR(1) parse tables 19 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ E → e . F → e . c d a b e S → b . F c S → b . E d E → . e F → . e $ $ d c S → a . E c S → a . F d E → . e F → . e $ $ c d
  • 83. state space reduction LR Parsing LALR(1) parse tables 20 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ E → e . F → e . c d a b e e E → e . F → e . d c S → b . F c S → b . E d E → . e F → . e $ $ d c S → a . E c S → a . F d E → . e F → . e $ $ c d
  • 84. state space reduction LR Parsing LALR(1) parse tables 21 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ E → e . F → e . c d a b e e E → e . F → e . d c S → b . F c S → b . E d E → . e F → . e $ $ d c S → a . E c S → a . F d E → . e F → . e $ $ c d
  • 85. state space reduction LR Parsing LALR(1) parse tables 22 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ S → a . E c S → a . F d E → . e F → . e $ $ c d S → b . F c S → b . E d E → . e F → . e $ $ d c E → e . F → e . {c, d} {c, d} a b e e
  • 86. state space reduction LR Parsing LALR(1) parse tables 23 S’ → S $ S → a E c S → a F d S → b F c S → b E d E → e F → e S' → . S $ S → . a E c S → . a F d S → . b F c S → . b E d ? $ $ $ $ E → e . F → e . {c, d} {c, d} a b e e Reduce/Reduce conflict! S → a . E c S → a . F d E → . e F → . e $ $ c d S → b . F c S → b . E d E → . e F → . e $ $ d c
  • 88. Lexical Analysis Generalized Parsing • Parse all interpretations of the input, therefore it can handle ambiguous grammars. • Parsers split whenever finding an ambiguous interpretation and act in (pseudo) parallel. • Multiple parsers can join whenever they finish parsing an ambiguous fragment of the input. • Some parsers may "die", if the ambiguity was caused by a lack of lookahead. 25
  • 89. Lexical Analysis Generalized LR • Multiple parsers are synchronized on shift actions. • Each parser has its own stack, and as they share states, the overall structure becomes a graph (GSS). • If two parsers have the same state on top of their stack, they are joined into a single parser. • Reduce actions affect all possible paths from the top of the stack. 26
  • 90. Lexical Analysis Generalized LR 27 S → E $ E → E + E E → E * E E → a SLR table
  • 91. Lexical Analysis Generalized LR 28 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a 0 SLR table
  • 92. Lexical Analysis Generalized LR 29 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E a 10 2 SLR table
  • 93. Lexical Analysis Generalized LR 30 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E → E + . E E → . E + E E → . E * E E → . a E → E * . E E → . E + E E → . E * E E → . a S → E $ .E $ a * + 10 3 2 4 5 SLR table
  • 94. Lexical Analysis Generalized LR 31 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E → E + . E E → . E + E E → . E * E E → . a E → E * . E E → . E + E E → . E * E E → . a E → E * E . E → E . + E E → E . * E S → E $ .E $ a * + E a 10 3 2 4 5 6 SLR table
  • 95. Lexical Analysis Generalized LR 32 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E → E + . E E → . E + E E → . E * E E → . a E → E * . E E → . E + E E → . E * E E → . a E → E + E . E → E . + E E → E . * E E → E * E . E → E . + E E → E . * E S → E $ .E $ a * + E E a a 10 3 2 4 5 7 6 SLR table
  • 96. Lexical Analysis Generalized LR 33 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E → E + . E E → . E + E E → . E * E E → . a E → E * . E E → . E + E E → . E * E E → . a E → E + E . E → E . + E E → E . * E E → E * E . E → E . + E E → E . * E S → E $ .E $ a * + E * E + a a 10 3 2 4 5 7 6 SLR table
  • 97. Lexical Analysis Generalized LR 34 S → E $ E → E + E E → E * E E → a S → . E $ E → . E + E E → . E * E E → . a S → E . $ E → E . + E E → E . * E E → a . E → E + . E E → . E + E E → . E * E E → . a E → E * . E E → . E + E E → . E * E E → . a E → E + E . E → E . + E E → E . * E E → E * E . E → E . + E E → E . * E S → E $ .E $ a * + E * * + E + a a 10 3 2 4 5 7 6 SLR table
  • 98. Lexical Analysis Generalized LR 35 Nonter minal Nullable First Follow S E State Action Goto a + * $ S E 0 1 2 3 4 5 6 7 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a SLR table
  • 99. Lexical Analysis Generalized LR 36 Nonter minal Nullable First Follow S no a - E no a +, *, $ State Action Goto a + * $ S E 0 1 2 3 4 5 6 7 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a SLR table
  • 100. Lexical Analysis Generalized LR 37 Nonter minal Nullable First Follow S no a - E no a +, *, $ State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a SLR table
  • 101. Lexical Analysis Generalized LR 38 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 a + a * a 0 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 102. Lexical Analysis Generalized LR 39 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 + a * a 0 2a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a synchronize on shifts
  • 103. 0 2a Lexical Analysis Generalized LR 40 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 + a * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 104. Lexical Analysis Generalized LR 41 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 + a * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a 0 2a 1a : E
  • 105. 0 2a 1a : E 4 + Lexical Analysis Generalized LR 42 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 a * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a synchronize
  • 106. Lexical Analysis Generalized LR 43 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 a * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a 0 1a : E 4 +
  • 107. Lexical Analysis Generalized LR 44 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a 0 1a : E 4 + 2 a synchronize
  • 108. 0 1a : E 4 + 2 a Lexical Analysis Generalized LR 45 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 109. 0 1a : E 4 + 2 a a : E 7 Lexical Analysis Generalized LR 46 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 110. Lexical Analysis Generalized LR 47 Parsing * a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a 0 1a : E 4 + 2 a a : E 7 1a + a : E State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1
  • 111. 0 1a : E 4 + 2 a a : E 7 1a + a : E 5 ** Lexical Analysis Generalized LR 48 Parsing a (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a synchronize State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1
  • 112. Lexical Analysis Generalized LR 49 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a 0 1a : E 4 + a : E 7 1a + a : E 5 * * a
  • 113. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 2 a Lexical Analysis Generalized LR 50 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a synchronize
  • 114. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 2 a Lexical Analysis Generalized LR 51 Parsing (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1
  • 115. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E Lexical Analysis Generalized LR 52 Parsing (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1
  • 116. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E Lexical Analysis Generalized LR 53 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 117. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E Lexical Analysis Generalized LR 54 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 118. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E Lexical Analysis Generalized LR 55 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 119. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E 1 [a + a] * a : E Lexical Analysis Generalized LR 56 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 120. 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E 1 [a + a] * a : E Lexical Analysis Generalized LR 57 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 121. [a + a] * a : E or a + [a * a] : E 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E 1 Lexical Analysis Generalized LR 58 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a
  • 122. [a + a] * a : E or a + [a * a] : E 0 1a : E 4 + a : E 7 1a + a : E 5 * * 6 a : E 7 a * a : E 1 3 $ Lexical Analysis Generalized LR 59 Parsing State Action Goto a + * $ S E 0 s2 1 1 s4 s5 s3 2 r3 r3 r3 3 acc 4 s2 7 5 s2 6 6 s4/r2 s5/r2 r2 7 s4/r1 s5/r1 r1 (0) S → E $ (1) E → E + E (2) E → E * E (3) E → a synchronize accept with trees on the link to initial state
  • 124. Lexical Analysis Scannerless Generalized LR • Integrates scanning + parsing into a single GLR algorithm. • Normalization separates lexical and context-free symbols. • Crucial for avoiding conflicts when composing languages. • Introduces lexical ambiguities. Solution: ‣Follow Restrictions (implement longest match). ‣Reject Rules (implement reserved keywords). 61
  • 125. Lexical Analysis Scannerless Generalized LR 62 Normalization context-free syntax Exp.Add = <<Exp> + <Exp>> {left} Exp.Inc = <<Exp>++> Exp.ID = ID lexical syntax ID = [a-zA-Z] IDRest* IDRest = [a-zA-Z0-9] LAYOUT = [ tnr] ID = "let" {reject}
  • 126. context-free syntax Exp.Add = <<Exp> + <Exp>> {left} Exp.Inc = <<Exp>++> Exp.ID = ID lexical syntax ID = [a-zA-Z] IDRest* IDRest = [a-zA-Z0-9] LAYOUT = [ tnr] ID = "let" {reject} Lexical Analysis Scannerless Generalized LR 63 Normalization Exp-CF.Add = Exp-CF LAYOUT?-CF "+" LAYOUT?-CF Exp-CF {left} Exp-CF.Inc = Exp-CF LAYOUT?-CF "++" Exp-CF.ID = ID-CF ID-LEX = [a-zA-Z] IDRest*-LEX IDRest*-LEX = [a-zA-Z0-9] ID-LEX = "let" {reject} LAYOUT-CF = LAYOUT-LEX Separate lexical from context-free symbols and separate symbols in context-free syntax by optional layout.
  • 127. Lexical Analysis Scannerless Generalized LR 64 Normalization LAYOUT-CF = LAYOUT-LEX IDRest-CF = IDRest-LEX IDRest*-CF = IDRest*-LEX ID-CF = ID-LEX context-free syntax Exp.Add = <<Exp> + <Exp>> {left} Exp.Inc = <<Exp>++> Exp.ID = ID lexical syntax ID = [a-zA-Z] IDRest* IDRest = [a-zA-Z0-9] LAYOUT = [ tnr] ID = "let" {reject} Create injections from lexical to context-free symbols
  • 128. Lexical Analysis Scannerless Generalized LR 65 Normalization "+" = [43] "++" = [43] [43] "let" = [108] [101] [116] ID-LEX = [65-9097-122] IDRest*-LEX IDRest*-LEX = [48-5765-9097-122] LAYOUT-LEX = [9-101332] context-free syntax Exp.Add = <<Exp> + <Exp>> {left} Exp.Inc = <<Exp>++> Exp.ID = ID lexical syntax ID = [a-zA-Z] IDRest* IDRest = [a-zA-Z0-9] LAYOUT = [ tnr] ID = "let" {reject} Normalize literals symbols and character classes.
  • 129. Lexical Analysis Scannerless Generalized LR 66 Normalization LAYOUT?-CF = LAYOUT-CF LAYOUT?-CF = IDRest+-LEX = IDRest-LEX IDRest+-LEX = IDRest+-LEX IDRest-LEX IDRest*-LEX = IDRest*-LEX = IDRest+-LEX IDRest+-CF = IDRest+-LEX context-free syntax Exp.Add = <<Exp> + <Exp>> {left} Exp.Inc = <<Exp>++> Exp.ID = ID lexical syntax ID = [a-zA-Z] IDRest* IDRest = [a-zA-Z0-9] LAYOUT = [ tnr] ID = "let" {reject} Normalize regular expressions.
  • 130. Lexical Analysis Scannerless Generalized LR 67 Normalization context-free start-symbols Exp <START> = LAYOUT?-CF Exp-CF LAYOUT?-CF <Start> = <START> [256] Define extra rules for the start symbols.
  • 131. Lexical Analysis Scannerless Generalized LR 68 Parse Table Generation • Generation is based on SLR(1) item-sets. • Uses character classes instead of tokens. • Follow sets and goto actions are calculated for productions instead of non-terminals.
  • 132. Lexical Analysis Scannerless Generalized LR 69 Lexical Disambiguation • A reject rule is of the form: ID = "let" {reject} • When SGLR does a reduction with a reject rule, it marks the link as rejected. • Further action on a stack is forbidden whenever all links to it are rejected. • Follow restrictions are implemented as filters on the follow sets of productions.
  • 133. Lexical Analysis Generalized LR 70 • Priority rules: applied at parse table generation. E → E * . E E → . E + E E → . E * E E → . a multiplication has higher priority than addition! Context-free Disambiguation
  • 134. Lexical Analysis Generalized LR 71 • Priority rules: applied at parse table generation. multiplication is left associative! E → E * . E E → . E + E E → . E * E E → . a Context-free Disambiguation
  • 135. Lexical Analysis Generalized LR 72 • Priority rules: applied at parse table generation. • Disambiguation filters: applied after parsing (prefer and avoid). multiplication is left associative! E → E * . E E → . E + E E → . E * E E → . a Context-free Disambiguation
  • 138. lessons learned LR Parsing Summary How can we generate LR parse tables? • items, closure, goto 74
  • 139. lessons learned LR Parsing Summary How can we generate LR parse tables? • items, closure, goto How can we improve LR(0) parse table generation? • SLR: consider FOLLOW sets to avoid shift-reduce conflicts • LR(1): consider look-ahead in states • LALR(1): unify LR(1) states to reduce state space 74
  • 140. lessons learned LR Parsing Summary How can we generate LR parse tables? • items, closure, goto How can we improve LR(0) parse table generation? • SLR: consider FOLLOW sets to avoid shift-reduce conflicts • LR(1): consider look-ahead in states • LALR(1): unify LR(1) states to reduce state space How can we handle conflicts in the parse table? • generalized parsing - supports all class of context free grammars • scannerless generalized LR - allow for proper language composition. 74
  • 142. learn more LR Parsing Literature LR parsing Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers: Principles, Techniques, and Tools, 2nd edition. 2006 75
  • 143. learn more LR Parsing Literature LR parsing Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers: Principles, Techniques, and Tools, 2nd edition. 2006 Generalised LR parsing Eelco Visser: Syntax Definition for Language Prototyping. PhD thesis 1997 M.G.J. van den Brand, J. Scheerder, J.J. Vinju, and E. Visser: Disambiguation Filters for Scannerless Generalized LR Parsers. CC 2002 75
  • 146. copyrights LR Parsing Pictures Slide 1: Book Scanner by Ben Woosley, some rights reserved Slide 19: Ostsee by Mario Thiel, some rights reserved 78