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

LL1 Parser Presentation

The document discusses LL(1) parsers. It defines that an LL(1) parser uses 1 token of lookahead when parsing sentences. LL(1) grammars are easy to construct and many computer languages are designed to be LL(1). The document then discusses the requirements for LL(1) grammars including no left factoring or left recursion. It explains the process of constructing a parsing table which involves calculating FIRST and FOLLOW sets and using these to determine the production rules based on the next input symbol. An example grammar and its corresponding FIRST and FOLLOW sets are provided and used to demonstrate how to fill the parsing table.

Uploaded by

M Fahad Imtiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

LL1 Parser Presentation

The document discusses LL(1) parsers. It defines that an LL(1) parser uses 1 token of lookahead when parsing sentences. LL(1) grammars are easy to construct and many computer languages are designed to be LL(1). The document then discusses the requirements for LL(1) grammars including no left factoring or left recursion. It explains the process of constructing a parsing table which involves calculating FIRST and FOLLOW sets and using these to determine the production rules based on the next input symbol. An example grammar and its corresponding FIRST and FOLLOW sets are provided and used to demonstrate how to fill the parsing table.

Uploaded by

M Fahad Imtiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

LL(1 PARSER

M Azhar) ( FA20-BCS-050 )
M Fahad Imtiaz ( FA20-BCS-053)
Imran Ali ( FA20-BCS-054 )
LL(1) PARSER 1

An LL parser is called an LL(k) parser if it


uses k tokens of look ahead when parsing a
sentence.

LL grammars, particularly LL(1) grammars,


as parsers are easy to construct, and many
computer languages are designed to be LL(1)
for this reason.

The 1 stands for using one input symbol of


look ahead at each step to make parsing
action decision.
2
LL(k) parsers must predict which production replace a non-terminal with as soon as they see
the non-terminal. The basic LL algorithm starts with a stack containing [S, $] (top to bottom)
and does whichever of the following is applicable until done:

▶ If the top of the stack is a non-terminal, replace the top of the stack with one of the
productions for that non-terminal, using the next k input symbols to decide which one
(without moving the input cursor), and continue.

▶ If the top of the stack is a terminal, read the next input token. If it is the same terminal,
pop the stack and continue. Otherwise, the parse has failed and the algorithm finishes.

▶ If the stack is empty, the parse has succeeded and the algorithm finishes. (We assume
that there is a unique EOF-marker $ at the end of the input.)

So look ahead meaning is - looking at input tokens without moving the input cursor.
PRIME REQUIREMENT OF LL(1) 3

 The grammar must be


- 
no left factoring
 no left recursion

 FIRST() & FOLLOW()


 Parsing Table
 Stack Implementation
 Parse Tree
4

STEP:FIRST & FOLLOW


5
Rules of FIRST
FIRST always find out the terminal symbol from the grammar. When
we check out FIRST for any symbol then if we find any terminal
symbol in first place then we take it. And not to see the next symbol.
 If a grammar is
A → a then FIRST ( A )={ a }
 If a grammar is
A → a B then FIRST ( A )={ a }
6
Rules of FIRST

 If a grammar is
A → aB ǀ ε then FIRST ( A )={ a, ε }
 If a grammar is

A → BcD ǀ εB
→ eD ǀ ( A )
Here B is non terminal. So, we check the transition of B
and find the FIRST of A.
then FIRST ( A )={ e,( , ε }
7
Rules of FOLLOW
For doing FOLLOW operation we need FIRST operation mostly. In FOLLOW we
use a $ sign for the start symbol. FOLLOW always check the right
portion of the symbol.
 If a grammar is
A → BAc ; A is start symbol.
Here firstly check if the selected symbol stays in right side of the grammar.
We see that c is right in A.
then FOLLOW (A) = {c , $ }
8

Rules of FOLLOW

 If a grammar is
A → BA’

A' →*Bc
Here we see that there is nothing at the right side of A' . So
FOLLOW ( A' )= FOLLOW ( A )= { $ }
Because A' follows the start symbol.
9

Rules of FOLLOW

 If a grammar is
A → BC
B → Td
C →*D ǀ ε
When we want to find FOLLOW (B),we see that B follows by C . Now put
the FIRST( C) in the there.
FIRST(C)={*, ε }.
But when the value is € it follows the parents symbol. So
FOLLOW(B)={*,$ }
10
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε
F -> (E)|id
11
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id }
F -> (E)|id
E'

T'

F
12
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id }
F -> (E)|id
E' {+,ε}

