LL Parser
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, an LL parser (Left-to-right,
leftmost derivation In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
) is a
top-down parser Top-down parsing in computer science is a parsing strategy where one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar. LL parsers are a type of parser that uses a top-d ...
for a restricted
context-free language In formal language theory, a context-free language (CFL) is a language generated by a context-free grammar (CFG). Context-free languages have many applications in programming languages, in particular, most arithmetic expressions are generated by ...
. It parses the input from Left to right, performing Leftmost derivation of the sentence. An LL parser is called an LL(''k'') parser if it uses ''k'' tokens of lookahead when parsing a sentence. A grammar is called an LL(''k'') grammar if an LL(''k'') parser can be constructed from it. A formal language is called an LL(''k'') language if it has an LL(''k'') grammar. The set of LL(''k'') languages is properly contained in that of LL(''k''+1) languages, for each ''k'' ≥ 0. A corollary of this is that not all context-free languages can be recognized by an LL(''k'') parser. An LL parser is called LL-regular (LLR) if it parses an LL-regular language. The class of LLR grammars contains every LL(k) grammar for every k. For every LLR grammar there exists an LLR parser that parses the grammar in linear time. Two nomenclative outlier parser types are LL(*) and LL(finite). A parser is called LL(*)/LL(finite) if it uses the LL(*)/LL(finite) parsing strategy. LL(*) and LL(finite) parsers are functionally more closely resemblant to PEG parsers. An LL(finite) parser can parse an arbitrary LL(k) grammar optimally in the amount of lookahead and lookahead comparisons. The class of grammars parsable by the LL(*) strategy encompasses some context-sensitive languages due to the use of syntactic and semantic predicates and has not been identified. It has been suggested that LL(*) parsers are better thought of as TDPL parsers. Against the popular misconception, LL(*) parsers are not LLR in general, and are guaranteed by construction to perform worse on average (super-linear against linear time) and far worse in the worst-case (exponential against linear time). LL grammars, particularly LL(1) grammars, are of great practical interest, as parsers for these grammars are easy to construct, and many
computer language A computer language is a formal language used to communicate with a computer. Types of computer languages include: * Construction language – all forms of communication by which a human can specify an executable problem solution to a compu ...
s are designed to be LL(1) for this reason. LL parsers may be table-based, i.e. similar to
LR parser In computer science, LR parsers are a type of bottom-up parser that analyse deterministic context-free languages in linear time. There are several variants of LR parsers: SLR parsers, LALR parsers, Canonical LR(1) parsers, Minimal LR(1) parse ...
s, but LL grammars can also be parsed by
recursive descent parser In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure implements one of the nonterminals of the grammar. Thus t ...
s. According to Waite and Goos (1984), LL(''k'') grammars were introduced by Stearns and Lewis (1969).


Overview

For a given context-free grammar, the parser attempts to find the
leftmost derivation In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
. Given an example grammar G: # S \to E # E \to ( E + E ) # E \to i the leftmost derivation for w = ((i+i)+i) is: :S\ \overset\ E\ \overset\ (E+E)\ \overset\ ((E+E)+E)\ \overset\ ((i+E)+E)\ \overset\ ((i+i)+E)\ \overset\ ((i+i)+i) Generally, there are multiple possibilities when selecting a rule to expand the leftmost non-terminal. In step 2 of the previous example, the parser must choose whether to apply rule 2 or rule 3: :S\ \overset\ E\ \overset\ ? To be efficient, the parser must be able to make this choice deterministically when possible, without backtracking. For some grammars, it can do this by peeking on the unread input (without reading). In our example, if the parser knows that the next unread symbol is ( , the only correct rule that can be used is 2. Generally, an LL(k) parser can look ahead at k symbols. However, given a grammar, the problem of determining if there exists a LL(k) parser for some k that recognizes it is undecidable. For each k, there is a language that cannot be recognized by an LL(k) parser, but can be by an LL(k+1). We can use the above analysis to give the following formal definition: Let G be a context-free grammar and k \ge 1. We say that G is LL(k), if and only if for any two leftmost derivations: # S\ \Rightarrow\ \cdots\ \Rightarrow\ wA\alpha\ \Rightarrow\ \cdots\ \Rightarrow\ w\beta\alpha\ \Rightarrow\ \cdots\ \Rightarrow\ wu # S\ \Rightarrow\ \cdots\ \Rightarrow\ wA\alpha\ \Rightarrow\ \cdots\ \Rightarrow\ w\gamma\alpha\ \Rightarrow\ \cdots\ \Rightarrow\ wv the following condition holds: the prefix of the string u of length k equals the prefix of the string v of length k implies \beta\ =\ \gamma. In this definition, S is the start symbol and A any non-terminal. The already derived input w, and yet unread u and v are strings of terminals. The Greek letters \alpha, \beta and \gamma represent any string of both terminals and non-terminals (possibly empty). The prefix length corresponds to the lookahead buffer size, and the definition says that this buffer is enough to distinguish between any two derivations of different words.


