I've wrote a grammar rule for a language in ANTLR as below:
variable: idlist COLON type (EQUAL explist)? SEMI;
idlist: identifier (COMMA identifier)*;
explist: exp (COMMA exp)*;
COLON: ':';
EQUAL: '=';
SEMI: ';';
COMMA: ',';
This input is valid for above grammar:
a, b, c: integer = 3, 4, 6;
But now if I want this input:
a, b, c, d: integer = 3, 4, 6;
or this:
a, b, c: integer = 3, 4, 6, 1;
becomes invalid due to inequality between amount of ID in idlist and value in explist, how I rewrite my grammar? Tks
grammar Foo; variable: var_ ';' EOF; var_ : identifier var2 exp ; var2: ',' identifier var2 exp ',' | type_info ; type_info : ':' type '='; identifier: Id; Id: [a-zA-Z_]+; type: Id; exp: Num; Num: [0-9]+; WS: [ \t\n\r]+ -> skip;
. Make sure to use full grammars and examples in questions. In Antlr, make sure to use an EOF-terminated start rule.