Java Reference
In-Depth Information
procedure A( ts )
switch (...)
case ts . peek()∈ Predict( p 1 )
/
Code for p 1
/
case ts . peek()∈ Predict( p i )
/
Code for p 2
/
/
.
/
/
.
/
/
.
/
case ts . peek()∈ Predict( p n )
/
Code for p n
/
case default
/
Syntax error
/
end
Figure 5.6: A typical recursive-descent procedure. Successful LL(1)
analysis ensures that only one of the case predicates is true .
to right. As each symbol is visited, code is written into the parsing procedure.
For productions of the form A
0, so there are no symbols to visit. In
such cases, the parsing procedure simply returns immediately. In considering
each
→λ
, m =
X i , there are two possible cases, as follows.
•X i
is a terminal symbol. In this case, a call to
match
( ts ,X i ) is written
into the parser to insist that
X i is the next symbol in the token stream.
If the token is successfully matched, then the token stream is advanced.
Otherwise, the input string cannot be in the grammar's language and an
error message is issued.
•X i is a nonterminal symbol. In this case, there is a procedure responsible
for continuing the parse by choosing an appropriate production for
X i .
Thus, a call to
X i ( ts ) is written into the parser.
Figure 5.7 shows the parsing procedures created for the LL(1) grammar shown
in Figure 5.2. For presentation purposes, the default case (representing a
syntax error) is not shown in the parsing procedures of Figure 5.7.
5.4 Table-Driven LL(1) Parsers
The task of creating recursive-descent parsers as presented in Section 5.3 is
mechanical and can therefore be automated. However, the size of the parser's
 
Search WWH ::




Custom Search