6
votes

I'm working on defining a grammar using ANTLR4 and Java. For Integers, I want a number that mat be preceeded by a minus sign. I know it is possible to do it like this:

integer: '-' (DIGIT)* | DIGIT* ;

But I was wondering if there is a symbol (similar to the *) that assures a the minus sign occurs zero or one time:

integer: ('-')<some symbol here> (DIGIT)* ;
1

1 Answers

13
votes

Yes, it's the ?, which means zero or once (optional). Also, the * means zero ore more. You probably want + which means once or more:

integer : '-'? DIGIT+ ;