0% found this document useful (0 votes)
13 views

Lecture 8

This document discusses predictive parsing and LL(1) grammars. It contains the following key points: 1. Predictive parsing uses a parsing table constructed from a grammar's terminals, non-terminals, first sets and follow sets to determine parsing rules without recursion. 2. For a grammar to be LL(1), it cannot have left recursion, ambiguity, or be non-deterministic as defined by the parsing table. 3. Error handling for predictive parsing involves synchronizing on errors by looking for terminals in follow sets and popping the stack. Phrase-level recovery uses error routines pointed to in the parsing table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 8

This document discusses predictive parsing and LL(1) grammars. It contains the following key points: 1. Predictive parsing uses a parsing table constructed from a grammar's terminals, non-terminals, first sets and follow sets to determine parsing rules without recursion. 2. For a grammar to be LL(1), it cannot have left recursion, ambiguity, or be non-deterministic as defined by the parsing table. 3. Error handling for predictive parsing involves synchronizing on errors by looking for terminals in follow sets and popping the stack. Phrase-level recovery uses error routines pointed to in the parsing table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Compiler Construction

Lecture 8: Predictive
BITS Pilaniparsers
Hyderabad Campus
Predictive parsing table

BITS Pilani, Hyderabad Campus


Terminals First Set Follow Set
E (,id ),$
E’ +,ε ),$
T (,id +,), $
T’ *, ε +,), $
F (,id *,+,),$

BITS Pilani, Hyderabad Campus


Input Symbols
NT id + * ( ) $
E E →TE’ E →TE’
E’ E’ →+TE’ E’ → ε E’ → ε
T T →FT’ T →FT’
T’ T’ → ε T’ →*FT’ T’ → ε T’ → ε
F F->id F →(E)

What are the blanks? --> syntax errors

BITS Pilani, Hyderabad Campus


Is it possible to put 2 grammar rules
in the same box?
Terminals First Set Follow Set

BITS Pilani, Hyderabad Campus


Predictive parsing tables

➢ If a predictive parsing table constructed this way contains


no duplicate entries, the grammar is called LL(1)
➢Left-to-right parse,
➢Left-most derivation,
➢1 symbol lookahead

➢ If not, the grammar is not LL(1).

BITS Pilani, Hyderabad Campus


Rules for LL(1) Grammars

➢ Grammar must not have left-recursion

➢ Grammar must not be ambiguous

➢ Grammar must be predictive / deterministic

BITS Pilani, Hyderabad Campus


Table-driven Predictive parsing
(non-recursive) algorithm

BITS Pilani, Hyderabad Campus


Input Symbols
NT id + * ( ) $
E E →TE’ E →TE’
E’ E’ →+TE’ E’ → E’ →
T T →FT’ T →FT’ ε ε

T’ T’ → T’ →*FT’ T’ → T’ →
F F->id ε F →(E) ε ε

BITS Pilani, Hyderabad Campus


Predictive parsing (non-
recursive) algorithm

BITS Pilani, Hyderabad Campus


Error Handling

• Errors
• Terminal at top of stack ≠ terminal on input
• Variable A is top of stack and M[A, a] = no production
• The parser has to “recover” or synchronize itself
• If we just continue: many further senseless errors
• The generated error messages should possibly be
• Exact in meaning
• Exact in place (e.g. line, column)
• Find as many errors as possible in one run
• Avoid propagated errors

BITS Pilani, Hyderabad Campus


Panic mode
Input Symbols
NT id + * ( ) $
E E →TE’ E →TE’
E’ E’ →+TE’ E’ → ε E’ → ε
T T →FT’ T →FT’
T’ T’ → ε T’ →*FT’ T’ → ε T’ → ε
F F->id F →(E)

Terminals First Set Follow Set


E (,id ),$
E’ +,ε ),$
T (,id +,), $
T’ *, ε +,), $
F (,id *,+,),$

BITS Pilani, Hyderabad Campus


BITS Pilani, Hyderabad Campus
BITS Pilani, Hyderabad Campus
How to select
synchronizing set?
➢ Place all symbols in FOLLOW(A) into the synchronizing set for
nonterminal A. If we skip tokens until an element of FOLLOW(A) is
seen and pop A from the stack, it likely that parsing can continue.

➢ We might add keywords that begins statements to the


synchronizing sets for the nonterminals generating expressions.

➢ If a terminal on top of stack cannot be matched, a simple idea is


to pop the terminal, issue a message saying that the terminal was
inserted.

BITS Pilani, Hyderabad Campus


Phrase-level recovery
➢ Phrase-level error recovery is implemented by filling in the blank
entries in the predictive parsing table with pointers to error
routines.
➢ These routines may change, insert, or delete symbols on the
input and issue appropriate error messages. They may also pop
from the stack.
➢ Alteration of stack symbols or the pushing of new symbols onto
the stack is questionable for several reasons.
➢ Checking that any recovery action eventually results in an input
symbol being consumed (or the stack being shortened if the end
of the input has been reached) is a good way to protect against
such loops.

BITS Pilani, Hyderabad Campus


Thank you

BITS Pilani, Hyderabad Campus

You might also like