Parser

The LL(k) parser is a
deterministic pushdown automaton In automata theory, a deterministic pushdown automaton (DPDA or DPA) is a variation of the pushdown automaton. The class of deterministic pushdown automata accepts the deterministic context-free languages, a proper subset of context-free languages. ...
with the ability to peek on the next k input symbols without reading. This peek capability can be emulated by storing the lookahead buffer contents in the finite state space, since both buffer and input alphabet are finite in size. As a result, this does not make the automaton more powerful, but is a convenient abstraction. The stack alphabet is \Gamma = N \cup \Sigma, where: * N is the set of non-terminals; * \Sigma the set of terminal (input) symbols with a special end-of-input (EOI) symbol \$. The parser stack initially contains the starting symbol above the EOI: S\ \$\ /math>. During operation, the parser repeatedly replaces the symbol X on top of the stack: * with some \alpha, if X \in N and there is a rule X \to \alpha; * with \epsilon (in some notations \lambda), i.e. X is popped off the stack, if X \in \Sigma. In this case, an input symbol x is read and if x \neq X, the parser rejects the input. If the last symbol to be removed from the stack is the EOI, the parsing is successful; the automaton accepts via an empty stack. The states and the transition function are not explicitly given; they are specified (generated) using a more convenient ''parse table'' instead. The table provides the following mapping: * row: top-of-stack symbol X * column: , w, \le k lookahead buffer contents * cell: rule number for X \to \alpha or \epsilon If the parser cannot perform a valid transition, the input is rejected (empty cells). To make the table more compact, only the non-terminal rows are commonly displayed, since the action is the same for terminals.


Concrete example


Set up

To explain an LL(1) parser's workings we will consider the following small LL(1) grammar: # S → F # S → ( S + F ) # F → a and parse the following input: :( a + a ) An LL(1) parsing table for a grammar has a row for each of the non-terminals and a column for each terminal (including the special terminal, represented here as $, that is used to indicate the end of the input stream). Each cell of the table may point to at most one rule of the grammar (identified by its number). For example, in the parsing table for the above grammar, the cell for the non-terminal 'S' and terminal '(' points to the rule number 2: : The algorithm to construct a parsing table is described in a later section, but first let's see how the parser uses the parsing table to process its input.


Parsing procedure

In each step, the parser reads the next-available symbol from the input stream, and the top-most symbol from the stack. If the input symbol and the stack-top symbol match, the parser discards them both, leaving only the unmatched symbols in the input stream and on the stack. Thus, in its first step, the parser reads the input symbol '(' and the stack-top symbol 'S'. The parsing table instruction comes from the column headed by the input symbol '(' and the row headed by the stack-top symbol 'S'; this cell contains '2', which instructs the parser to apply rule (2). The parser has to rewrite 'S' to '( S + F )' on the stack by removing 'S' from stack and pushing ')', 'F', '+', 'S', '(' onto the stack, and this writes the rule number 2 to the output. The stack then becomes: (, S, +, F, ), $ In the second step, the parser removes the '(' from its input stream and from its stack, since they now match. The stack now becomes: S, +, F, ), $ Now the parser has an 'a' on its input stream and an 'S' as its stack top. The parsing table instructs it to apply rule (1) from the grammar and write the rule number 1 to the output stream. The stack becomes: F, +, F, ), $ The parser now has an 'a' on its input stream and an 'F' as its stack top. The parsing table instructs it to apply rule (3) from the grammar and write the rule number 3 to the output stream. The stack becomes: a, +, F, ), $ The parser now has an 'a' on the input stream and an 'a' at its stack top. Because they are the same, it removes it from the input stream and pops it from the top of the stack. The parser then has an '+' on the input stream and '+' is at the top of the stack meaning, like with 'a', it is popped from the stack and removed from the input stream. This results in: F, ), $ In the next three steps the parser will replace 'F' on the stack by 'a', write the rule number 3 to the output stream and remove the 'a' and ')' from both the stack and the input stream. The parser thus ends with '$' on both its stack and its input stream. In this case the parser will report that it has accepted the input string and write the following list of rule numbers to the output stream: : 2, 1, 3, 3 This is indeed a list of rules for a
leftmost derivation In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
of the input string, which is: : S → ( S + F ) → ( F + F ) → ( a + F ) → ( a + a )


