This document provides an overview of LL parsing algorithms. It begins with a recap of formal language theory concepts like regular grammars, regular expressions, finite state automata, context-free grammars, derivation, and language generation. It then discusses predictive (recursive descent) parsing and LL parsing in particular. Key concepts covered include LL(k) grammars, filling the LL parsing table based on FIRST and FOLLOW sets, and using the table to perform LL parsing. An example grammar and its parsing table are provided to illustrate the process.
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
LL Parsing
1. Challenge the future
Delft
University of
Technology
Course IN4303
Compiler Construction
Eduardo Souza, Guido Wachsmuth, Eelco Visser
LL Parsing
Traditional Parsing Algorithms
2. lessons learned
LL Parsing
Recap: Lexical Analysis
What are the formalisms to describe regular languages?
• regular grammars
• regular expressions
• finite state automata
Why are these formalisms equivalent?
• constructive proofs
How can we generate compiler tools from that?
• implement DFAs
• generate transition tables
2
10. formal languages
LL Parsing
vocabulary Σ
finite, nonempty set of elements (words, letters)
alphabet
string over Σ
finite sequence of elements chosen from Σ
word, sentence, utterance
5
Recap: A Theory of Language
11. formal languages
LL Parsing
vocabulary Σ
finite, nonempty set of elements (words, letters)
alphabet
string over Σ
finite sequence of elements chosen from Σ
word, sentence, utterance
formal language λ
set of strings over a vocabulary Σ
λ ⊆ Σ*
5
Recap: A Theory of Language
13. formal grammars
LL Parsing
formal grammar G = (N, Σ, P, S)
nonterminal symbols N
terminal symbols Σ
production rules P ⊆ (N∪Σ)*
N (N∪Σ)*
× (N∪Σ)*
start symbol S∈N
6
Recap: A Theory of Language
14. formal grammars
LL Parsing
formal grammar G = (N, Σ, P, S)
nonterminal symbols N
terminal symbols Σ
production rules P ⊆ (N∪Σ)*
N (N∪Σ)*
× (N∪Σ)*
start symbol S∈N
6
Recap: A Theory of Language
nonterminal symbol
15. formal grammars
LL Parsing
formal grammar G = (N, Σ, P, S)
nonterminal symbols N
terminal symbols Σ
production rules P ⊆ (N∪Σ)*
N (N∪Σ)*
× (N∪Σ)*
start symbol S∈N
6
Recap: A Theory of Language
context
16. formal grammars
LL Parsing
formal grammar G = (N, Σ, P, S)
nonterminal symbols N
terminal symbols Σ
production rules P ⊆ (N∪Σ)*
N (N∪Σ)*
× (N∪Σ)*
start symbol S∈N
6
Recap: A Theory of Language
replacement
17. formal grammars
LL Parsing
formal grammar G = (N, Σ, P, S)
nonterminal symbols N
terminal symbols Σ
production rules P ⊆ (N∪Σ)*
N (N∪Σ)*
× (N∪Σ)*
start symbol S∈N
grammar classes
type-0, unrestricted
type-1, context-sensitive: (a A c, a b c)
type-2, context-free: P ⊆ N × (N∪Σ)*
type-3, regular: (A, x) or (A, xB)
6
Recap: A Theory of Language
20. formal languages
LL Parsing
formal grammar G = (N, Σ, P, S)
derivation relation G ⊆ (N∪Σ)*
× (N∪Σ)*
w G w’
∃(p, q)∈P: ∃u,v∈(N∪Σ)*
:
w=u p v ∧ w’=u q v
7
Recap: A Theory of Language
21. formal languages
LL Parsing
formal grammar G = (N, Σ, P, S)
derivation relation G ⊆ (N∪Σ)*
× (N∪Σ)*
w G w’
∃(p, q)∈P: ∃u,v∈(N∪Σ)*
:
w=u p v ∧ w’=u q v
formal language L(G) ⊆ Σ*
L(G) = {w∈Σ*
| S G
*
w}
7
Recap: A Theory of Language
22. formal languages
LL Parsing
formal grammar G = (N, Σ, P, S)
derivation relation G ⊆ (N∪Σ)*
× (N∪Σ)*
w G w’
∃(p, q)∈P: ∃u,v∈(N∪Σ)*
:
w=u p v ∧ w’=u q v
formal language L(G) ⊆ Σ*
L(G) = {w∈Σ*
| S G
*
w}
classes of formal languages
7
Recap: A Theory of Language
32. entry (X, w)∈P at row X and column T
T∈ FIRST(w)
nullable(w) ∧ T∈ FOLLOW(X)
filling the table
LL Parsing 15
Predictive parsing
33. entry (X, w)∈P at row X and column T
T∈ FIRST(w)
nullable(w) ∧ T∈ FOLLOW(X)
filling the table
LL Parsing 15
Predictive parsing
letters that w can start with
34. entry (X, w)∈P at row X and column T
T∈ FIRST(w)
nullable(w) ∧ T∈ FOLLOW(X)
filling the table
LL Parsing 15
Predictive parsing
w G
*
ε
35. entry (X, w)∈P at row X and column T
T∈ FIRST(w)
nullable(w) ∧ T∈ FOLLOW(X)
filling the table
LL Parsing 15
Predictive parsing
letters that can follow X
57. LL Parsing
Parser Combinators
34
• Parsers are modeled as functions, and larger parsers are built
from smaller ones using higher-order functions.
• Can be implemented in any lazy functional language with
higher-order style type system (e.g. Haskell, Scala).
• Parser combinators enable a recursive descent parsing strategy
that facilitates modular piecewise construction and testing.
62. LL Parsing
Parser Combinators
39
type Parser = String -> Tree
type Parser = String -> [(Tree, String)]
type Parser = String -> (Tree, String)
Returns either a singleton or
an empty list!
63. LL Parsing
Parser Combinators
40
type Parser = String -> Tree
type Parser = String -> [(Tree, String)]
A parser p returns either a singleton list [(a, String)] indicating
success, or an empty list [] indicating failure. Returning a list of
results, also allow to express ambiguities.
type Parser = String -> (Tree, String)
type Parser a = String -> [(a, String)]
64. LL Parsing
Primitive Parsers
41
success success :: a -> Parser a
success v = inp -> [(v, inp)]
success :: a String -> [(a, String)]
success v inp -> [(v, inp)]
fail
match
match :: Parser Char
match = inp -> case inp of
[] -> []
(x:xs) -> [(x,xs)]
fail :: Parser a
fail = inp -> []
or
65. LL Parsing
Parser Combinators
42
alternation
sequencing
alt :: Parser a -> Parser a -> Parser a
p 'alt' q = inp -> (p inp ++ q inp)
seq :: Parser a -> Parser b -> Parser (a, b)
p ‘seq‘ q = inp -> [((v,w), inp’’) | (v, inp’) <- p inp
, (w, inp’’) <- q inp’]
66. LL Parsing
Parser Combinators
43
• Parser combinators can use semantic actions to manipulate
intermediate results, being able to produce values rather than
trees.
• More powerful combinators can be build to allow parsing
context-sensitive languages (by passing the context together
with parsing results).
• Parser Combinators in Scala:
A. Moors, F. Piessens, Martin Odersky. Parser combinators in
Scala. Technical Report Department of Computer Science,
K.U. Leuven, February 2008.
72. LL Parsing
Parsing Expression Grammars
49
S ← a b / a ≠ S ← a / a b
• Prioritized choice '/'.
• Backtracks when parsing an alternative fails.
• Uses memoization of intermediate results to parse in linear time,
called packrat parsing.
• Does not allow left-recursion.
74. LL Parsing
LL(*)
51
• Parses LL-regular grammars: for any given non-terminal,
parsers can use the entire remaining of the input to
differentiate the alternative productions.
• Statically builds a DFA from the grammar productions to
recognize lookahead.
• When in conflict, chooses the first alternative and backtracks
when DFA lookahead recognition fails.
76. LL Parsing
LL(*) Grammar Analysis
53
S : ID
| ID = E
| unsigned* int ID
| unsigned* ID ID
(1)
(2)
(3)
(4)
augmented recursive
transition network (ATN)
s11
s
s1
s3
ε
s7
ε
ε
ε
s2
s4
s’
s8
s5
s10s9
s12 s14s13
ID
ID ID
IDint
unsigned
unsigned
ε
ε
ID =
s6
E
ε
ε
ε
ε
terminals: ID, =, int, unsigned
77. LL Parsing
LL(*) Grammar Analysis
54
S : ID
| ID = E
| unsigned* int ID
| unsigned* ID ID
(1)
(2)
(3)
(4)
terminals: ID, =, int, unsigned
converts ATN to DFA
s11
s
s1
s3
ε
s7
ε
ε
ε
s2
s4
s’
s8
s5
s10s9
s12 s14s13
ID
ID ID
IDint
unsigned
unsigned
ε
ε
ID =
s6
E
ε
ε
ε
ε
(i) Initial state = closure(s);
(ii) Create a final state for each
production;
(iii) While DFA state contains ATN states
from multiple paths, create transitions
with terminals (or EOF if DFA contains s’);
(iv) If DFA state contain ATN states from
a single path, add transition to
corresponding final DFA state;
78. s0’
s1’
s2’
s3’
=> 2
s4’
=> 1
s5’
=> 4
s6’
=> 3
ID
=
EOF
ID
ID
int
unsigned
unsigned
int
LL Parsing
LL(*) Grammar Analysis
55
(1)
(2)
(3)
(4)
S : ID
| ID = E
| unsigned* int ID
| unsigned* ID ID
s11
s
s1
s3
ε
s7
ε
ε
ε
s2
s4
s’
s8
s5
s10s9
s12 s14s13
ID
ID ID
IDint
unsigned
unsigned
ε
ε
ID =
s6
E
ε
ε
ε
ε
terminals: ID, =, int, unsigned
79. LL Parsing
Adaptive LL(*)
56
S : E = E ;
| E ;
E : E * E
| E + E
| E ( E )
| ID
void S() {
switch (adaptivePredict("S", callStack)){
case 1 :
E(); match('='); E();
match(‘;'); break;
case 2:
E(); match(';'); break;
}
}
80. LL Parsing
Adaptive LL(*)
57
Dynamically builds the lookahead DFA.
S : E = E ;
| E ;
E : E * E
| E + E
| E ( E )
| ID
terminals: =, ;, *, +, (, ), ID
s6 s8
E
s
s1
ε
ε
s2
s’
s3
E =
s4
E
ε
ε
s5
;
s7
;
81. LL Parsing
Adaptive LL(*)
58
D0 s1
ID
:1
=
s2 s4s3 :2
(
ID ) ;
D0 s1
ID
:1
=
After parsing:
x = y;
f(x);
Dynamically builds the lookahead DFA.
87. LL Parsing
Adaptive LL(*)
64
S : x B
| y C
B : A a
C : A b a
A : b
| ε
input: ba
stack: A ba
b in First(A)
b in Follow(A)
A1 or A2?
A1
A2
88. LL Parsing
Adaptive LL(*)
65
• Tries to parse using strong LL (without looking at the call
stack), when there are multiple alternatives, splits the parser to
try all possibilities.
• Parsing continues with a graph, handling multiple stacks in
paralell to explore all alternatives.
• Eventually, if the grammar is unambiguous, only one stack
"survives". If multiple stacks survive, an ambiguty has been
found and the parser picks the production with lower value.
89. LL Parsing
ANTLR 4
66
• Accepts as input any context-free grammar that does not
contain indirect or hidden left-recursion.
• Generates ALL(*) parses in Java or C#.
• Grammars contain lexical and syntactic rules and generate a
lexer and a parser.
• ANTLR4 grammars can be scannerless and composable by
using individual characters as input symbols.
93. lessons learned
LL Parsing
Summary
How can we parse context-free languages effectively?
• predictive parsing algorithms
Which grammar classes are supported by these algorithms?
• LL(k) grammars, LL(k) languages
68
94. lessons learned
LL Parsing
Summary
How can we parse context-free languages effectively?
• predictive parsing algorithms
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
68
95. lessons learned
LL Parsing
Summary
How can we parse context-free languages effectively?
• predictive parsing algorithms
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(*)
68
97. learn more
LL Parsing
Literature
formal languages
Noam Chomsky: Three models for the description of language. 1956
J. E. Hopcroft, R. Motwani, J. D. Ullman: Introduction to Automata
Theory, Languages, and Computation. 2006
69
98. learn more
LL Parsing
Literature
formal languages
Noam Chomsky: Three models for the description of language. 1956
J. E. Hopcroft, R. Motwani, J. D. Ullman: Introduction to Automata
Theory, Languages, and Computation. 2006
syntactical analysis
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
69
101. learn more
LL Parsing
Literature
ALL(*)
Terence John Parr, Sam Harwell, Kathleen Fisher. Adaptive LL(*) parsing: the
power of dynamic analysis. In OOPSLA 2014.
Parsing Expression Grammars
Bryan Ford. Parsing Expression Grammars: a recognition-based syntactic
foundation. In POPL 2004.
70
102. learn more
LL Parsing
Literature
ALL(*)
Terence John Parr, Sam Harwell, Kathleen Fisher. Adaptive LL(*) parsing: the
power of dynamic analysis. In OOPSLA 2014.
Parsing Expression Grammars
Bryan Ford. Parsing Expression Grammars: a recognition-based syntactic
foundation. In POPL 2004.
Parser Combinators
Graham Hutton. Higher-Order Functions for Parsing. Journal of Functional
Programming, 1992.
A. Moors, F. Piessens, Martin Odersky. Parser combinators in Scala. Technical
Report Department of Computer Science, K.U. Leuven, February 2008.
70
105. copyrights
LL Parsing
Pictures
Slide 1: Book Scanner by Ben Woosley, some rights reserved
Slides 5-7: Noam Chomsky by Fellowsisters, some rights reserved
Slide 8, 9, 26-28: Tiger by Bernard Landgraf, some rights reserved
Slide 32: Ostsee by Mario Thiel, some rights reserved
73