ANTLR4: zero or one times
Asked Answered
J

1

11

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)* ;
Jea answered 15/2, 2018 at 14:27 Comment(0)
B
20

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+ ;
Bingaman answered 15/2, 2018 at 14:39 Comment(1)
It worked, thanks! Also changed * to + thanks for the feedback.Jea

© 2022 - 2024 — McMap. All rights reserved.