09_1_TopDown
-
Upload
samin-afnan -
Category
Documents
-
view
4 -
download
0
description
Transcript of 09_1_TopDown
![Page 1: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/1.jpg)
1
Topic 9: Top Down Parsing
9.1 Introduction
2
Top-down Analysis
Top-down Analysis Revision
• Previously, we have looked at Syntactic Analysis in terms of bottom up algorithms: LR, SLR, LALR
• Now we will look at various top-down approaches.
• We start with the START symbol
• We apply expansions of non-terminal symbols.
Revision
![Page 2: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/2.jpg)
2
3
Top-down Analysis
Top-down Analysis Revision
• LL Parsing:
• Left-to-Right processing of input
• Expand Leftmost Nonterminal first
• Depth-first vs. Breadth-First:
• Depth-first: expand all children of node beforeexpanding sisters
• Breadth-First: expand all nodes at same levelbefore descending to their children
Revision
4
Top-down Analysis
Top-down analysis
• Top down analysis starts with the START symbol andexpands it
id + id * id
E
Revision
![Page 3: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/3.jpg)
3
5
Top-down Analysis
Top-down analysis
+ *
E
E
E Apply: E-> E+E
Revision
id id id
6
Top-down Analysis
Top-down analysis
+ *
E
E
E Apply: E-> id
Revision
id id id
![Page 4: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/4.jpg)
4
7
Top-down Analysis
Top-down analysis
+ *
E E
E
E
E Apply: E-> E*E
Revision
id id id
8
Top-down Analysis
Top-down analysis
+ *
E E
E
E
E Apply: E-> id
Revision
id id id
![Page 5: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/5.jpg)
5
9
Top-down Analysis
Top-down analysis
+ *
E E
E
E
E Apply: E-> id
Revision
id id id
Topic 9: Top Down Parsing
9.2 Deterministic Topdown Parsing(LL Parsers)
![Page 6: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/6.jpg)
6
11
Top-down Analysis
Predictive Parsers
• Some top-down parsers expand non-terminals non-deterministically: they apply the first alternative, and if later fail to parse, they go back(backtrack) and try one of the other expansions.
• In this class, we will consider top-down parserswhich deterministically select a single production toexpand each nonterminal:
• No backtracking required
• But grammar restrictions required.
Deterministic Topdown Parsing
12
Top-down Analysis
Predictive Parsers
• The class of grammars which permit thisdeterministic topdown analysis are called LL()grammars.
• An LL(1) parser can determine which rule to selectbased on one symbol of lookahead.
• In other words, we can select which rule shouldexpand a nonterminal purely on the basis of thenext input symbol.
Deterministic Topdown Parsing
![Page 7: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/7.jpg)
7
13
Top-down Analysis
LL Grammars
• An LL Grammar is any grammar which can be put into the parse table used by the LL parser without conflict.
• We can procede in two ways:
1. Given any CFG grammar, put it into the parse table and if there are no conflicts, it is LL.
2. Apply a series of transformations to convert the grammar into Greibach Normal Form (GNF) (and some other minor transformations). If these transformations can be applied, then the grammar is guaranteed to be LL.
• In this course, we will just use the first approach.
Deterministic Topdown Parsing
9.4 Building An LL Parser:Hand-coded Approach
![Page 8: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/8.jpg)
8
15
Building the parser
• The simplest approach is to write this table directly as code.
• Each LHS symbol has its own procedure
• The procedure receives two arguments, the list of inputtokens, and an index to indicate which is the current token.
int U (char *cadena, int i)
• The code consists of a switch/case statement which calls a different function (to expand a RHS element) depending onwhat the next input is.switch (cadena[i]) {
case x:
i++;
i = X1 (cadena, i);
i = X2 (cadena, i);
…
16
Code for Recursive Descent Parser
Grammar: U:- X1 X2 X3 | Y1 Y2 Y3
int U (char *cadena, int i)
{
if (i<0) return i;
switch (cadena[i]) {
case x:
i++;
i = X1 (cadena, i);
i = X2 (cadena, i);
i = X3 (cadena, i);
break;
case y:
i++;
i = Y1 (cadena, i);
i = Y2 (cadena, i);
i = Y3 (cadena, i);
break;
default: return -n;
}
return i;
}
![Page 9: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/9.jpg)
9
17
Code for Recursive Descent Parser
int T (char *cadena, int i)
{
if (i<0) return i;
switch (cadena[i]) {
case 'i':
i++;
i = U (cadena, i);
break;
case '(':
i++;
i = E (cadena, i);
i = C (cadena, i);
i = U (cadena, i);
break;
default: return -2;
}
return i;
}
Function returnserror code if none ofthe rules match next
input token
18
Code for Recursive Descent Parser
int T (char *cadena, int i)
{
if (i<0) return i;
switch (cadena[i]) {
case 'i':
i++;
i = U (cadena, i);
break;
case '(':
i++;
i = E (cadena, i);
i = C (cadena, i);
i = U (cadena, i);
break;
default: return -2;
}
return i;
}
Otherwise, functionreturns pointer tonext input token
![Page 10: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/10.jpg)
10
19
Code for Recursive Descent Parser
int T (char *cadena, int i)
{
if (i<0) return i;
switch (cadena[i]) {
case 'i':
i++;
i = U (cadena, i);
break;
case '(':
i++;
i = E (cadena, i);
i = C (cadena, i);
i = U (cadena, i);
break;
default: return -2;
}
return i;
}
If one sub-call returns a negative number (an error)
skip through remainingelements without parsing
20
Using the parser
• In order to analyse an input string x with this method, it isenough to invoke the function corresponding to the Startsymbol of the grammar with arguments:
• x (the chain to analyze) and
• 0 (the initial value of the counter).
• E.g., E(x, 0).
• If the value given back by the function agrees with thelength of the input string, the chain is recognised.
• If not, the function gives back a negative number, thatindicates the type of error detected.
![Page 11: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/11.jpg)
11
9.5 Building An LL ParserAutomatically Generated
Parse Tables
22
Use of Parse Tables
• Rather than hand-writing code for the parser, it ismore usual to use a parser generator.
• LL(1) grammar given as input.
• General purpose LL(1) pre-processor loads in thegrammar, and builds a parse table for it.
• The general purpose processor accepts inputstrings and processes on the basis of the parsetable.
![Page 12: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/12.jpg)
12
23
The Parse Table
• Rows correspond to Nonterminals
• Columns correspond to possible input tokens, and $
• The cells contain:
• The rule appropriate for the LHS and next token, or
• Nothing (indicates an error in parsing)
E::=bE
P’::=λP’::=eP
P’::=λ
P’
P::=iEtPP’P::=aP
$tieba
Note: note an LL(1) grammar because there is a conflict!
24
Top-down Analysis
Constructing the Table
• The cells of the table are filled by applying the followingprocedure:
For each production A:= α of the grammar:
• For each terminal symbol a ∈ First(α),
• Add the production A:= α in the cell T[A,a]
• If λ∈ First(α), for each terminal symbol b ∈ Follow(A), add the production A:= α in the T[A,b ].
• Note that b can be $.
Deterministic Topdown Parsing
![Page 13: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/13.jpg)
13
25
Top-down Analysis
Constructing the Table: Example
• Assume the followingGrammar:
Deterministic Topdown Parsing
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
26
Top-down Analysis
Constructing the Table: Example
• Assume the followingGrammar:
Deterministic Topdown Parsing
F
T´
T
E’
E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First, generate a table for each NT vs.
possible inputs
![Page 14: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/14.jpg)
14
27
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T
E’
E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T´) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
Next, calculate theFirsts of each NT
28
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T
E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T´) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
For each rule, place the rule in the cell for the LHS and
each First of the rule
![Page 15: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/15.jpg)
15
29
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T
E’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T´) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
Where the First includes lambda, we need to put the rule under all followers of the LHS!
30
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T
E’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
Calculate the Follow of these NTs
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
![Page 16: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/16.jpg)
16
31
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
…and place the rule under each follow symbol
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
32
Top-down Analysis
Constructing the Table: Example
Deterministic Topdown Parsing
F
T´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
…continuing
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
![Page 17: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/17.jpg)
17
33
Top-down Analysis
Constructing the Table: Example
• Assume the following Grammar:
Deterministic Topdown Parsing
F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
…continuing
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
34
Top-down Analysis
Constructing the Table: Example
• Assume the following Grammar:
Deterministic Topdown Parsing
F := id F := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
…continuing
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
![Page 18: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/18.jpg)
18
35
Top-down Analysis
Constructing the Table: Example
• Assume the following Grammar:
Deterministic Topdown Parsing
F := id F := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E := TE’E’ := +TE’E’ := λT := FT’T’ := *FT’T’ := λF := (E)F := id
First(F) = { (, id }First(T’) = { *, λ }First(T) = First(F) = { (, id }First(E’) = { +, λ }
First(E) = First(T) = { (, id }
Giving a completed table!
Follow(E’) = { $, ) }Follow(T’) = Follow(T)
= First(E’)- λ+Follow(E’)= {+, $, ) }
36
Top-down Analysis
LL(1) Grammars
• A grammar is LL(1) if the parse table produced by the previous procedure has at most one rule in each cell.
Deterministic Topdown Parsing
![Page 19: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/19.jpg)
19
37
Comparison with Grammars produced in previous classes
• The grammars produced in prior classes (converting to GNF and thence to a S-Grammar) will always be LL(1) followingthis definition.
• However, note that many grammars which are not GNF can be LL(1).
• We only require that:
• The selection between alternative expansion can be uniquelydetermined by looking at the next input symbol.
• The grammar is not left recursive.
• Basically, if a grammar is not left recursive, and CAN BE converted to an s-grammar, then it is LL(1).
38
Exercise:
• Construct the parse table for the following grammar:
P ::= iEtPP’
P ::= a
P’::= eP
P’::= λ
E ::= b
• Is this grammar LL(1)?
![Page 20: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/20.jpg)
20
7.6 Using the LL Parse Table
40
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
$)id+id(
E
![Page 21: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/21.jpg)
21
41
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
Given symbol E to expand andnext input=“(“, expansion is: TE’
T E’
42
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
Don’t advance the input pointer, Expand T
T E’
![Page 22: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/22.jpg)
22
43
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
Given symbol T to expand andnext input=“(”, expansion is FT’
T’F
44
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expand F
![Page 23: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/23.jpg)
23
45
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Given symbol F to expand andnext input=“(”, expansion is (E)
)( E
46
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Leftmost symbol is terminal ´(´, whichmatches next input, so advance input
)( E
![Page 24: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/24.jpg)
24
47
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expanding E with next input id, use
expansion: TE’
)E
E’T
(
48
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expanding T with next input id, use
expansion: FT’
)E
E’T
T’F
(
![Page 25: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/25.jpg)
25
49
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expanding F with next input id, use
expansion: id – advance pointer
)E
E’T
T’F
id(
50
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expanding T’ with next input +, use
expansion: λ
)E
E’T
T’F
id λ(
![Page 26: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/26.jpg)
26
51
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Expanding E’ with next input +, use
expansion: +TE’
)
(
E
E’T
T’F
id λ
E’
+
T
52
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
Match + and advance pointer
)
(
E
E’T
T’F
id λ
E’
+
T
![Page 27: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/27.jpg)
27
53
Top-down Analysis
Using the Parse Table
• Assume the following input, and Start symbol E
Deterministic Topdown Parsing
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
E
$)id+id(
T E’
T’F
)
(
E
E’T
T’F
id λ
E’
+
TExpanding T with next input id, use
expansion: FT’
54
Analysis using an Analysis Stack (rather than tree)
1. Initialize the Analysis Stack with 2 entries: $ and the Start symbol
2. Initialise the Input Queue with the string to analyse, terminated by $
3. Set the Input Pointer to the first item
4. Repeat the following procedure:
• Compare the symbol on top of the Analysis Stack (P) with thenext input symbol, S.
• If P==S:• If P == $, accept the input string and exit.
• Otherwise, pop top of Stack and advance input pointer
• If P≠ S:• If S is a terminal emit error and return
• If cell(P, S) is empty, emit error message and exit.
• Otherwise replace top of stack with its expansion:
• the cell contains an expansion of P:- X1 X2 …Xn.
• Pop P off the stack
• Push the symbols X1 X2… Xn onto the stack in reverse order.
![Page 28: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/28.jpg)
28
55
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E$
INITIAL STATEP
S
56
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E$
•E & ( not equal
•Cell( E,( ) has rule
•Replace on stack
![Page 29: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/29.jpg)
29
57
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E’ T$
•T & ( not equal
•Cell( T,( ) has rule
•Replace on stack
58
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
T’E’ F$
•F & ( not equal
•Cell( F,( ) has rule
•Replace on stack
![Page 30: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/30.jpg)
30
59
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E)T’E’ ($
•( & ( EQUAL!
•Pop Stack
•Advance pointer
60
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E)T’E’$
•E & id not equal
•Cell( E,id ) has rule
•Replace on stack
![Page 31: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/31.jpg)
31
61
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
E’ T)T’E’$
•T & id not equal
•Cell( T,id ) has rule
•Replace on stack
62
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
T’E’ F)T’E’$
•F & id not equal
•Cell( F,id ) has rule
•Replace on stack
![Page 32: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/32.jpg)
32
63
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
T’E’ id)T’E’$
•id & id EQUAL!
•Pop Stack
•Advance pointer
64
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
T’E’)T’E’$
…and so on!
![Page 33: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/33.jpg)
33
65
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
)$
Eventually:
•) and ) equal
•Advance pointer
•Pop Stack
66
Top-down Analysis
Using the Parse Table with Analysis Stack: Example
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Deterministic Topdown Parsing
$)id+id(
$
Eventually:
• $ and $ equal
• Top of stack is $
• Accept and Exit!
![Page 34: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/34.jpg)
34
67
Top-down Analysis
Using the Parse Table with Analysis Stack: Summary
Four Actions:
1. Accept: If P==S==$
2. Advance: If P==S ≠ $
3. Replace: If P ≠ S and Cell(S, P) ≠ Ø
4. Error: If P ≠ S and Cell(S, P) == Ø.
Deterministic Topdown Parsing
68
Top-down Analysis
Keeping track of the Analysis
• One can use a chart like the following to record each step:
Deterministic Topdown Parsing
Input Accepted$$13
Apply E’::=λ$$E’12
Apply T’::=λ$$E’T’11
Advanceid$$E’T’id10
Apply F::=idid$$E’T’F9
Apply T::=FT’id$$E’T8
Advance+id$$E’T+7
Apply E’::=+TE’+id$$E’6
Apply T’::=λ+id$$E’T’5
Advanceid+id$$E’T’id4
Apply F::=idid+id$$E’T’F3
Apply T::=FT’id+id$$E’T2
Apply E::=TE’id+id$$E1
ActionInputStackStep
![Page 35: 09_1_TopDown](https://reader033.fdocuments.us/reader033/viewer/2022051416/563dbbbc550346aa9aafd3f0/html5/thumbnails/35.jpg)
35
69accept$
advanceid
advance)
advance(
advance*
advance+
F := idF := (E)F
T’ := λT’ := λT’ := *FT’T’ := λT´
T := FT’T := FT’T
E’ := λE’ := λE’ := +TE’E’
E := TE’E := TE’E
$id)(*+
Alternative Table
• Row labels are symbols which can be on top of stack (term or nonterm)
• Column labels are next input
• Action given for current top of stack whether terminal or nonterm.
70accept22222$
3advance1111id
31advance111)
311advance11(
3111advance1*
31111advance+
3F := id4F := (E)44F
T’ := λ4T’ := λ4T’ := *FT’T’ := λT´
3T := FT’4T := FT’44T
E’ := λ4E’ := λ44E’ := +TE’E’
3E := TE’4E := TE’44E
$id)(*+
Alternative Table: error types
1. Next input does not match terminal on top of stack
2. Tree fully expanded, some input unconsumed
3. Tree incomplete, all input consumed
4. Next input cannot start nonterm on top of stack