user840718
user840718

Reputation: 1611

Regex in ANTLR4

I want to generate strings like these:

tablename(attr1, attr2)    
tablename(attr1, attr2, attr3)
tablename(attr1, attr2, attr3, attr4)

The problem is that with the wildcards operators (*,?,+) I cannot control the cardinality I want, because I need at least two. So, strings like this one are not accepted:

tablename(attr1)
tablename()

Moreover, I want to reproduce also the commas but not for the last attribute. Is there a way to do it ANTLR4?

Upvotes: 1

Views: 100

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53337

Formulate the possible variantions explicitly. For instance:

table: id OPEN_PAR attribute CLOSE_PAR
       | id OPEN_PAR attribute COMMA attribute CLOSE_PAR
       | id OPEN_PAR attribute COMMA attribute COMMA attribute CLOSE_PAR
       | id OPEN_PAR attribute COMMA attribute COMMA attribute COMMA attribute CLOSE_PAR
       ... etc.
;

Upvotes: 1

Related Questions