Skip to main content
edited tags
Link
Duy Duy
  • 621
  • 1
  • 6
  • 9
added 48 characters in body
Source Link
Duy Duy
  • 621
  • 1
  • 6
  • 9

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

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;

becomes invalid due to inequality between amount of ID in idlist and value in explist, how I rewrite my grammar? Tks

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

Source Link
Duy Duy
  • 621
  • 1
  • 6
  • 9

write ANTLR regex when declaring variables list

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;

becomes invalid due to inequality between amount of ID in idlist and value in explist, how I rewrite my grammar? Tks