Docsity

Predictive Parsing Table - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:2-1
L
Le
ec
ct
tu
ur
re
e
1
19
9
LL(1) Table Construction
Here now is the algorithm to construct a predictive parsing table.
1. For each production A ? α
1. for each terminal a in FIRST(α), add A ? α to M[A,a].
2. If ε is in FIRST(α), add A ? α to M[A,b] for each terminal b in FOLLOW(A). If ε
is in FIRST(α), and $ is in FOLLOW(A), add A ? α to M[A,$].
2. Make each undefined entry of M be error.
Let us apply the algorithm to the expression grammar. Since FIRST(TE ') = FIRST(T )
= { (, id }, the production E ?TE' cause M[E,(] and M[E,id] to get E ?TE'. The
production E' ? +TE' causes M[E',+] to get E' ?+TE'. The production E' ? ε causes
M[E',)] and M[E',$] to get E' ? ε since FOLLOW (E' ) = { ), $ }. And so on. The final
parsing table produced is:
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 )
Left Factoring
Consider the grammar
E ? T + E | T
T ? int | int∗ T | (E)
It is impossible to predict because for T, two productions start with int. For E, it is not
clear how to predict; the two productions start with the non-terminal T. A grammar must
be left factored before use for predictive parsing. The procedure to left-factor a grammar
is as follows:
pf2

Partial preview of the text

Download Predictive Parsing Table - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:2-

Le Leccttuurree 1 199

LL(1) Table Construction

Here now is the algorithm to construct a predictive parsing table.

  1. For each production A? α
    1. for each terminal a in FIRST(α), add A? α to M[ A ,a].
    2. If ε is in FIRST(α), add A? α to M[ A ,b] for each terminal b in FOLLOW( A ). If ε is in FIRST(α), and $ is in FOLLOW( A ), add A? α to M[ A ,$].
  2. Make each undefined entry of M be error.

Let us apply the algorithm to the expression grammar. Since FIRST( TE ') = FIRST( T ) = { (, id }, the production E? TE' cause M[ E ,(] and M[ E ,id] to get E? TE'. The production E'? + TE' causes M[ E' ,+] to get E'? + TE'. The production E'? ε causes M[ E' ,)] and M[ E' ,$] to get E'? ε since FOLLOW ( E' ) = { ), $ }. And so on. The final parsing table produced is:

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 )

Left Factoring

Consider the grammar

E? T + E | T

T? int | int∗ T | (E)

It is impossible to predict because for T, two productions start with int. For E, it is not clear how to predict; the two productions start with the non-terminal T. A grammar must be left factored before use for predictive parsing. The procedure to left-factor a grammar is as follows:

Sohail Aslam Compiler Construction Notes Set:2-

If α ≠ ε , replace all productions

A? αβ 1 | αβ 2 | .... | αβn | γ with A? α Z | γ Z? β 1 | β 2 | .... | βn

where Z is a new non-terminal

A graphical explanation:

Example: consider following fragment of expression grammar

Factor? id | id [ ExprList ] | id ( ExprList )

After left factoring, the grammar becomes

Factor? id Args Args? [ ExprList ] | ( ExprList ) | ε

Given a CFG that does not meet the LL(1) condition, it is undecidable whether or not an equivalent LL(1) grammar exists.

A (^) αβ 2

αβ 1

αβ 3

β 2

β 1

β 3

A^ α Z