Please see: antlr.markmail.org This questions is answered many times in the list :-)
Jim > -----Original Message----- > From: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of ZelluX > Sent: Thursday, July 08, 2010 1:39 AM > To: [email protected] > 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 List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
