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

William Cai william.cai at yahoo.com
Wed Nov 28 19:11:07 PST 2007


At least, the code marked in red below could be updated, say "
    if(LA(1) != LITERAL_and && LA(1) != EOF) 
        throw Exception(); //blablabla...
" in my case.

Did I miss anything?


    
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;

    
}




----- Original Message ----
From: William Cai <william.cai at yahoo.com>
To: Terence Parr <parrt at cs.usfca.edu>; antlr-interest Interest <antlr-interest at antlr.org>
Sent: Thursday, November 29, 2007 10:51:47 AM
Subject: Re: [antlr-interest] Why the generated parser code tolerates illegal expression?



I'm adding the item. By the way, why not add some check code in the generated parser so that we need not declare EOF explicitly? Is this already done in ANTLR v3, or still in to-do list, or just impossible to implement?

Thanks, William

----- Original Message ----
From: Terence Parr <parrt at cs.usfca.edu>
To: antlr-interest Interest <antlr-interest at antlr.org>
Sent: Thursday, November 29, 2007 12:46:21 AM
Subject: Re: [antlr-interest] Why the generated parser code tolerates illegal expression?




On Nov 28, 2007, at 5:52 AM, William Cai wrote:

Thanks Harald. It works now. Should this be recorded in FAQ? 



Sure!  Somebody sign up and add it ;)


Ter







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


More information about the antlr-interest mailing list