I am trying to adapt a PDDL parser and there is a token that is optional to be exist. Suppose these are 2 options I want to read. (figure1)
(node1)
(node1 :isGood) // :isGood is optional to be exist
To support both situations, I developed code in .jj like this figure2 below. It works correctly; however, it is an inappropriate way to write like this. (figure2)
<LEFT_BRACKET>
<NODE>
(LOOKAHEAD(2) <IS_GOOD> <RIGHT_BRACKET> | <RIGHT_BRACKET>)
The code in .jj that I actually want it to be should be like in this figure3 below. From figure3, it parses from .jj successfully but it is unable to parse the scripts in the figure1 from which I received unexpected token ")"
instead. (figure3)
<LEFT_BRACKET>
<NODE>
(LOOKAHEAD(2) <IS_GOOD>) // this is where it should support an optional token
<RIGHT_BRACKET>
Question: How to write the code in .jj to support both conditions in figure1? In other words, how to make it support the optional token :isGood
that may not be exist with an appropriate approach of programming.
I probably don't know how the LOOKAHEAD works. Any solution to read the figure1 is appreciate.
You don't need the lookahead specification in this case. Just use
<LEFT_BRACKET>
<NODE>
[ <IS_GOOD> ]
<RIGHT_BRACKET>
or
<LEFT_BRACKET>
<NODE>
( <IS_GOOD> )?
<RIGHT_BRACKET>
or
<LEFT_BRACKET>
<NODE>
( <IS_GOOD> | {} )
<RIGHT_BRACKET>
They mean the same thing.