Starting from the Java 1.1 grammar I have tried to write a simplified grammar for a new language. Trying to parse IfStatements in various shapes I have encountered a strange problem which I don’t understand and for which I can’t find a solution. I have written two test programs, the first containing:
… if (a > b) { x = 15; } else { x = 30; }
and the second:
… if (a > b) x = 15; else x = 30;
The first program is parsed without any problems while for the second the parser stops with the message “Encountered else at (start of ELSE) expecting one of: boolean | break | byte | char | continue ... “. According to the grammar (or rather to my intention ) there shouldn’t be any difference between a block or a single statement, and to my astonishment this perfectly works for WHILE or DO but not for IF.
The relevant pieces of my grammar are as follows:
void Statement() :
{}
{
( LOOKAHEAD(2)
Block() | EmptyStatement() | IfStatement() |
WhileStatement() | ...
}
void Block() :
{}
{ "{" ( BlockStatement() )* "}"
}
void BlockStatement() :
{}
{
( LOOKAHEAD(2) LocalVariableDeclaration() ";" |
Statement()
)
}
void EmptyStatement() :
{}
{ ";" }
void IfStatement() :
{}
{
"if" "(" Expression() ")" Statement()
[ LOOKAHEAD(1) "else" Statement() ]
}
void WhileStatement() :
{}
{
"while" "(" Expression() ")" Statement()
}
There is a second observation possibly connected with the above problem: As stated before, the first test program is parsed correctly. However, for the “Statement” in the (partial) grammar rule <"if" "(" Expression() ")" Statement()> in the IF statement the parser yields an overly complex sequence of “Statement, Block, BlockStatement, Statement” nodes including an EmptyStatement before and after the statement content while parsing a test program containing an equivalent WHILE loop just produces “Statement” and no EmptyStatements at all.
So far I have tried to increase the number of lookahead elements in the IfStatement and/or the Statement productions but without any effect.
What is going wrong here, or what is wrong in my grammar?
Aucun commentaire:
Enregistrer un commentaire