Compiler Designs and Constructions

Download Report

Transcript Compiler Designs and Constructions

Compiler Designs and
Constructions
Chapter 6: Top-Down Parser
Objectives:
Types
of Parsers
Backtracking Vs. Non-backtracking
PDA
Dr. Mohsen Chitsaz
Chapter 6: Top-Down Parser
1
Definition:







Alphabet: Set of Char (token)
Language: Set of Token
Sentence: a group of token
Grammar: a set of (productions) that control
the order of the occurrence of words
Syntax: a set of rules
Parser: software to check the syntax
Parsing: Process of breaking down the
lexemes
Chapter 6: Top-Down Parser
2
Types of Parser

Universal Parsing Method example:
Cocke-Younger-Kasami Algorithm
•
•

Top-Down Parsing
•

Can parse any grammar
too inefficient to use in compiler production
Left most derivation
Bottom-up Parsing
•
Right most derivation
Chapter 6: Top-Down Parser
3
Top-Down Parsing
Non-backtracking Vs. Backtracking
Large Plant
Green Plant



Recursive Descent Parsing (Predictive
Parser)
We execute a set of recursive
procedures to process the input
There is one procedure associated with
each non-terminal of a grammar
Chapter 6: Top-Down Parser
4
Example
<type>  <simple>
 <type>  ^id
 <type>  array [<simple>] of <type>
<simple> integer
 <simple>  char
 <simple>  num .. num

Chapter 6: Top-Down Parser
5
Procedure Match (T:token);
Begin
If LookaHead==T then
LookaHead==NextToken
Else
Error();
End;
Chapter 6: Top-Down Parser
6
Procedure Type( );
Begin
If LookaHead in {integer, char, num} then
Simple();
Else
If LookaHead == ‘^’ then
Begin
Match (‘^’);
Match (id);
End
Else
If LookaHead == array then
Chapter 6: Top-Down Parser
7
Begin
Match (array);
Match (‘[‘);
Simple();
Match (‘]’);
Match (of);
Type();
End
Else
Error();
End;
Chapter 6: Top-Down Parser
8
Procedure Simple();
Begin
If LookaHead == integer then
Match (integer)
Else
If LookaHead == char then
Match (char)
Else
If LookaHead == num then
Begin
Match (num);
Match (‘..’);
Match (num);
End
Else Error();
End;
Chapter 6: Top-Down Parser
9
Example
<type>  <simple>
 <type>  ^id
 <type>  array [<simple>] of <type>
<simple> integer
 <simple>  char
 <simple>  num .. num

Chapter 6: Top-Down Parser
10
Example

Which production should we use?
integer

<type>  ^id
<type>  <simple>
<simple>  integer
<simple>  char
Chapter 6: Top-Down Parser
11
Example

First (simple) = {integer, char, num}
First (^id) = {^}
First (array [<simple>] of type = {array})
First (type) = {^, array, integer, char, num}
Chapter 6: Top-Down Parser
12
Definition:
A  ab
 First (A) = a

A
A
 First of  &  must be disjoint

Chapter 6: Top-Down Parser
13
Push Down Automata
(Non-recursive predictive Parsing)
Chapter 6: Top-Down Parser
14
Push Down Automata
(Non-recursive predictive Parsing)

PDA = {Q, , H,, q0, Z, F}
 Q = finite set of States
  = set of input alphabet
 H = set of stack symbols
  = transition function (q, a, z)
 q0 = starting state
 Z = starting stack
 F = final states
Chapter 6: Top-Down Parser
15
Operation on 

input
state operation
operation
 change()
 stay()

 advance()
 retain()
stack operation
 push()
 pop()
 replace()
 none()
Chapter 6: Top-Down Parser
16
Example:
INPUT




 = { (, ), -| }
H = {A, $}
q0 = {S}
Z = {$}
S
T
A
R
T
(
)
q1 A
Push (A)
State (S)
Advance
Pop
Reject
State (S)
Advance
q0 $
Push (A)
State (S)
Advance
Reject
Chapter 6: Top-Down Parser
-|
Accept
17
If input is (())-|
 Stack
Input

$
$A
$AA
$A
$
(())-|
())-|
))-|
)-|
-|
Chapter 6: Top-Down Parser
18
Derivation Tree

S  (S) | 
Chapter 6: Top-Down Parser
19
LL(1) Grammar (push down machine)
Scan from Left (L), Leftmost derivation (L), Look a
head 1 Token
Chapter 6: Top-Down Parser
20
Definition:

A production is called NULL if the RHS of
that production is Ø
A
 A production is call NULLABLE if the RHS
can be reduced to Ø
• BA
• A
Chapter 6: Top-Down Parser
21
Example of LL(1) Grammar:
1- S  AbB-|
2- S  d
3- A  C Ab
4- A  B
5- B  gSd
6- B  
7- C  a
8- C  ed
First(AbB-|) ={a,e,g, }
First(d)
= {d}
First(C Ab) = {a,e}
First(B)
= {g, }
First(gSd) = {g}
First (Ø)
= {}
First (a)
= {a}
First (ed)
= {e}
Chapter 6: Top-Down Parser
22
First () = {a|  =>*a}
 Set of terminal symbols that occur at the
beginning of string derived from 

Chapter 6: Top-Down Parser
23

Follow ()= Set of input symbols that can
immediately follow 
Follow (A) = {b}
Follow (B) = {-|, b, d}
1

S --->AbB --->BbB
1

4
5
1
S --->AbB --->AbgSd --->AbgAbBd
Chapter 6: Top-Down Parser
24
SELECT (PREDICT):

SELECT (A->) = {First ()
If = Ø; First () U Follow (A)}

