0% found this document useful (0 votes)
53 views5 pages

Top Down Parsing Summary

This document summarizes top-down parsing and LL(1) parsing. It discusses calculating first and follow sets for grammars with lambda productions. An algorithm is provided for building LL(1) parse tables from grammars based on the first and follow sets. Finally, it demonstrates using the parse table to parse input strings using a stack-based approach in tabular form.

Uploaded by

resshiya
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)
53 views5 pages

Top Down Parsing Summary

This document summarizes top-down parsing and LL(1) parsing. It discusses calculating first and follow sets for grammars with lambda productions. An algorithm is provided for building LL(1) parse tables from grammars based on the first and follow sets. Finally, it demonstrates using the parse table to parse input strings using a stack-based approach in tabular form.

Uploaded by

resshiya
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/ 5

Summary: Topdown Parsing

1 Basic Concepts
1.1 Calculating the First set of a symbol with lambda
productions
The First set for a nonterminal is the set of terminals which could
potentially be the first token on the left of the nonterminals
structure.
Previously we have seen how to calculate this, but now we extend
the approach to allow for lambda productions.
Where a is a terminal, First(a) = { a }
The First set of a nonterminal N can be found as follows:
1. Set First(N) = { }
2. For each production N :
symbols):

( corresponds to one or more

append First() to First(N)

1.2 FIRST of a SET of symbols


If is a set of symbols (terminals or nonterminals) where

= X1 X2 Xn
For terminal a: First(a) = { a }
First() can be calculated as follows:
1. Set i to 1
2. First() = { }
3. Add First(Xi) to First()
4. If I < n AND FIRST(Xi) includes :
a. Delete from First()
b. Increment i
c. Goto 3
Basically, First() is the First() set of the first symbol of , and
also of subsequent symbols where the first symbols can be
realised as lambda.
Example: Given the following grammar,
SA | B
AaAb | Bc
By |
First(B) is { y, }
First(A) is {a, y, c}
First(S) is {a, y, c, }
1

1.3 FOLLOW with Lambda


The algorithm given for calculating followers under bottom-up
parsing needs to be modified for grammars with lambdas.
The follower set of a given nonterminal N is derived as follows:
In each rule, where N occurs on the RHS,
1. Set Follow(N) to { }
2. For each occurrence of N in the RHS of a rule such that
P :- N ( and represent 0 or more symbols):
a. If is empty, append Follow(P) to Follow(N)
b. Else:
i. Add First() to Follow(N)
ii. If First() includes , delete and
append Follow(P) to Follow(N)

Example:Given the following grammar,


(0) S S $
(1) SAC
(2) C B |
(3) BbA |
Follow(S) = {$}
Follow(C) = Follow (S) = {$}
Follow(B) = Follow (C) = {$}
Follow(A) = (i) First(C) except = {c}
(ii) Follow(C) = {$} (because of the lambda in First(C))
(iii) Follow(B) = {$}
= {c, $}

2 LL(1) Parse Tables


2.1 Building the Parse Table

Rows correspond to Nonterminals


Columns correspond to possible input tokens, and $
The cells contain:
o The rule appropriate for the LHS and next token, or
o Nothing (indicates an error in parsing)

Algorithm:
For each production A:= of the grammar:
For each terminal symbol a First(),
Add the production A:= in the cell T[A,a]
2

If First(), for each terminal symbol b Follow(A),


add the production A:= in the cell T[A,b ].
Note that that b can be $.

Example:
For the following grammar:
E := TE
E := +TE
E :=
T := FT
T := *FT
T :=
F := (E)
F := id
Firsts and Follows:
First(F) = { (, id }
First(T) = { *, }
First(T) = First(F) = { (, id }
First(E) = { +, }
First(E) = First(T) = { (, id }

Follow(E) = { $, ) }
Follow(T) = Follow(T)
=
+Follow(E)
= {+, $, ) }

First(E)-

Result:
+

E := +TE

T := *FT

E :=
T := FT

T :=
F := (E)

E := TE

T := FT
T :=

id

E :=

T
T

E := TE

E
E

T :=
F := id

2.2 Defining an LL(1) Grammar


A grammar is LL(1) if the parse table produced by the previous
procedure has at most one rule in each cell.
A grammar need not be in Greibach Normal Form to be an LL(1)
grammar. But note that all extended GNF grammars (all
expansions of the same RHS are conditioned by a unique terminal)
are LL(1) grammars.

2.3 Using the LL(1) Parse Table


For analysis on paper, we use a table, showing one step of the
analysis in each line.
1. Draw up a table with 4 columns and as many rows as you
should need.
Step
1
2

Stack

Input

Action

2. The Analysis Stack is always initialised with 2 entries: $ and


the Start symbol. E.g., for a grammar with start symbol E:
Step Stack
1
$E
2

Input

Action

3. Initialise the Input Queue with the string to analyse,


terminated by $, e.g.
Step Stack
Input
1
$E
(id+id)$
2

Action

- The top of the stack is E, the next input is (


4. Repeat the following procedure:
a. Compare the symbol on top of the Stack (P) with the
next input symbol (S):
b. If equal:
If P==$, put Input Accepted in Action column and
stop.
Otherwise:
- put Advance in Action column,
- In next row,
repeat stack except last symbol
repeat input except first symbol
c. If different:
If cell(P, S) is empty, put Error in Action column and
stop.
Otherwise:
- the cell contains an expansion of P:- X1 X2
Xn.
- Place Apply $RULE in the Action column
- In the next row:
o repeat stack except replace last
symbol with its expansion, in reverse
order.
o repeat input unchanged.

Example:
Step Stack

Input

Action

$E

id+id$

Apply E::=TE

$ET

id+id$

Apply T::=FT

$ETF

id+id$

Apply F::=id

$ETid

id+id$

Advance

$ET

+id$

Apply T::=

$E

+id$

Apply E::=+TE

$ET+

+id$

Advance

$ET

id$

Apply T::=FT

$ETF

id$

Apply F::=id

10

$ETid

id$

Advance

11

$ET

Apply T::=

12

$E

Apply E::=

13

Input Accepted

You might also like