[antlr-interest] Interpreting IF statements in ANTLR
Jim Idle
jimi at temporal-wave.com
Thu Jul 8 07:25:40 PDT 2010
Please see: antlr.markmail.org
This questions is answered many times in the list :-)
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of ZelluX
> Sent: Thursday, July 08, 2010 1:39 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Interpreting IF statements in ANTLR
>
> I'm implementing a BASIC-like language, the syntax of if statements is
> almost the same to BASIC:
>
> IF a == b THEN
> PRINT "EQUAL"
> ELSE
> PRINT "UNEQUAL"
> ENDIF
>
> I have write a grammar file to parse and a tree walker to interpreter
> the
> language:
>
> [Expr.g]
> options {
> language=Python;
> output=AST;
> ASTLabelType=CommonTree;
> }
>
> tokens {
> BLOCK;
> }
>
> block
> : stmt* -> ^(BLOCK stmt*)
> ;
>
> if_stmt
> : 'IF' c=expr 'THEN' t=block ('ELSE' f=block)? 'ENDIF'
> -> ^('IF' $c $t+ ^('ELSE' $f+))
> ;
>
> In the AST walker:
>
> [Walker.g]
> options {
> language=Python;
> tokenVocab=Expr;
> ASTLabelType=CommonTree;
> }
>
> block
> : ^(BLOCK stmt*)
> ;
>
> stmt
> : ...
> | 'IF' expr t=stmt* 'ELSE' f=stmt*
> {}
>
> Now I can correctly generate AST for my language, but I don't know how
> to
> handle branch statement. To be more exactly, if the *expr* in if
> statement
> is true, how can I avoid evaluation of the ELSE statement? Thanks
>
> --
> Best regards,
> Wang Yuanxuan
> Parallel Processing Institute, Fudan University
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
More information about the antlr-interest
mailing list