T'

F
13
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id }
F -> (E)|id
E' {+,ε}

T { id , ( }

T'

F
14
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id }
F -> (E)|id
E' {+,ε}

T { id , ( }

T' {*,ε }

F
15
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id }
F -> (E)|id
E' {+,ε}

T { id , ( }

T' {*,ε}

F { id , ( }
16
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id } {$,)}
F -> (E)|id
E' {+,ε}

T { id , ( }

T' {*,ε}

F { id , ( }
17
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id } {$,)}
F -> (E)|id
E' {+,ε} {$,)}

T { id , ( }

T' {*,ε }

F { id , ( }
18
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id } {$,)}
F -> (E)|id
E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε }

F { id , ( }
19
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id } {$,)}
F -> (E)|id
E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε} {$,),+}

F { id , ( }
20
Example of FIRST and FOLLOW

GRAMMAR
: E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'|ε E { ( , id } {$,)}
F -> (E)|id
E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε } {$,),+}

F { id , ( } {$,),+,*}
21

STEP: PARSING TABLE


22

Example of LL(1) grammar

E -> TE’
E’ -> +TE’|ε
T -> FT’
T’ -> *FT’|ε
F -> (E)|id
23
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

TABLE: id + * ( ) $

PARSING E

TABLE E’
T
T’
F
24
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE:
PARSING E E -> TE’

TABLE E’
T
T’
F
25
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE:
PARSING E E -> TE’ E -> TE’

TABLE E’
T
T’
F
26
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE:
E E -> TE’ E -> TE’
PARSING
TABLE E’ E’-> +TE’

T
T’
F
27
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Terminal

id + * ( ) $
TABLE:
E E -> TE’ E -> TE’
PARSING
TABLE E’ E’-> +TE’ E’ -> ε E’ -> ε

T
T’
F
28
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Terminal

id + * ( ) $
TABLE:
E E -> TE’ E -> TE’
PARSING
TABLE E’ E’-> +TE’ E’ -> ε E’ -> ε

T T -> FT’
T’
F
29
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE: E E -> TE’ E -> TE’
PARSING
E’ E’-> +TE’ E’ -> ε E’ -> ε
TABLE
T T -> FT’ T -> FT’

T’
F
30
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE: E E -> TE’ E -> TE’
PARSING E’ E’-> +TE’ E’ -> ε E’ -> ε
TABLE
T T -> FT’ T -> FT’

T’ T’ -> *FT’

F
31
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE: E E -> TE’ E -> TE’
PARSING E’ E’-> +TE’ E’ -> ε E’ -> ε
TABLE
T T -> FT’ T -> FT’

T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε

F
32
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE: E E -> TE’ E -> TE’
PARSING E’ E’-> +TE’ E’ -> ε E’ -> ε
TABLE
T T -> FT’ T -> FT’

T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε

F F -> id
33
Production Symbol FOLLOW
E -> TE’

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}


TABLE:
FIRST & FOLLOW
T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
INPUT SYMBOLS
Termina
l

id + * ( ) $
TABLE: E E -> TE’ E -> TE’
PARSING E’ E’-> +TE’ E’ -> ε E’ -> ε
TABLE
T T -> FT’ T -> FT’

T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε

F F -> id F -> (E)


34

Non
INPUT SYMBOLS
Termina
l
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)

TABLE: PARSING TABLE


 This grammar is LL(1).
 So, the parse tree can be derived from the stack
implementation of the given parsing table.
35

STEP: STACK IMPLEMENTATION


36

STACK Implementation

 The predictive parser uses an explicit stack to keep track of pending non-
terminals. It can thus be implemented without recursion.
 Note that productions output are tracing out a lefmost derivation
 The grammar symbols on the stack make up left-sentential forms
37

LL(1) Stack
 The input buffer contains the string to be parsed; $ is the end- of-
input marker
 The stack contains a sequence of grammar symbols
 Initially, the stack contains the start symbol of the grammar on the
top of $.
38
LL(1) Stack

The parser is controlled by a program that behaves as follows:


 The program considers X, the symbol on top of the stack, and a, the
