[antlr-interest] help regrading actions in antlr grammer
Bjoern Doebel
doebel at tudos.org
Mon Aug 27 02:38:28 PDT 2007
Hi,
> Hello Dear
> I am a student .And I am trying to add actions to this grammer as below.
So assuming input like
add foo, bar ;
you want to print
add foo bar
in your terminal? Just add some print statements to your parser rules.
> I want to get a list of OPCODES and then operands on the terminal.
> Can any one plz help me how can I do it without using the string
> Template and will be useful if also using the string template.
> i will be greatful.
>
>
> Here is the grammer
>
> grammar assemb;
> options {output=template; rewrite=true;}
You don't need these options.
> program
> : (stat)+
>
> ;
>
> stat: OPCODE operands
rewrite to:
stat : OPCODE operands {print $OPCODE.text} ;
>
> ;
> operands
> :REGISTER SEMI
> | REGISTER COMMA REGISTER SEMI
> ;
operands:
REGISTER SEMI { print $REGISTER.text }
| r1=REGISTER COMMA r2=REGISTER SEMI { print r1.text, r2.text }
> OPCODE :
> 'add'
> |'store'
> |'load'
> |'call'
> |'ret'
> |'print' ;
>
> REGISTER :
> 'r1'
> |'r2'
> |'r3'
> |'r4' ;
>
> NUM : '[0-9]+' ;
> ID : '[a-zA-Z]+' ;
>
> EOL
> : (('\r')? '\n') ;
>
> WS
> : (
> | (' ' | '\t')
> ) { $channel=HIDDEN; }
> ;
Do you really want to catch EOL? Otherwise you could include it into the
ignored whitespace rule.
Regards,
Bjoern
More information about the antlr-interest
mailing list