views:

35

answers:

1

Can I write a rule where the initial token is partly fixed and partly generic?

rule: ID '=' NUMBER
      ;

ID: (A.. Z | a.. Z) +

NUMBER: (0 .. 9) +

But only if the token ID is in the form var* (var is fixed)

Thanks

+2  A: 

Do you mean this?

// Use this instead of ID in all parser rules that take either token
identifier
    :   VAR_ID | ID
    ;

VAR_ID
    :   'var' ('A'..'Z' | 'a'..'z')*
    ;

ID
    :   ('A'..'Z' | 'a'..'z')+
    ;

Either way: when possible, do NOT use a strict lexer grammar for your error messages. They are slower and completely illegible to users. You should parse ID in a relaxed form, then check later on if it's in precisely the correct form.

280Z28
Thank you. But the token ID is present in other rules and I need that it remains generic.
Boo
@Boo: If you follow my *strong* suggestion, it would remain general anyway.
280Z28
is just what I need if you say you do it in the parser. Please keep me you could post an example? I'm not an expert in ANTLR ... Thank you very much.
Boo