Parser implementation in C++

Below follows a C++ implementation of a table-based LL parser for the example language: #include #include #include enum Symbols ; /* Converts a valid token to the corresponding terminal symbol */ Symbols lexer(char c) int main(int argc, char **argv)


Parser implementation in Python

# All constants are indexed from 0 TERM = 0 RULE = 1 # Terminals T_LPAR = 0 T_RPAR = 1 T_A = 2 T_PLUS = 3 T_END = 4 T_INVALID = 5 # Non-Terminals N_S = 0 N_F = 1 # Parse table table = 1, -1, 0, -1, -1, -1 1, -1, 2, -1, -1, -1 RULES = (RULE, N_F) TERM, T_LPAR), (RULE, N_S), (TERM, T_PLUS), (RULE, N_F), (TERM, T_RPAR) TERM, T_A) stack = TERM, T_END), (RULE, N_S) def lexical_analysis(inputstring): print("Lexical analysis") tokens = [] for c in inputstring: if c

"+": tokens.append(T_PLUS) elif c

"(": tokens.append(T_LPAR) elif c

")": tokens.append(T_RPAR) elif c

"a": tokens.append(T_A) else: tokens.append(T_INVALID) tokens.append(T_END) print(tokens) return tokens def syntactic_analysis(tokens): print("Syntactic analysis") position = 0 while len(stack) > 0: (stype, svalue) = stack.pop() token = tokens osition if stype

TERM: if svalue

token: position += 1 print("pop", svalue) if token

T_END: print("input accepted") else: print("bad term on input:", token) break elif stype

