












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













































Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Mark Hills [email protected]
University of Illinois at Urbana-Champaign
June 23, 2008
(^1) Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Objectives and Review
Quick review
Parsing
Parser Generation with OCaml
Objectives and Review Quick review Parsing Parser Generation with OCaml
CFGs, Derivations, Parse Trees Example
Def: A Context-free Grammar (CFG) is a 4-tuple: G = (N, Σ, P, S) where:
Objectives and Review Quick review Parsing Parser Generation with OCaml
CFGs, Derivations, Parse Trees Example
◮ (^) A Derivation is a sequence of steps that, starting with the start symbol, leads to a sentence in the language (a string with only terminals). Each derivation step replaces one nonterminal with the right-hand side of one production. ◮ (^) A Parse Tree is a tree representation of a derivation, with nonterminals as nodes and either nonterminals or terminals as leaves (only terminals for a derivation, both while building the tree). A node’s children are based directly on the terminals and nonterminals on the right-hand side of the rule used to construct them.
Objectives and Review Quick review Parsing Parser Generation with OCaml
CFGs, Derivations, Parse Trees Example
exp
exp (^) + exp
exp (^) ∗ (^) term term
term
factor
3
factor
4
factor
5
Objectives and Review Quick review Parsing Parser Generation with OCaml
CFGs, Derivations, Parse Trees Example
G = (N, Σ, P, S) where:
Σ = {(, ), +, ∗, id} P = {E → T E → E + T T → F T → T ∗ F F → id F → (E )} S = E
Note: P ⊆ N × V∗, where V = N ∪ Σ = {E , T , F , (, ), +, ∗, id}
Note: (A, α) ∈ P is usually writ- ten: A → α or A ::= α or A : α
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
This algorithm, named after Cocke, Younger, and Kasami, is a general algorithm for testing membership in a context-free language. Advantage Algorithm works for any unambiguous grammar Disadvantage Running time is O(n^3 ), much too slow for practical use (n is the length of the input) For more information, see §7.4.4 in Hopcroft, Motwani, and Ullman.
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
LL parsing methods are faster than the CYK algorithm, but work only on grammars with some restrictions. LL stands for ◮ (^) L – Left-to-right scan of input ◮ (^) L – Leftmost derivation LL methods are categorized by the number of tokens lookahead needed. So, LL(1)-methods are left-to-right methods using a leftmost derivation and one token lookahead.
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
LL parsing is often called recursive-descent parsing or predictive parsing (predictive parsing is resursive-descent parsing without backtracking). ◮ (^) Most hand-written parsers are LL parsers, since they are easier to write than LR parsers ◮ (^) Few tools use LL techniques – the most notable is ANTLR, which works over LL(n) grammars (grammars that allow arbitrary lookahead) These are top-down parsing techniques, since they start by expanding the start symbol and then expand out nonterminals based on the input, building the parse tree from the top down.
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
FIRST sets specify the first terminals that can be part of a member of the vocabulary, based on the productions. Essentially, they tell us when an element of our language vocabulary is starting. From §4.4, Dragon book: ◮ (^) If X is a terminal, then FIRST(X ) is {X } ◮ (^) If X → ǫ is a production, add ǫ to FIRST(X ) ◮ (^) If X → Y 1 Y 2 · · · Yn is a production, add token a if a is in FIRST(Yj ) and for all i < j ǫ is in FIRST(Yi ); if for all i FIRST(Yi ) includes ǫ, add ǫ to FIRST(X )
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
With FIRST and FOLLOW sets, we can then construct PREDICT tables, which tell us which production we will process based on which we are currently processing and what the next input token is. We will build a table M of productions indexed by nonterminal and then terminal. From §4.4, Dragon book: ◮ (^) For each production A → α ◮ (^) For each terminal a in FIRST(α), add A → α to table entry M[A, a] ◮ (^) If ǫ is in FIRST(α) then add A → α to M[A, b] for all b in FOLLOW(A); if $ was also in FOLLOW(A), then add A → α to M[A, ǫ] ◮ (^) Each entry in the table containing no productions is defined as an error
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
Using the information from PREDICT, was can now hand-construct a predictive parser ◮ (^) For each production, create a new function ◮ (^) Inside this function, read each token and call the appropriate function based on the contents of the PREDICT table ◮ (^) Return from the function when the end of the production is reached – should return some data structure that is the representation of the AST
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
In cases where we have left recursion, we can remove it by modifying the grammar. See Algorithm 4.1 in §4.3 of the Dragon book for more information. Note that the resulting grammar is generally not as “clean” as the original, which is one of the disadvantages of LL parsing.
Objectives and ReviewQuick review Parsing Parser Generation with OCaml
Parsing Methods LL Parsing LR Parsing
What if we have two productions that both start identically?
stmt → if expr then stmt else stmt stmt → if expr then stmt
How do we know which to choose? We need to left factor the grammar – modify it so that we “factor out” matching left-hand portions of productions into a single production, adding multiple productions for the parts that are different. More details in §4.3 of the Dragon book. This ensures that the productions are pairwise disjoint – they do not have overlapping FIRST sets.