[antlr-interest] C Target - Assigning rule info to imaginary tokens

John B. Brodie jbb at acm.org
Tue May 10 10:08:43 PDT 2011


Greetings!

On Tue, 2011-05-10 at 09:14 -0500, A Z wrote:
> Hello all,
> 
>   I have a case where I need to assign an imaginary token the attributes of
> a token inside a rule. I tried the below but as expected it does not have
> the desired effect.
> 
> 
> var_or_function :
>   identifier
>   (
>     LPARAN arg_list RPARAN
>       -> ^(I_FUNC[identifier] arg_list)
>   |
>       -> I_UNKN[identifier]
>   );
> 
> identifier :
>     SIMPLE_IDENT
>   | ESCAPED_IDENT;
> 
> 
> Is there any way to do this without merging the two lexer rules into one
> token?

perhaps this:

var_or_function :
  id=identifier
  (
    '(' arg_list ')'
      -> ^(I_FUNC[$id.token] arg_list)
  | /* empty */
      -> I_UNKN[$id.token]
  );

identifier returns [Token token]:
      ( i=SIMPLE_IDENT | i=ESCAPED_IDENT )  { $token = $i; };


I do not have the talent to try this with the C target. But did test
with Java target. Should be target agnostic as it just uses ANTLR's
metalanguage (well maybe the return type of the identifier rule may need
to be adjusted.... YMMV ;-)

Hope this helps...
   -jbb




More information about the antlr-interest mailing list