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

Harald Mueller harald_m_mueller at gmx.de
Wed Nov 28 02:12:59 PST 2007


I once found that explicit checking fro EOF helps to force the parser to read thru all its input. So make your outermost rule (constraint?):

 constraint: orexpression EOF ;

- then it should work.
I think that right now, only the part before the ANND is recognized, then parsing ends.

Regards
Harald M.


-------- Original-Nachricht --------
> Datum: Wed, 28 Nov 2007 02:00:20 -0800 (PST)
> Von: William Cai <william.cai at yahoo.com>
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] Why the generated parser code tolerates illegal	expression?

> 
> 
> 
> 
> 
>  
>  
> 
> <!--
>  _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
> --------------------------------------
>  
> 
> 
> 
> 
> 
> 
>  
> 
> 
> 
> 
> 
> 

-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger


More information about the antlr-interest mailing list