How can I define Epsilon in JavaCC?? I'm trying smething like
< EPSILON: "">
but it doesn't work.
You can define a token to match the empty string if the token manager then switches to a lexical state in which at least one character will be consumed before returning to the state in which the empty string was matched. So:
TOKEN : {
< EMPTY : "" >
: MATCH_NON_EMPTY // change state, or infinite matches of the empty string could result
}
<MATCH_NON_EMPTY>
TOKEN : {
< NON_EMPTY : ~[] >
}
void Start() : { } {
<EMPTY> <NON_EMPTY>
}
That will work fine given any single-character input.
I should mention a caveat, however. It seems that a match of EOF is considered to be "longer" than a match of the empty string, so an empty string cannot be matched immediately before EOF without some lexical action magic when EOF is matched.
Since I was also looking for a way to use the empty word epsilon I came up with the following approach.
I simply used an empty production rule such as:
void Empty () : { } { System.out.prntln("epsilon"); }
Hope it works - at least for now it seems so ...
Addendum:
It works pretty fine :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With