current input symbol.
 These two symbols, X and a determine the action of the parser.
 There are three possibilities.
39
LL(1) Stack

1. X  a  $,
the parser halts and annouces successful completion.
2. Xa$
the parser pops x off the stack and advances input pointer to
next input symbol
3. If X is a nonterminal, the program consults entry M[x,a] of parsing
table M.
If the entry is a production M[x,a] = {x → uvw } then the
parser replaces x on top of the stack by wvu (with u on top).

As output, the parser just prints the production used:


x → uvw .
40

LL(1) Stack

Grammar: Example:
E -> TE' Let’s parse the input
E'-> +TE'|ε string
T -> FT'
T' -> *FT'|ε id+idid
F -> (E)|id Using the
nonrecursive
LL(1) parser
41
id + id  id $

E
$
stack Parsing
Table
Parsing Table
42

id + * ( ) $
E E →TE' E →TE'

E' E' → E' → E' →


+TE'
T T →FT' T →FT'

T' T' →  T →*FT' T' → T' →


F F → id F →(E )
43
id + id  id $

E
$
stack EPa→rsTin
Table
Eg'
44
id + id  id $

T
E'
$ Non
Terminal
INPUT SYMBOLS

stack id TParsing+ * ( ) $

Table M
E E -> TE’ E -> TE’

E’ E' -> +TE’ E’ -> ε E’ -> ε

T
T’
T -> FT’
→F T’ -> ε
T'
T’ -> *FT’
T -> FT’
T’ -> ε T’ -> ε
F F -> id F -> (E)
45
id + id  id $

F
T'
E'
$ Non
Terminal
INPUT SYMBOLS

stack id FParsing+ * ( ) $

Table M
E E -> TE’ E -> TE’

E’ E' -> +TE’ E’ -> ε E’ -> ε

T
T’
T -> FT’→d i T’ -> ε T’ -> *FT’
T -> FT’
T’ -> ε T’ -> ε
F F -> id F -> (E)
46
id + id  id $

id
T'
E'
$ Non
Terminal
INPUT SYMBOLS

stack id
Parsing
+ * ( ) $

Table M
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)
47
+ id  id $

T'
E'
$ Non
Terminal
INPUT SYMBOLS

stack id T'Parsing
+ * ( ) $

Table M
E E -> TE’ E -> TE’

E’ E' -> +TE’ E’ -> ε E’ -> ε

T
T’
T -> FT’→ T’ -> ε T’ -> *FT’
T -> FT’
T’ -> ε T’ -> ε
F F -> id F -> (E)
48
+ id  id $

E'
$ Non
Terminal
INPUT SYMBOLS

stack E'Parsing→
id + * ( ) $

Table M
E E -> TE’ E -> TE’

E’ E' -> +TE’ E’ -> ε E’ -> ε

T
T’ +T E'
T -> FT’
T’ -> ε T’ -> *FT’
T -> FT’
T’ -> ε T’ -> ε
F F -> id F -> (E)
49
+ id  id $

+
T
E'
$ Non
Terminal
INPUT SYMBOLS

stack id
Parsing
+ * ( ) $

Table M
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)
MATCHED STACK INPUT ACTION
E$ id+id * id$
TE’$ id+id * id$ E->TE’
50

FT’E’$ id+id * id$ T->FT’

id T’E’$ id+id * id$ F->id


id T’E’$ +id * id$ Match id
id E’$ +id * id$ T’->Є
id +TE’$ +id * id$ E’-> +TE’
id+ TE’$ id * id$ Match +

id+ FT’E’$ id * id$ T-> FT’

id+ idT’E’$ id * id$ F-> id


id+id T’E’$ * id$ Match id
id+id * FT’E’$ * id$ T’-> *FT’
id+id * FT’E’$ id$ Match *
id+id * idT’E’$ id$ F-> id
id+id * id T’E’$ $ Match id
id+id * id E’$ $ T’-> Є
id+id * id $ $ E’-> Є
51

STEP: LL(1) PARSE TREE


LL(1) Parse Tree 52

 Top-down parsing expands a parse tree from the start


symbol to the leaves
 Always expand the leftmost non-terminal
id+idid 53
E

E'
T

+ E'
F T' T


id  F T'

id * F T'
DONE…

id 

You might also like