lec03-topdownparser - Top-Down Parsing The parse tree is...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
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.
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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).
Image of page 4
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; }
Image of page 5

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

View Full Document Right Arrow Icon
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’; } }
Image of page 6
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. Most correct choice: We should apply an ε -production for a non- terminal A when the current token is in the follow set of A (which terminals can follow A in the sentential forms).
Image of page 7

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

View Full Document Right Arrow Icon
BİM454 Programming Language Concepts 8 Recursive Predictive Parsing (Example) A aBe | cBd | C B bB | ε C f proc C { match the current token with f, proc A {
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern