how to use the empty word in JAVACC as a token?

I am setting up a parser using JavaCC and I want to use the empty word as a token (the epsilon word )

I tried to use a non terminal

    void Empty() : 
     {}
     {
     } 

but i am getting an error

i tried a token < EMPTY : "" > nothing worked

I want a token to represent an empty word

Upvotes: 0

Views: 1076

Answers (2)

I found a solution I used { } and it worked fine

Upvotes: -1

Theodore Norvell
Theodore Norvell

Reputation: 16221

Do you want a token that is an empty sequence of characters or a nonterminal that matches an empty sequence of tokens?


As explained in the JavaCC FAQ, a nonterminal that matches an empty sequence of tokens would be

void Empty() : {}
{
     {}
} 

The syntax of the JavaCC grammar files does not permit expansion_choices to be empty. But you can put an empty Java block as above.

However, if you have this nonterminal, then Empty() is the same as {}. For example, I could write

 void SomeNonterminal() : {}
 {
     A() B() C()
 |
     Empty() 
 }

or I could write

 void SomeNonterminal() : {}
 {
     A() B() C()
 |
     {}
 }

It's logically the same thing.


A token that matches the empty sequence of characters is easy to define

TOKEN: { <EMPTY : ""> }

However, this is probably a bad idea. If this token were to match, then it would match again on the next iteration of the lexer, and you'd have an infinite sequence of these token!

The only time this would make sense is to take the lexer out of one lexical state and into another. E.g.

 <SOMESTATE> TOKEN: { <EMPTY : ""> : DEFAULT }

Upvotes: 2

Related Questions