SELECT (1) = {a,e,g} U Follow(A)={a, e, g ,b}
SELECT (2) = {d}
SELECT (3) = {a,e}
SELECT (4) = First(4) U Follow (A) = {g,b}
SELECT (5) = {g}
SELECT (6) = First(6) U Follow (B) = {-|,b,d}
SELECT (7)={a}
SELECT (8) = {e}







Chapter 6: Top-Down Parser
25
Is this grammar LL(1)?





S
A
B
C
SELECT (1)  SELECT (2) ={a,e,g,b}  {d}= Ø
SELECT (3)  SELECT (4) ={a,e}  {g,b}= Ø
SELECT (5)  SELECT (6) = {g}  {-|,b,d}= Ø
SELECT (7)  SELECT (8) = {a}  {e}= Ø
Def: Grammar is LL(1), IIF production with the
same LHS have disjoint prediction set.
Chapter 6: Top-Down Parser
26
Creating the Table:



Stack symbols=rows
Input symbols=columns
A b 
For row A, column b
=REPLACE(r), ADVANCE

A 
For row A, column(selection set)
=REPLACE (r), RETAIN

Ab
For row A, column b
=POP, ADVANCE
Chapter 6: Top-Down Parser
27
Creating the Table:



Row b, column b = POP, ADVANCE
Row Δ, column -|=ACCEPT
All other are ERROR
Chapter 6: Top-Down Parser
28
Parsed Table
S
a
#1
b
#1
g
#1
d
#2
e
#1
-|
Rej
A
#3
#4
#4
Rej
#3
Rej
B
C
Rej
#7
#6
Rej
#5
Rej
#6
Rej
Rej
#8
#6
Rej
b
d
Δ
Rej
Rej
Ad/Pop
Rej
Rej
Rej
Rej
Ad/P
Rej
Rej
Rej
Rej
Rej
Rej
Rej
Rej
Rej
Accept
Chapter 6: Top-Down Parser
29
1- Replace (Δ B b A), Retain
2- Pop, Advance
3- Replace (bAC), Retain
4- Replace (B), Retain
5- Replace (dS), Advance
6- Pop, Retain
7- Pop, Advance
8- Replace(d), Advance
Chapter 6: Top-Down Parser
30
Input:
b g d d -|
Stack
Input
Production
Action
S
bgdd-|
#1
Replace (D BbA),
Retain
Δ BbA
bgdd-|
#4
Replace(B), Retain
Δ BbB
bgdd-|
#6
Pop, Retain
Δ Bb
bgdd-|
ΔB
gdd-|
#5
Replace (dS), Advance
Δ dS
dd-|
#2
Pop, Advance
Δd
d-|
Pop, Advance
Δ
-|
Accept
Pop, Advance
Chapter 6: Top-Down Parser
31
Parse Tree
Chapter 6: Top-Down Parser
32
Recovery in a Top-Down Parser

Advantage of TDP is efficient error recovery.
Error: If Top of stack (token) is not the same as
Look a Head (token)

Recovery:



Pop stack until Top: Synchronization Token (ST)
ST: is a token that can end blocks of codes
Read input symbol until current LookaHead symbol
matches the symbol at the top of the stack or
reaches the end of input
If not end of input you are recovered
Chapter 6: Top-Down Parser
33
LL(1) Grammars & Parser:

facts:
CFG
 Leftmost Parser
 Unambiguous
 O(n)
 Can be used for automatically
generated parser

Chapter 6: Top-Down Parser
34
Making Grammar: LL(1)

1-Remove Common Prefixed (Left Factor)
 S --> aBCd
S --> aBD
 S --> aB
D -->

D --> Cd





<s> --> if <exp> then <action> endif;
<s> --> if <exp> then <action> else <action> endif;
<s> --> if <exp> then <action> <rest>
<rest> --> endif;
<rest> --> else <action> endif;
Chapter 6: Top-Down Parser
35
2-Remove Left Recursion:E

E --> E + T
E --> T
T --> id

A--> A
A-->B

A-->A1 A2
A1-->B
A2-->A2
A2-->
•E-->E1E2
E1-->T
E2-->+T E2
T-->id
E2-->
Chapter 6: Top-Down Parser
36
3-Remove Unreachable Products
S-->a
B-->S
S -->a
Chapter 6: Top-Down Parser
37
4-Corner Substitution
S --> AB
 B -->1
 B -->2
 B -->3

•S --> a
•S --> AS
•A --> ab
•A --> cA
•S --> aB
•B -->
 S --> A1
•B --> bS
 S --> A2
•A --> ab
 S --> A3
•A --> cA
•S
-->
cAS
Chapter 6: Top-Down Parser
38
5-Singleton Substitution

S-->B

S--> S'
S'--> B

<S> --> <LABEL> <UNLABLE>
<LABEL> --> id:
<LABEL> -->
<UNLABLE> --> id:= <EXP>;
Chapter 6: Top-Down Parser
39
Singleton
Substitution

-<S> --> id: <UNLABLE>
<S> --> <UNLABLE>
<UNLABLE> --> id:=<EXP>;

-<S> --> id:<UNLABLE>
<S> --> id:= <EXP>;
<UNLABLE> --> id:= <EXP>;

-<S> --> id <id-rest>
<id-rest> --> : <UNLABLE>
<id-rest> --> := <EXP>
<UNLABLE> --> id := <EXP>
Chapter 6: Top-Down Parser
40
Some Grammars Can Not be LL(1)

*If _ Then _ Else
If <exp> then <action>
If <exp> then <action> else <action>

*S --> aBcS
S --> aBcSeS
B --> d
S --> b
Chapter 6: Top-Down Parser
41

Q-Grammar:




S-Grammar:



A --> a1
A --> b1
A -->
A --> a1
A --> b2
A and S grammar are a LL(1) and we can make a push
down Machine
Chapter 6: Top-Down Parser
42