[antlr-interest] if-then-else - Grammar generates faulty parser code

Poornima Prakash poornima.prakash at cgi.com
Thu Apr 22 22:36:57 PDT 2004


Thanks a lot for the prompt reply. That helped!

-Poornima

>>> monty at codetransform.com 04/22/04 10:18PM >>>
That generated code looks correct to me.  The statement "rule_AST = 
currentAST.root;" needs to be run at the end of the try block.  You are 
reaching the default block because you've hit EOF but the parser is 
expecting more.  Add EOF at the end of 'rule', or make a top-level rule 
that calls 'rule'.  When you call a parser rule it must be a top level 
rule--which means it must not be called by other rules.  Try adding 
this to your grammar and then calling in at topRule.

topRule: rule ;


Monty Zukowski

ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit -- 
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html

On Apr 22, 2004, at 12:16 AM, poornima_prakash wrote:

> We have a classic 'if-then-else' rule in the grammar file. The
> problem we are facing is with the ANTLR versions 2.7.2 and 2.7.4rc1.
>
> The 'rule' in the grammar file is defined as follows :
>
>   rule options { defaultErrorHandler = true;} :
>     ifPart thenPart
>     (options {warnWhenFollowAmbig = false;} :
>         elsePart
>         )?;
>         
>   ifPart: "if"^ LPARENTHESIS! conditional_expression RPARENTHESIS!;
>     
>   thenPart  : "then"^ stmt_blk;
>     
>   elsePart  :"else"^ stmt_blk;
>
> Since the 'else' part is optional, when we give an input rule with
> only the 'if-then' and without the 'else', AST is not generated
> correctly. It stops at the root node!
>
> However, if we manipulate the generated C# code, AST is correctly
> generated. We have to  move the line "rule_AST = currentAST.root;" in
> the generated sources such that it is traversed even when only 'if-
> then' input rule is given.
>
> To be more precise, the line "rule_AST = currentAST.root;"  at the
> end of the 'try' block in the below (automatically generated parser)
> code has to be moved into the 'default' case of switch-case for the
> correct AST to get generated.
>
> public void rule() //throws RecognitionException, TokenStreamException
> {
>         
>         returnAST = null;
>         ASTPair currentAST = new ASTPair();
>         AST rule_AST = null;
>         
>         try {      // for error handling
>             ifPart();
>             astFactory.addASTChild(currentAST, returnAST);
>             thenPart();
>             astFactory.addASTChild(currentAST, returnAST);
>             {
>                 switch ( LA(1) )
>                 {
>                 case LITERAL_else:
>                 {
>                     elsePart();
>                     astFactory.addASTChild
> (currentAST, returnAST);
>                     break;
>                 }
>                 case LITERAL_if:
>                 ....
>                 case IDENTIFIER:
>                 {
>                     break;
>                 }
>                 default:
>                 {
>                     throw new NoViableAltException
> (LT(1), getFilename());
>                 }
>                 }
>             }
>             rule_AST = currentAST.root;
>         }
>         catch (RecognitionException ex)
>         {...
>         }
>         returnAST = rule_AST;
>     }
>
>
> Please advice the required grammar change such that the AST gets
> generated properly without moving around the generated code.
>
> Looking forward to your reply.
>
> regards,
> Poornima and Raghu.
>     
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>




Yahoo! Groups Links




__________________________________________________
************** Confidentiality Notice ************
 
Proprietary/Confidential Information belonging to 
CGI may be contained in this message. If you are 
not a recipient indicated or intended in this 
message (or responsible for delivery of this 
message to such person), or you think for any 
reason that this message may have been addressed 
to you in error, you may not use or copy or deliver 
this message to anyone else. In such case, you 
should destroy this message and kindly notify the 
sender by reply email.
___________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040423/97f8f3d1/attachment.html


More information about the antlr-interest mailing list