11.07.2015 Views

Recursive descent parsing for grammars with contexts

Recursive descent parsing for grammars with contexts

Recursive descent parsing for grammars with contexts

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

39th International Conference on Current Trendsin Theory and Practice of Computer ScienceŠpindleruv Mlýn, Czech Republic<strong>Recursive</strong> <strong>descent</strong> <strong>parsing</strong><strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>Mikhail BarashPh.D. student,Department of Mathematics and Statistics, University of TurkuTurku Centre <strong>for</strong> Computer Science (TUCS)FinlandJanuary 26–31, 2013Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


What a recursive <strong>descent</strong> is?subclass of <strong>grammars</strong> allowing recursive <strong>descent</strong>:LL(k)-<strong>grammars</strong>k look-ahead symbolsMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


What a recursive <strong>descent</strong> is?subclass of <strong>grammars</strong> allowing recursive <strong>descent</strong>:LL(k)-<strong>grammars</strong>k look-ahead symbolsLL(1) context-free grammar generating { a n b n | n 0 }:S → aSb | ε★S() { a() {if (look-ahead is “a”) { if (current symbol is “a”)a(); S(); b(); advance position by 1} else error} }✧✥✦Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


What a recursive <strong>descent</strong> is?subclass of <strong>grammars</strong> allowing recursive <strong>descent</strong>:LL(k)-<strong>grammars</strong>k look-ahead symbolsLL(1) context-free grammar generating { a n b n | n 0 }:S → aSb | ε★S() { a() {if (look-ahead is “a”) { if (current symbol is “a”)a(); S(); b(); advance position by 1} else error} }✧First compilers <strong>for</strong> Pascal are recursive <strong>descent</strong> parsers.Implemented “by hand” (N. Wirth, 1970).Program code can be easily generated automatically.✥✦Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Extension of CFGs <strong>with</strong> Boolean operationsConjunctive <strong>grammars</strong> (Okhotin, 2001)A → α 1 & . . . & α k ,α i ∈ (Σ ∪ N) ∗Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Extension of CFGs <strong>with</strong> Boolean operationsConjunctive <strong>grammars</strong> (Okhotin, 2001)A → α 1 & . . . & α k , α i ∈ (Σ ∪ N) ∗“a string w has property A ⇐⇒ w has all the properties α 1, . . . , α k ”Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Extension of CFGs <strong>with</strong> Boolean operationsConjunctive <strong>grammars</strong> (Okhotin, 2001)A → α 1 & . . . & α k ,α i ∈ (Σ ∪ N) ∗“a string w has property A ⇐⇒ w has all the properties α 1, . . . , α k ”{ a n b n c n | n 0 }S → AB & DCA → aA | ε B → bBc | εC → cC | ε D → aDb | εMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Extension of CFGs <strong>with</strong> Boolean operationsConjunctive <strong>grammars</strong> (Okhotin, 2001)A → α 1 & . . . & α k ,α i ∈ (Σ ∪ N) ∗“a string w has property A ⇐⇒ w has all the properties α 1, . . . , α k ”{ a n b n c n | n 0 }S → AB & DCA → aA | ε B → bBc | εC → cC | ε D → aDb | εnon-context-free languages generatedcomplexity of basic <strong>parsing</strong> algorithms preservednontrivial properties of subclassesMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α kMikhail Barash <strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β mMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines wMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”{a n b n c} ∪ {a n b 2n d}Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”{a n b n c} ∪ {a n b 2n d}S → Ac | BdA → aAb | ε L(A) = {a n b n }B → aBbb | ε L(B) = {a n b 2n }Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”{a n b n c} ∪ {a n b 2n d}S → Ac | BdA → aAb | ε L(A) = {a n b n }B → aBbb | ε L(B) = {a n b 2n }the language is not standard LL(k) <strong>for</strong> any kMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”{a n b n c} ∪ {a n b 2n d}S → Ac | BdA → aAb | ε L(A) = {a n b n }B → aBbb | ε L(B) = {a n b 2n }the language is not standard LL(k) <strong>for</strong> any kway out: use right <strong>contexts</strong> to peek the last symbol of the stringMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Grammars <strong>with</strong> <strong>contexts</strong> (B, Okhotin, 2012)A → α 1 & . . . & α k & ✄β 1 & . . . & ✄β m & γ 1 & . . . & γ nGiven a string x = u w v:each α i defines weach β i defines v (the right context of w)each γ i defines wv (the extended right context of w)Semantics by logical deduction of elementary propositions[X , 〈w〉v] : “a string w written in right context v has the property X ”{a n b n c} ∪ {a n b 2n d}S → Ac | BdA → aAb | ε L(A) = {a n b n }B → aBbb | ε L(B) = {a n b 2n }X → aX | bX | ε L(X ) = (a | b) ∗the language is not standard LL(k) <strong>for</strong> any kway out: use right <strong>contexts</strong> to peek the last symbol of the stringMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:conjunction in rules: scan the substring multiple timesMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Theory:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialmathematically sound definition of right <strong>contexts</strong>(“look-ahead”) <strong>with</strong>in recursive <strong>descent</strong>Mikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Theory:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialmathematically sound definition of right <strong>contexts</strong>(“look-ahead”) <strong>with</strong>in recursive <strong>descent</strong>higher expressive power, but still linear timeMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Theory:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialmathematically sound definition of right <strong>contexts</strong>(“look-ahead”) <strong>with</strong>in recursive <strong>descent</strong>higher expressive power, but still linear time<strong>parsing</strong> algorithm proven correctMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Theory:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialmathematically sound definition of right <strong>contexts</strong>(“look-ahead”) <strong>with</strong>in recursive <strong>descent</strong>Practice:higher expressive power, but still linear time<strong>parsing</strong> algorithm proven correctimplemented as a prototype softwareMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>


Results<strong>Recursive</strong> <strong>descent</strong> parser:Theory:conjunction in rules: scan the substring multiple timesright <strong>contexts</strong> (✄, ): to choose suitable alternatives of rulesbacktracking: to go over the alternativesmemoization: linear time instead of exponentialmathematically sound definition of right <strong>contexts</strong>(“look-ahead”) <strong>with</strong>in recursive <strong>descent</strong>Practice:higher expressive power, but still linear time<strong>parsing</strong> algorithm proven correctimplemented as a prototype softwareMikhail Barash<strong>Recursive</strong> <strong>descent</strong> <strong>for</strong> <strong>grammars</strong> <strong>with</strong> <strong>contexts</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!