Ghiloufi
Ghiloufi

Reputation: 321

How to write a rule to define a method in ANTLR

I am creating a DSL with ANTLR and I want to define the following syntax

// study without parameters
study()
// study  with a single parameter
study(x = 1)
// study with several parameters
study(x = 1, x = 2)

here my grammer ,it allows the following input : study(x=1x=2)

study: 'study' '(' ( assign* | ( assign (',' assign)*) ) ')' NEWLINE;
assign: ID '=' (INT  | DATA );
INT :   [0-9]+ ;
DATA    : '"' ID '"' | '"' INT '"';
ID  :   [a-zA-Z]+ ;

Upvotes: 1

Views: 170

Answers (1)

sepp2k
sepp2k

Reputation: 370162

Your grammar allows study(x=1x=2) because assign* matches x=1x=2. If you don't want to allow input like that, you should remove the assign* alternative. To allow empty parameter lists, you can just make everything between the parentheses optional:

study: 'study' '(' (assign (',' assign)*)? ')' NEWLINE;

Upvotes: 1

Related Questions