lec03-topdownparser

lec03-topdownparser - BİM454 Programming Language Concepts...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: BİM454 Programming Language Concepts 1 Top-Down Parsing • The parse tree is created top to bottom. • Top-down parser – Recursive-Descent Parsing • Backtracking is needed (If a choice of a production rule does not work, we backtrack to try other alternatives.) • It is a general parsing technique, but not widely used. • Not efficient – Predictive Parsing • no backtracking • efficient • needs a special form of grammars (LL(1) grammars). • Recursive Predictive Parsing is a special form of Recursive Descent parsing without backtracking. • Non-Recursive (Table Driven) Predictive Parser is also known as LL(1) parser. BİM454 Programming Language Concepts 2 Recursive-Descent Parsing (uses Backtracking) • Backtracking is needed. • It tries to find the left-most derivation. S → aBc B → bc | b S S input: abc a B c a B c b c b fails, backtrack BİM454 Programming Language Concepts 3 Predictive Parser a grammar a grammar suitable for predictive eliminate left parsing (a LL(1) grammar) left recursion factor no %100 guarantee. • When re-writing a non-terminal in a derivation step, a predictive parser can uniquely choose a production rule by just looking the current symbol in the input string. A → α 1 | ... | α n input: ... a ....... current token BİM454 Programming Language Concepts 4 Predictive Parser (example) stmt → if ...... | while ...... | begin ...... | for ..... • When we are trying to write the non-terminal stmt , if the current token is if we have to choose first production rule. • When we are trying to write the non-terminal stmt , we can uniquely choose the production rule by just looking the current token. • We eliminate the left recursion in the grammar, and left factor it. But it may not be suitable for predictive parsing (not LL(1) grammar). BİM454 Programming Language Concepts 5 Recursive Predictive Parsing • Each non-terminal corresponds to a procedure. Ex: A → aBb (This is only the production rule for A) proc A {- match the current token with a, and move to the next token;- call ‘B’;- match the current token with b, and move to the next token; } BİM454 Programming Language Concepts 6 Recursive Predictive Parsing (cont.) A → aBb | bAB proc A { case of the current token { ‘a’: - match the current token with a, and move to the next token;- call ‘B’;- match the current token with b, and move to the next token; ‘b’: - match the current token with b, and move to the next token;- call ‘A’;- call ‘B’; } } BİM454 Programming Language Concepts 7 Recursive Predictive Parsing (cont.) • When to apply ε-productions. A → aA | bB | ε • If all other productions fail, we should apply an ε-production. For example, if the current token is not a or b, we may apply the ε-production....
View Full Document

This note was uploaded on 10/20/2009 for the course CENG PLC taught by Professor Anyone during the Fall '09 term at Anadolu University.

Page1 / 31

lec03-topdownparser - BİM454 Programming Language Concepts...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online