We all know variable names cannot start with a number. (eg. foo1 is valid, 1foo is not).
I am trying to write a grammar file to allow only valid variable names, and they must be followed by a colon. (This is part of a much larger grammar - I'm just stuck on this one part)
It seems like it should be simple. I define a rule id that takes in only an alpha value as its first character, followed by any number of alpha-numeric characters. However what seems like a simple task is failing for me. Can anyone explain why?
Here is my grammar:
grammar validName;
var_declaration :VAR id COLON;
VAR: 'var';
COLON: ':';
DIGIT: [0-9];
ALPHA: [a-zA-Z_];
ALPHANUM: ALPHA | DIGIT;
id: ALPHA ALPHANUM*;
WS: [ \n\t\r]+ -> skip;
Here is my test input:
var myId :
And here is the error:
line 1:5 mismatched input 'y' expecting ':'
Why is ALPHANUM* not matching anything??