0% found this document useful (0 votes)
223 views10 pages

Practical Top Down Parsing

The document summarizes recursive descent parsing and LL(1) parsing. Recursive descent parsing uses recursive procedures to perform top-down parsing without backtracking. It can be implemented in any language that supports recursive procedures. LL(1) parsing uses a table-driven approach where the prediction is determined by the next input symbol. The table guides the parser through the different production rules based on the current sentential form and next input symbol.

Uploaded by

akhot86
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views10 pages

Practical Top Down Parsing

The document summarizes recursive descent parsing and LL(1) parsing. Recursive descent parsing uses recursive procedures to perform top-down parsing without backtracking. It can be implemented in any language that supports recursive procedures. LL(1) parsing uses a table-driven approach where the prediction is determined by the next input symbol. The table guides the parser through the different production rules based on the current sentential form and next input symbol.

Uploaded by

akhot86
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Practical Top Down Parsing

Recursive Descent Parser


• A recursive descent (RD) parser is a variant
of top down parsing without backtracking.
• It uses a set of recursive procedures to
perform parsing.
• Salient advantages of RD parsing is its
simplicity and generality
• It can implemented in any language that
supports recursive procedures
Recursive Descent Parser
• To implement recursive descent parsing, a
left-factored grammar is modified to make
repeated occurrences of strings more explicit
E ::= T{+T}*
T ::= V {*V}*
V ::= <id>
• The notation {…}* indicates zero or more
occurrences of the enclosed specification
Recursive Descent Parser
• A parser procedure is now written for each NT
of G.
• It handles prediction making, matching and
error reporting for that NT
• The structure of the parser procedure is
dictated by the grammar production for the NT
• If A ::= …B… is a production of G, the parser
procedure for A contains a call on the
procedure for B.
proc_E (tree_root) proc_T (tree_root)
{ a, b : pointer to a tree node { a, b : pointer to a tree node
proc_T (a); proc_V (a);
while (nextsymb ==‘+’) while (nextsymb ==‘*’)
{ match (‘+’); { match (‘*’);
proc_T (b); proc_V (b);
a = treebuild (‘+’, a, b); a = treebuild (‘*’, a, b);
tree_root = a; tree_root = a;
} }
return; return;
} }
proc_V (tree_root)
{ a : pointer to a tree node
if (nextsymb ==<id>)
tree_root = treebuild (<id>, - , - );
else
print “Error!”;
return;
}
Recursive Descent Parser
Example
• The parser returns an AST for a valid source
string, ad reports an error for an invalid string
• The procedures proc_E, proc_T and
proc_V handle the parsing for E, T and V,
respectively
• And build ASTs for these NTs using the
procedure treebuild
• Procure match increments SSM
An LL(1) Parser
• An LL(1) parser is a table driven parser for left-to-
left parsing.
• The ‘1’ in LL(1) indicates that the grammar uses a
look-ahead of one source symbol – that is, the
prediction to be made is determined by the next
source symbol.
• A major advantage of LL(1) parsing is its
amenability to automatic construction by the
parser generator
An LL(1) Parser
Example
• Grammar:
E ::= TE'
E' ::= +TE' | ε
T ::= V
T' ::= *VT' | ε
V ::= <id>
• Source String:
<id> + <id> * <id>
LL(1) Parser Table
Non- Source Symbol
terminal <id> + * $

E E => TE'
E' E' => +TE' E' => ε
T T=>VT'
T' T' => ε T' => *VT' T' => ε
V V=><id>
LL(1) Parser Table
• A parsing table (PT) has a row for each NT∈SNT and
a column for each T ∈ Σ .
• A parsing table entry PT(ntj, tj) indicates what
prediction should be made if ntj is the leftmost NT in
a sentential form and tj is the next source symbol
• A blank entry in PT indicates an error situation
• A source string is assumed to be enclosed between the
symbols ‘$’ and ‘$’. Hence the parser starts with the
sentential form $E$
Current Sentential Form Symbol Prediction
$E$ <id> E => TE'
$TE'$ <id> T => VT'
$VT'E'$ <id> V => <id>
$<id>T'E'$ + T' => ε
$<id>E'$ + E' => +TE'
$<id>+TE'$ <id> T => VT'
$<id>+VT'E'$ <id> V => <id>
$<id>+<id>T'E'$ * T' => *VT'
$<id>+<id>*VT'E'$ <id> V => <id>
$<id>+<id>*<id>T'E'$ $ T' => ε
$ <id>+<id>*<id>E'$ $ E' => ε
$<id>+<id>*<id>$ - -

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy