Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Epsilon definition in JavaCC

How can I define Epsilon in JavaCC?? I'm trying smething like

< EPSILON: "">

but it doesn't work.

like image 817
Anatoly Avatar asked Oct 19 '25 02:10

Anatoly


2 Answers

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.

like image 158
Nathan Ryan Avatar answered Oct 21 '25 16:10

Nathan Ryan


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 :)

like image 28
marc wellman Avatar answered Oct 21 '25 17:10

marc wellman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!