RULE: print("svalue", svalue, "token", token) rule = table
value Value or values may refer to: Ethics and social * Value (ethics) wherein said concept may be construed as treating actions themselves as abstract objects, associating value to them ** Values (Western philosophy) expands the notion of value beyo ...
token] print("rule", rule) for r in reversed(RULES
ule Ule is a German surname Personal names in German-speaking Europe consist of one or several given names (''Vorname'', plural ''Vornamen'') and a surname (''Nachname, Familienname''). The ''Vorname'' is usually gender-specific. A name is usually ci ...
: stack.append(r) print("stack", stack) inputstring = "(a+a)" syntactic_analysis(lexical_analysis(inputstring))


Remarks

As can be seen from the example, the parser performs three types of steps depending on whether the top of the stack is a nonterminal, a terminal or the special symbol $: * If the top is a nonterminal then the parser looks up in the parsing table, on the basis of this nonterminal and the symbol on the input stream, which rule of the grammar it should use to replace nonterminal on the stack. The number of the rule is written to the output stream. If the parsing table indicates that there is no such rule then the parser reports an error and stops. * If the top is a terminal then the parser compares it to the symbol on the input stream and if they are equal they are both removed. If they are not equal the parser reports an error and stops. * If the top is $ and on the input stream there is also a $ then the parser reports that it has successfully parsed the input, otherwise it reports an error. In both cases the parser will stop. These steps are repeated until the parser stops, and then it will have either completely parsed the input and written a
leftmost derivation In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
to the output stream or it will have reported an error.


Constructing an LL(1) parsing table

In order to fill the parsing table, we have to establish what grammar rule the parser should choose if it sees a nonterminal ''A'' on the top of its stack and a symbol ''a'' on its input stream. It is easy to see that such a rule should be of the form ''A'' → ''w'' and that the language corresponding to ''w'' should have at least one string starting with ''a''. For this purpose we define the ''First-set'' of ''w'', written here as Fi(''w''), as the set of terminals that can be found at the start of some string in ''w'', plus ε if the empty string also belongs to ''w''. Given a grammar with the rules ''A''1 → ''w''1, …, ''A''''n'' → ''w''''n'', we can compute the Fi(''w''''i'') and Fi(''A''''i'') for every rule as follows: # initialize every Fi(''A''''i'') with the empty set # add Fi(''w''''i'') to Fi(''A''''i'') for every rule ''A''''i'' → ''w''i, where Fi is defined as follows: #* Fi(''aw) = for every terminal ''a'' #* Fi(''Aw) = Fi(''A'') for every nonterminal ''A'' with ε not in Fi(''A'') #* Fi(''Aw' '') = (Fi(''A'') \ ) ∪ Fi(''w' '') for every nonterminal ''A'' with ε in Fi(''A'') #* Fi(ε) = # add Fi(''w''''i'') to Fi(''A''''i'') for every rule ''A''''i'' → ''w''''i'' # do steps 2 and 3 until all Fi sets stay the same. The result is the least fixed point solution to the following system: * Fi(''A'') ⊇ Fi(''w'') for each rule A → ''w'' * Fi(''a'') ⊇ , for each terminal ''a'' * Fi(''w''0 ''w''1) ⊇ Fi(''w''0)·Fi(''w1''), for all words ''w''0 and ''w''1 * Fi(ε) ⊇ where, for sets of words U and V, the truncated product is defined by U \cdot V = \, and w:1 denotes the initial length-1 prefix of words w of length 2 or more, or w, itself, if w has length 0 or 1. Unfortunately, the First-sets are not sufficient to compute the parsing table. This is because a right-hand side ''w'' of a rule might ultimately be rewritten to the empty string. So the parser should also use the rule ''A'' → ''w'' if ε is in Fi(''w'') and it sees on the input stream a symbol that could follow ''A''. Therefore, we also need the ''Follow-set'' of ''A'', written as Fo(''A'') here, which is defined as the set of terminals ''a'' such that there is a string of symbols ''αAaβ'' that can be derived from the start symbol. We use $ as a special terminal indicating end of input stream, and ''S'' as start symbol. Computing the Follow-sets for the nonterminals in a grammar can be done as follows: # initialize Fo(''S'') with and every other Fo(''A''''i'') with the empty set # if there is a rule of the form ''A''''j'' → ''wAiw' '', then #* if the terminal ''a'' is in Fi(''w' ''), then add ''a'' to Fo(''A''''i'') #* if ε is in Fi(''w' ''), then add Fo(''A''''j'') to Fo(''A''''i'') #* if ''w' '' has length 0, then add Fo(''A''''j'') to Fo(''A''''i'') # repeat step 2 until all Fo sets stay the same. This provides the least fixed point solution to the following system: * Fo(''S'') ⊇ * Fo(''A'') ⊇ Fi(''w'')·Fo(''B'') for each rule of the form B → ... A w Now we can define exactly which rules will appear where in the parsing table. If ''T'' 'A'', ''a''denotes the entry in the table for nonterminal ''A'' and terminal ''a'', then : ''T'' 'A'',''a''contains the rule ''A'' → ''w'' if and only if :: ''a'' is in Fi(''w'') or :: ε is in Fi(''w'') and ''a'' is in Fo(''A''). Equivalently: ''T'' 'A'', ''a''contains the rule ''A'' → ''w'' for each ''a'' ∈ Fi(''w'')·Fo(''A''). If the table contains at most one rule in every one of its cells, then the parser will always know which rule it has to use and can therefore parse strings without backtracking. It is in precisely this case that the grammar is called an ''LL''(1) ''grammar''.


Constructing an LL(''k'') parsing table

The construction for LL(1) parsers can be adapted to LL(''k'') for ''k'' > 1 with the following modifications: * the truncated product is defined U \cdot V = \, where w:k denotes the initial length-k prefix of words of length > k, or w, itself, if w has length k or less, * Fo(''S'') = * Apply Fi(αβ) = Fi(α)\cdotFi(β) also in step 2 of the Fi construction given for LL(1). * In step 2 of the Fo construction, for ''Aj'' → ''wAiw''' simply add Fi(''w''')\cdotFo(''Aj'') to Fo(''Ai''). where an input is suffixed by k end-markers $, to fully account for the k lookahead context. This approach eliminates special cases for ε, and can be applied equally well in the LL(1) case. Until the mid-1990s, it was widely believed that LL(''k'') parsing (for ''k'' > 1) was impractical, since the parser table would have
exponential Exponential may refer to any of several mathematical topics related to exponentiation, including: *Exponential function, also: **Matrix exponential, the matrix analogue to the above * Exponential decay, decrease at a rate proportional to value *Exp ...
size in ''k'' in the worst case. This perception changed gradually after the release of the Purdue Compiler Construction Tool Set around 1992, when it was demonstrated that many
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s can be parsed efficiently by an LL(''k'') parser without triggering the worst-case behavior of the parser. Moreover, in certain cases LL parsing is feasible even with unlimited lookahead. By contrast, traditional parser generators like
yacc Yacc (Yet Another Compiler-Compiler) is a computer program for the Unix operating system developed by Stephen C. Johnson. It is a Look Ahead Left-to-Right Rightmost Derivation (LALR) parser generator, generating a LALR parser (the part of a com ...
use LALR(1) parser tables to construct a restricted
LR parser In computer science, LR parsers are a type of bottom-up parser that analyse deterministic context-free languages in linear time. There are several variants of LR parsers: SLR parsers, LALR parsers, Canonical LR(1) parsers, Minimal LR(1) parse ...
with a fixed one-token lookahead.


Conflicts

As described in the introduction, LL(1) parsers recognize languages that have LL(1) grammars, which are a special case of context-free grammars; LL(1) parsers cannot recognize all context-free languages. The LL(1) languages are a proper subset of the LR(1) languages, which in turn are a proper subset of all context-free languages. In order for a context-free grammar to be an LL(1) grammar, certain conflicts must not arise, which we describe in this section.


Terminology

Let ''A'' be a non-terminal. FIRST(''A'') is (defined to be) the set of terminals that can appear in the first position of any string derived from ''A''. FOLLOW(''A'') is the union over: # FIRST(''B'') where ''B'' is any non-terminal that immediately follows ''A'' in the right-hand side of a production rule. # FOLLOW(''B'') where ''B'' is any head of a rule of the form ''B'' → ''wA''.


LL(1) conflicts

There are two main types of LL(1) conflicts:


FIRST/FIRST conflict

The FIRST sets of two different grammar rules for the same non-terminal intersect. An example of an LL(1) FIRST/FIRST conflict: S -> E , E 'a' E -> 'b' , ε FIRST(''E'') = and FIRST(''E'' ''a'') = , so when the table is drawn, there is conflict under terminal ''b'' of production rule ''S''.


= Special case: left recursion

=
Left recursion In the formal language theory of computer science, left recursion is a special case of recursion where a string is recognized as part of a language by the fact that it decomposes into a string from that same language (on the left) and a suffix (on ...
will cause a FIRST/FIRST conflict with all alternatives. E -> E '+' term , alt1 , alt2


FIRST/FOLLOW conflict

The FIRST and FOLLOW set of a grammar rule overlap. With an
empty string In formal language theory, the empty string, or empty word, is the unique string of length zero. Formal theory Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special cas ...
(ε) in the FIRST set, it is unknown which alternative to select. An example of an LL(1) conflict: S -> A 'a' 'b' A -> 'a' , ε The FIRST set of ''A'' is , and the FOLLOW set is .


Solutions to LL(1) conflicts


Left factoring

A common left-factor is "factored out". A -> X , X Y Z becomes A -> X B B -> Y Z , ε Can be applied when two alternatives start with the same symbol like a FIRST/FIRST conflict. Another example (more complex) using above FIRST/FIRST conflict example: S -> E , E 'a' E -> 'b' , ε becomes (merging into a single non-terminal) S -> 'b' , ε , 'b' 'a' , 'a' then through left-factoring, becomes S -> 'b' E , E E -> 'a' , ε


Substitution

Substituting a rule into another rule to remove indirect or FIRST/FOLLOW conflicts. Note that this may cause a FIRST/FIRST conflict.


Left recursion removal

See.Modern Compiler Design
Grune, Bal, Jacobs and Langendoen For a general method, see removing left recursion. A simple example for left recursion removal: The following production rule has left recursion on E E -> E '+' T E -> T This rule is nothing but list of Ts separated by '+'. In a regular expression form T ('+' T)*. So the rule could be rewritten as E -> T Z Z -> '+' T Z Z -> ε Now there is no left recursion and no conflicts on either of the rules. However, not all context-free grammars have an equivalent LL(k)-grammar, e.g.: S -> A , B A -> 'a' A 'b' , ε B -> 'a' B 'b' 'b' , ε It can be shown that there does not exist any LL(k)-grammar accepting the language generated by this grammar.


See also

*
Comparison of parser generators This is a list of notable lexer generators and parser generators for various language classes. Regular languages Regular languages are a category of languages (sometimes termed Chomsky Type 3) which can be matched by a state machine (more sp ...
*
Parse tree A parse tree or parsing tree or derivation tree or concrete syntax tree is an ordered, rooted tree that represents the syntactic structure of a string according to some context-free grammar. The term ''parse tree'' itself is used primarily in co ...
*
Top-down parsing Top-down parsing in computer science is a parsing strategy where one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar. LL parsers are a type of parser that uses a top- ...
*
Bottom-up parsing In computer science, parsing reveals the grammatical structure of linear input text, as a first step in working out its meaning. Bottom-up parsing recognizes the text's lowest-level small details first, before its mid-level structures, and leavin ...


Notes


External links


A tutorial on implementing LL(1) parsers in C# (archived)

Parsing Simulator
This simulator is used to generate parsing tables LL(1) and to resolve the exercises of the book.
LL(1) DSL PEG parser (toolkit framework)

Language theoretic comparison of LL and LR grammars


{{DEFAULTSORT:Ll Parser Parsing algorithms Articles with example C++ code Articles with example Python (programming language) code