Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LL Parsing

3,597 views

Published on

Lecture on LL parsing for Compiler Construction course at TU Delft

Published in: Software
  • My only statement is "WOW"...I thought your other systems were special but this is going to turn out to be the "Holy Grail" of all MLB systems, no doubt! ◆◆◆ http://t.cn/A6zP2wH9
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: ❶❶❶ http://bit.ly/2Qu6Caa ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/2Qu6Caa ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y3nhqquc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

LL Parsing

  1. 1. Challenge the future Delft University of Technology Course IN4303 Compiler Construction Eduardo Souza, Guido Wachsmuth, Eelco Visser LL Parsing Traditional Parsing Algorithms
  2. 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
  3. 3. today’s lecture LL Parsing Overview 3
  4. 4. today’s lecture LL Parsing Overview efficient parsing algorithms • predictive/recursive descent parsing • LL parsing 3
  5. 5. today’s lecture LL Parsing Overview efficient parsing algorithms • predictive/recursive descent parsing • LL parsing grammar classes • LL(k) grammars • LR(k) grammars 3
  6. 6. today’s lecture LL Parsing Overview efficient parsing algorithms • predictive/recursive descent parsing • LL parsing grammar classes • LL(k) grammars • LR(k) grammars more top-down parsing algorithms • Parser Combinators • Parsing Expression Grammars • ALL(*) 3
  7. 7. LL Parsing Predictive (Recursive Descent) Parsing I 4
  8. 8. formal languages LL Parsing 5 Recap: A Theory of Language
  9. 9. formal languages LL Parsing vocabulary Σ finite, nonempty set of elements (words, letters) alphabet 5 Recap: A Theory of Language
  10. 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. 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
  12. 12. formal grammars LL Parsing 6 Recap: A Theory of Language
  13. 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. 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. 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. 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. 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
  18. 18. formal languages LL Parsing 7 Recap: A Theory of Language
  19. 19. formal languages LL Parsing formal grammar G = (N, Σ, P, S) 7 Recap: A Theory of Language
  20. 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. 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. 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
  23. 23. recursive descent LL Parsing 8 Exp → “while” Exp “do” Exp Predictive parsing public void parseExp() { consume(WHILE); parseExp(); consume(DO); parseExp(); }
  24. 24. look ahead LL Parsing 9 Exp → “while” Exp “do” Exp Exp → “if” Exp “then” Exp “else” Exp Predictive parsing public void parseExp() { switch current() { case WHILE: consume(WHILE); parseExp(); ...; break; case IF : consume(IF); parseExp(); ...; break; default : error(); } }
  25. 25. parse table LL Parsing 10 rows • nonterminal symbols N • symbol to parse columns • terminal symbols Σk • look ahead k entries • production rules P • possible conflicts Predictive parsing T1 T2 T3 ... N1 N1 →... N1 →... N2 N2 →... N3 N3 →... N3 →... N4 N4 →... N5 N5 →... N6 N6 →... N6 →... N7 N7 →... N8 N8 →... N8 →... N8 →... ...
  26. 26. automaton LL Parsing Predictive parsing 11 … tn $t1 $ S input parse table stack
  27. 27. automaton LL Parsing Predictive parsing 12 x … $ $ x …
  28. 28. automaton LL Parsing Predictive parsing 12 … $ $ …
  29. 29. automaton LL Parsing Predictive parsing 13 x … $ $ Xi x Xi Xi → Y1... Yk …
  30. 30. automaton LL Parsing Predictive parsing 13 x … $ $ x Xi Xi → Y1... Yk … Yk … Y1
  31. 31. LL Parsing LL Parse Tables II 14
  32. 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. 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. 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. 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
  36. 36. nullable LL Parsing nullable(X) (X, ε) ∈ P nullable(X) (X0, X1 … Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk) nullable(X0) nullable(w) nullable(ε) nullable(X1 … Xk) = nullable(X1) ∧ … ∧ nullable(Xk) 16 Predictive parsing
  37. 37. first sets LL Parsing FIRST(X) X∈Σ : FIRST(X) = {X} (X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi) FIRST(X0) ⊇ FIRST(Xi+1) FIRST(w) FIRST(ε) = {} ¬nullable(X) FIRST(Xw) = FIRST(X) nullable(X) FIRST(Xw) = FIRST(X) ∪ FIRST(w) 17 Predictive parsing
  38. 38. follow sets LL Parsing FOLLOW(X) (X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk) FOLLOW(Xi) ⊇ FOLLOW(X0) (X0, X1 … Xi … Xk)∈P FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk) 18 Predictive parsing
  39. 39. LL Parsing 19 Example nullable FIRST FOLLOW Start Exp Exp’ Term Term’ Fact p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)”
  40. 40. nullable LL Parsing 20 Example (X, ε) ∈ P nullable(X) (X0, X1 … Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk) nullable(X0) nullable FIRST FOLLOW Start Exp Exp’ Term Term’ Fact p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)”
  41. 41. nullable LL Parsing 21 Example (X, ε) ∈ P nullable(X) (X0, X1 … Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk) nullable(X0) p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” nullable FIRST FOLLOW Start no Exp no Exp’ yes Term no Term’ yes Fact no
  42. 42. FIRST sets LL Parsing 22 Example (X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi) FIRST(X0) ⊇ FIRST(Xi+1) p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” nullable FIRST FOLLOW Start no Exp no Exp’ yes Term no Term’ yes Fact no
  43. 43. FIRST sets LL Parsing 23 Example (X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi) FIRST(X0) ⊇ FIRST(Xi+1) p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” nullable FIRST FOLLOW Start no Num ( Exp no Num ( Exp’ yes + Term no Num ( Term’ yes * Fact no Num (
  44. 44. FOLLOW sets LL Parsing 24 Example (X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk) FOLLOW(Xi) ⊇ FOLLOW(X0) (X0, X1 … Xi … Xk)∈P FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk) nullable FIRST FOLLOW Start no Num ( Exp no Num ( Exp’ yes + Term no Num ( Term’ yes * Fact no Num ( p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)”
  45. 45. FOLLOW sets LL Parsing 25 Example (X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk) FOLLOW(Xi) ⊇ FOLLOW(X0) (X0, X1 … Xi … Xk)∈P FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk) p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” nullable FIRST FOLLOW Start no Num ( Exp no Num ( ) EOF Exp’ yes + ) EOF Term no Num ( + ) EOF Term’ yes * + ) EOF Fact no Num ( * + ) EOF
  46. 46. LL parse table LL Parsing 26 Example entry (X, w)∈P at row X and column T T∈ FIRST(w) nullable(w) ∧ T∈ FOLLOW(X) + * Num ( ) EOF Start Exp Exp’ Term Term’ Fact p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)”
  47. 47. LL parse table LL Parsing 27 Example entry (X, w)∈P at row X and column T T∈ FIRST(w) nullable(w) ∧ T∈ FOLLOW(X) p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” + * Num ( ) EOF Start p0 p0 Exp p1 p1 Exp’ p2 p3 p3 Term p4 p4 Term’ p6 p5 p6 p6 Fact p7 p8
  48. 48. parsing (4+3)*6 EOF LL Parsing 28 Example p0: Start → Exp EOF p1: Exp → Term Exp’ p2: Exp’ → “+” Term Exp’ p3: Exp’ → p4: Term → Fact Term’ p5: Term’ → “*” Fact Term’ p6: Term’ → p7: Fact → Num p8: Fact → “(” Exp “)” + * Num ( ) EOF Start p0 p0 Exp p1 p1 Exp’ p2 p3 p3 Term p4 p4 Term’ p6 p5 p6 p6 Fact p7 p8
  49. 49. LL Parsing Grammar classes 29 context-free grammars
  50. 50. LL Parsing Grammar classes 29 context-free grammars LL(0)
  51. 51. LL Parsing Grammar classes 29 context-free grammars LL(1) LL(0)
  52. 52. LL Parsing Grammar classes 29 context-free grammars LL(k) LL(1) LL(0)
  53. 53. encoding precedence LL Parsing 30 Exp → Num Exp → “(” Exp “)” Exp → Exp “*” Exp Exp → Exp “+” Exp Predictive parsing Fact → Num Fact → “(” Exp “)” Term → Term “*” Fact Term → Fact Exp → Exp “+” Term Exp → Term
  54. 54. eliminating left recursion LL Parsing 31 Term → Term “*” Fact Term → Fact Exp → Exp “+” Term Exp → Term Predictive parsing Term’ → “*” Fact Term’ Term’ → Term → Fact Term’ Exp’ → “+” Term Exp’ Exp’ → Exp → Term Exp’
  55. 55. left factoring LL Parsing 32 Exp → “if” Exp “then” Exp “else” Exp Exp → “if” Exp “then” Exp Predictive parsing Exp → “if” Exp “then” Exp Else Else → “else” Exp Else →
  56. 56. LL Parsing Parser Combinators III 33
  57. 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.
  58. 58. LL Parsing Parser Combinators 35 type Parser = String -> Tree
  59. 59. LL Parsing Parser Combinators 36 type Parser = String -> Tree What about intermediate parsers?
  60. 60. LL Parsing Parser Combinators 37 type Parser = String -> (Tree, String) type Parser = String -> Tree Return the remainder of the input!
  61. 61. LL Parsing Parser Combinators 38 type Parser = String -> (Tree, String) type Parser = String -> Tree WWhat about parsers that fail?
  62. 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. 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. 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. 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. 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.
  67. 67. LL Parsing Parsing Expression Grammars IV 44
  68. 68. LL Parsing Parsing Expression Grammars (PEGs) 45 A ← e Rules are of the form: where A is a non-terminal and e is a parsing expression.
  69. 69. LL Parsing PEG operators 46
  70. 70. LL Parsing Syntactic Predicates 47 &e and !e : preserves the knowledge whether e fails or not. Comment ← '//' (!EndOfLine .)* EndOfLine
  71. 71. LL Parsing Parsing Expression Grammars 48 S ← a b / a S ← a / a b • Prioritized choice '/'.
  72. 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.
  73. 73. LL Parsing ANTLR / ALL(*) V 50
  74. 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.
  75. 75. LL Parsing LL(*) Grammar Analysis 52 S : ID | ID = E | unsigned* int ID | unsigned* ID ID
  76. 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. 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. 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. 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. 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. 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.
  82. 82. LL Parsing Adaptive LL(*) 59 S : x B | y C B : A a C : A b a A : b | ε parse: yba
  83. 83. LL Parsing Adaptive LL(*) 60 S : x B | y C B : A a C : A b a A : b | ε input: yba stack: S
  84. 84. LL Parsing Adaptive LL(*) 61 S : x B | y C B : A a C : A b a A : b | ε input: yba stack: yC
  85. 85. LL Parsing Adaptive LL(*) 62 S : x B | y C B : A a C : A b a A : b | ε input: ba stack: C
  86. 86. LL Parsing Adaptive LL(*) 63 S : x B | y C B : A a C : A b a A : b | ε input: ba stack: A ba
  87. 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. 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. 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.
  90. 90. LL Parsing Summary VI 67
  91. 91. lessons learned LL Parsing Summary 68
  92. 92. lessons learned LL Parsing Summary How can we parse context-free languages effectively? • predictive parsing algorithms 68
  93. 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. 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. 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
  96. 96. learn more LL Parsing Literature 69
  97. 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. 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
  99. 99. learn more LL Parsing Literature 70
  100. 100. learn more LL Parsing Literature ALL(*) Terence John Parr, Sam Harwell, Kathleen Fisher. Adaptive LL(*) parsing: the power of dynamic analysis. In OOPSLA 2014. 70
  101. 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. 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
  103. 103. LL Parsing copyrights 71
  104. 104. LL Parsing 72
  105. 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

×