Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top