Java Reference
In-Depth Information
procedure S
tmts
()
if ts . peek()= id or ts . peek()= print
then
8
call S
tmt
()
9
call S
tmts
()
10
else
if ts . peek()= $
then
11
/
do nothing for
λ
-production
/
12
else call
error
()
end
Figure 2.8: Recursive-descent parsing procedure for Stmts.
The production Stmts
Stmt Stmts begins with the nonterminal Stmt.
To discover the terminals that predict this rule, we must find those sym-
bols that predict any rule for Stmt. Fortunately, we have already done
this in Figure 2.7. The predicate at Marker 8 in Figure 2.8 checks for id
or print as the next token.
The production Stmts
derives no symbols, so we must look instead
for what symbols could occur after such a production. Grammar anal-
ysis (Chapter 4) can show that $ is the only such symbol, so it predicts
Stmts
→λ
→λ
at Marker 11 .
The analysis required to compute predict sets in general is covered in Chap-
ters 4 and 5.
2.5.2
Implementing the Production
Once a given production has been predicted, the recursive descent procedure
then executes code to trace across that production, one symbol at a time.
For example, the production Stmt
id assign Val Expr in Figure 2.7 derives 4
symbols, and those will be considered in the order id, assign, Val,andExpr.
The code for processing those symbols, shown at Markers 2 , 3 , 4 ,and 5 ,
is written into the recursive descent procedure as follows:
When a terminal such as id is encountered, a call to
match
( ts ,
id)is
placed into the code, as shown by Marker 2 in Figure 2.7. The
match
procedure (code shown in Figure 5.5 on page 149) simply consumes the
expected token id if it is indeed the next token in the input stream. If
some other token is found, then the input stream has a syntax error, and
an appropriatemessage is issued. The call afterMarker 2 tries to match
assign, which is the next symbol in the production.
 
 
 
Search WWH ::




Custom Search