[antlr-interest] Loop until EOF
Magnus Knuth
mai00cas at studserv.uni-leipzig.de
Mon Sep 25 10:45:11 PDT 2006
Hello.
I want to parse a file where "foo( /term?/ )." repeats until EOF and get
an AST with foo_list as root with all the foos as children. So I use
these two rules:
foo_simple_list
: (foo_entry )* EOF!
{
## = #([FOO_LIST, "FooList"], ##);
};
foo_entry
: FOO^ LPAREN! ( term )? RPAREN! FULL_STOP!
;
exception catch [RecognitionException ex] {
reportError(ex);
consume();
while (LA(1) != Token.EOF_TYPE && LA(1) != FULL_STOP) {
consume();
}
if (LA(1) == FULL_STOP) {
System.out.println("recover");
consume();
}
return;
}
My problem is that when there is an spelling error in 'foo' the parser
throws a RecognitionException "expecting EOF, found 'bar'". But i would
like the parser to go into foo_entry rule and recover from not finding a
"foo" until there is nothing but a endoffile.
The produced parser code looks like:
{
_loop4159:
do {
if ((LA(1)==FOO)) {
foo_entry();
astFactory.addASTChild(currentAST, returnAST);
}
else {
break _loop4159;
}
} while (true);
}
match(Token.EOF_TYPE);
but should be more something like:
{
_loop4159:
do {
if ((LA(1) != EOF)) {
foo_entry();
astFactory.addASTChild(currentAST, returnAST);
}
else {
break _loop4159;
}
} while (true);
}
match(Token.EOF_TYPE);
How do I get such a behavior? I already tried with syntactic predicates,
greedyness and many more without success.
Thanks for hints. Magnus.
More information about the antlr-interest
mailing list