[antlr-interest] my parser loops
Jerome Pequery
jpequery at objecteering.com
Tue Mar 21 09:37:12 PST 2006
Hi everyone,
I have written a java parser using antlr 2.7.2, having for particularity
to integrate comments into the AST while parsing. And i have a problem
using class contents and what is between { and }:
> // This is the body of a class. You can have fields and extra semicolons,
> // That's about it (until you see what a field is...)
> classBlock
> : LCURLY!
> ((j:commentDefinition)?
> ( (field[#j])=>field[#j]
> | (semiOrSemiComment)
> {
> astFactory.addASTChild(currentAST, j_AST);
> break;
> }
> )
> )*
> RCURLY!
> {#classBlock = #([OBJBLOCK, "OBJBLOCK"], #classBlock);}
> ;
I parse an optional comment (commentdefinition) and, if there is, parse
a field (attribute or operation), passing it its association comment as
parameter.
But now, if the comment close the class content, as in this example :
> public class GameStatusLabel extends JLabel implements GameListener {
> /**
> * commentaire
> */
> public void updateStatus(String message)
> {
> frame.addWindowListener(new WindowAdapter()
> {
> });
> }
>
> //public Dimension getMaximumSize() {
> //Dimension d = getPreferredSize();
> //d.width = Short.MAX_VALUE;
> //return d;
> //}
> }
There is no associated field for my comment. So the parser give me a
syntax error on }.
Now, if I use sytactic predicates to check for this case and simply drop
theses comments, so my rule is modified.
> classBlock
> : LCURLY!
> ((j:commentDefinition)?
> ( (field[#j])=>field[#j]
> | (semiOrSemiComment)
> {
> astFactory.addASTChild(currentAST, j_AST);
> break;
> }
> | (RCURLY)=> {
> astFactory.addASTChild(currentAST, j_AST);
> break;
> }
> )
> )*
> RCURLY!
> {#classBlock = #([OBJBLOCK, "OBJBLOCK"], #classBlock);}
> ;
The syntax error disappear, and my code is parsed. But anonymous classes
parsing are now looping. In my upper java sample, the generated parser
loop indefinitly with LA(1)=RCURLY and LA(2)=RPAREN.
How can I correct my grammar to support both comments at end of a class
and anonymous classes ?
Thanks in advance,
Jerome Pequery
More information about the antlr-interest
mailing list