[antlr-interest] Why the generated parser code tolerates illegal expression?

William Cai william.cai at yahoo.com
Wed Nov 28 02:00:20 PST 2007






 
 

<!--
 _filtered {font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;}
 _filtered {panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;margin-bottom:.0001pt;text-align:justify;font-size:12.0pt;font-family:"Times New Roman";}
a:link, span.MsoHyperlink
	{color:blue;text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{color:purple;text-decoration:underline;}
span.EmailStyle17
	{font-family:"Times New Roman";color:windowtext;font-weight:normal;font-style:normal;text-decoration:none none;}
 _filtered {margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
	{}
-->






Hello
list,

Here is a problem I encountered with ANTLR v2. 
 

  
 

I
defined a general logic operation grammar and compiled it to Java code as
below. It works well except one problem – the generated code tolerates illegal
expression. I can pass the expression “NOTExpression1 ANND NOTExpression2” in without any
problems. I looked into the generated parser code and found the illegal word “ANND”
was just ignored since it didn't match the key word “and”. (Please see the
code marked in red below.) Anybody in the list got the problem before? Please
share your experience. 
 

  
 

Any
comments are greatly appreciated.



Thanks,

William
 

  
 

----------------
Grammar -------------------
 

// Constraint Rules
 

constraint:
 

    orexpression
 

    ;
 

orexpression
 

    :   andexpression ("or"^
andexpression)*
 

    ;
 

andexpression
 

    : notexpression ("and"^
notexpression)*
 

    ;
 

notexpression
 

    : ("not"^)? equalityExpression
 

    ;
 

equalityExpression
 

    :   relationalExpression ((NOTEQ^ |
EQUAL^) relationalExpression)*
 

    ;

---------------- End of Grammar -------------------
 

  
 

  
 

----------------------------------
Generated Code --------------------------------------

    public final void andexpression() throws RecognitionException,
TokenStreamException {
 

       
 

       returnAST = null;
 

       ASTPair currentAST = new ASTPair();
 

       AST andexpression_AST = null;
 

       
 

       try {      // for error handling
 

           notexpression();
 

           astFactory.addASTChild(currentAST,
returnAST);
 

           {
 

           _loop34:
 

           do {
 

              if ((LA(1)==LITERAL_and)) {
 

                  AST
tmp61_AST = null;
 

                  tmp61_AST
= astFactory.create(LT(1));
 

                  astFactory.makeASTRoot(currentAST,
tmp61_AST);
 

                  match(LITERAL_and);
 

                  notexpression();
 

                  astFactory.addASTChild(currentAST,
returnAST);
 

              }
 

              else {
 

                  break _loop34;
 

              }
 

              
 

           } while (true);
 

           }
 

           andexpression_AST =
(AST)currentAST.root;
 

       }
 

       catch (RecognitionException ex) {
 

           if (inputState.guessing==0) {
 

              reportError(ex);
 

              consume();
 

              consumeUntil(_tokenSet_15);
 

           } else {
 

             throw ex;
 

           }
 

       }
 

       returnAST = andexpression_AST;
 

    }

---------------------------------- End of Generated Code --------------------------------------
 






 






-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071128/a2271d93/attachment-0001.html 


More information about the antlr-interest mailing list