Upload
syma-masood
View
222
Download
0
Embed Size (px)
Citation preview
8/4/2019 LL11013
1/18
LL(1) Parser
8/4/2019 LL11013
2/18
What does LL signify ?The first L means that the scanning takes placefrom Left to right.The second L means that the Left derivation is
produced first.
The prime requirements are : - Stack
Parsing Table Input buffer Parsing program .
8/4/2019 LL11013
3/18
Input buffer contains the string to be
parsed, followed by $ ,a symbol used toindicate end of the input string. The stackindicates a sequence of grammar symbols
with $ on the bottom,indicating bottom ofthe stack. Initially, the stack contains thestart symbol of the grammar on the top of$. The parsing table is a 2-D array M[A,a]where A is a nonterminal, and a is aterminal or the symbol $.
8/4/2019 LL11013
4/18
How to control the parser?
If X=a=$ , parser halts, string accepted.
If X=a !=$ , parser pops X, and advances theinput pointer to point to next input symbol.
If X is a non-terminal, the program consultsentry M[X,a] of the parsing table M. Replacethe top of stack(X) with production rulecorresponding to entry in table. If entry =
ERROR, call error recovery routine.
8/4/2019 LL11013
5/18
Algo for Construction of predictive
parsing table :
1. For each production Aa of grammar G, dosteps 2 and 3
2. For each terminal 'a' in FIRST(a) , add Aa inM[A,a].
3. If e is in FIRST(a) , add Aa to M[A,b] foreach terminal b in FOLLOW(A). If is inFIRST(a ) , and $ is in FOLLOW(A), then add
Aa to M[A,$]4. Make each undefined entry as ERROR, i.e.
An error entry.
8/4/2019 LL11013
6/18
Example:
Grammar
ETE'
E'+TE' |
T FT'
T'
*FT' | F(E) | id
( stands for epsilon)
8/4/2019 LL11013
7/18
First and Follow
Symbol FIRST FOLLOW
E (,id $,)
E +, $,)
T (,id +,$,)
T *, +,$,)
F (,id *,+,$,)
8/4/2019 LL11013
8/18
Building the table
Id + * ( ) $
E ETE ETE
E
E+TE E E
T TFT TFT
T
T T*FT T T
F Fid F(E)
8/4/2019 LL11013
9/18
Input=id+id*id
Stack Input buffer
$E id+id*id$
$E'T' Id+id*id$
$E'T'F Id+id*id$
$E'T'id Id+id*id$
$E'T' +id*id$$E' +id*id$
$E'T+ +id*id$
$E'T id*id$
8/4/2019 LL11013
10/18
Stack Input Buffer
$E'T'F id*id$$E'T'id id*id$
$E'T' *id$
$E'T'F* *id$$E'T'F id$
$E'T'id id$
$E'T' $$E' $
$ Accepted
8/4/2019 LL11013
11/18
Thus, we can easily construct an LL parsewith 1 lookahead. Since one look ahead is
involved, we also call it an LL(1) parser.There are grammars which may requiteLL(k) parsing.
For e.g. look at next example..
8/4/2019 LL11013
12/18
8/4/2019 LL11013
13/18
Parse Table
a b e i t $
S Sa SiEtSS
S S
SeS
S
E EbAmbiguity
8/4/2019 LL11013
14/18
The grammar is ambiguous and it isevident by the fact that we have two
entries corresponding to M[S,e]containing S and SeS. Thisambiguity can be resolved if we choose
SeS i.e associating the elses with theclosest previous then.
8/4/2019 LL11013
15/18
Note that the ambiguity will be solved ifwe use LL(2) parser, i.e. always see for
the two input symbols. How?
When input is e then it looks at next
input. Depending on the next input wechoose the appropriate rule.
8/4/2019 LL11013
16/18
LL(1) grammars have distinct properties. -No ambiguous grammar or left recursive
grammar can be LL(1).A grammar is LL(1) if and only if whenevera production A C | D the following
conditions hold:contd
8/4/2019 LL11013
17/18
1)For no terminal a both C and D derivestrings beginning with a.
Thus First(C) != First(D)
2)At most one of C or D can derive
3) If C* then D does not derive any stringbeginning with terminal Follow(A).
8/4/2019 LL11013
18/18
Prepared byMukesh Kumar B
04CS1013,CSE,IIT Kharagpur.
Visit:http://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.html#Lecture
http://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.htmlhttp://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.htmlhttp://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.htmlhttp://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.html