Java Reference
In-Depth Information
method recoverFromError() and try to recover to the nearest semicolon ( SEMI ) or
to the end of file ( EOF ). We pass to this method the instance of ParseException so
that the method can report a meaningful error message.
As another example, let us see how we specify the non-terminal statement,
statement ::= block
j <identifier>: statement
j if parExpression statement [ else statement]
j while parExpression statement
j return [expression] ;
j ;
j statementExpression ;
for parsing statements in j--.
privateJStatementstatement():{
intline=0;
JStatementstatement=null;
JExpressiontest=null;
JStatementconsequent=null;
JStatementalternate=null;
JStatementbody=null;
JExpressionexpr=null;
}
{
try{
statement=block()|
<IF>{line=token.beginLine;}
test=parExpression()
consequent=statement()
//Evenwithoutthelookaheadbelow,whichisaddedto
//suppressJavaCCwarnings,danglingif-elseproblemis
//resolvedbybindingthealternatetotheclosest
//consequent.
[
LOOKAHEAD(<ELSE>)
<ELSE>alternate=statement()
]
{statement=
newJIfStatement(line,test,consequent,alternate);}|
<WHILE>{line=token.beginLine;}
test=parExpression()
body=statement()
{statement=newJWhileStatement(line,test,body);}|
<RETURN>{line=token.beginLine;}
[
expr=expression()
]
<SEMI>
{statement=newJReturnStatement(line,expr);}|
<SEMI>
{statement=newJEmptyStatement(line);}|
//MustbeastatementExpression
statement=statementExpression()
<SEMI>
}
catch(ParseExceptione){
recoverFromError(newint[]{SEMI,EOF},e);
}
{returnstatement;}
 
Search WWH ::




Custom Search