Ichimov Catalin
Ichimov Catalin

Reputation: 13

Antlr grammar won't work

My grammar won't accept inputs like "x = 4, xy = 1"

grammar Ex5;

prog : stat+;
stat : ID '=' NUMBER;

LETTER : ('a'..'z'|'A'..'Z');
DIGIT : ('0'..'9');
ID : LETTER+;
NUMBER : DIGIT+;
WS : (' '|'\t'| '\r'? '\n' )+ {skip();};

What do i have to do to make it accept a multitude of inputs like ID = NUMBER?? Thank you in advance.

Upvotes: 0

Views: 47

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170138

You'll have to account for the comma, ',', inside your grammar. Also, since you (most probably) don't want LETTER and DIGIT tokens to be created since they are only used in other lexer rules, you should make these fragments:

grammar Ex5;

prog : stat (',' stat)*;
stat : ID '=' NUMBER;

ID     : LETTER+;
NUMBER : DIGIT+;

fragment LETTER : 'a'..'z' | 'A'..'Z';
fragment DIGIT  : '0'..'9';

WS : (' '|'\t'| '\r'? '\n')+ {skip();};

Upvotes: 1

Related Questions