10

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)* ;
peter.cyc
  • 1,763
  • 1
  • 12
  • 19
Elias
  • 432
  • 6
  • 17

1 Answers1

18

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+ ;
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288