0
votes

I'm using JavaCC to build a complex parser. At one point, I would like to skip all the character I see until a desired token in my grammar... let's take for example the following

/* bla bla bla bla bla bla bla bla */ => I would like to define a kind of grammar like

<OPEN_COMMENT> SKIP ~[] until <CLOSE_COMMENT> I want it to be true even if "bla" is a regular token

Thanks for your help

2

2 Answers

0
votes

You can do it using regular expressions.

You can define tokens and a rule as follows:

TOKEN :
{
< #DIGIT : [ "0"-"9" ] >
| < #ALPHABET: ["a" - "z"] >
| < #CAPSALPHABET: ["A" - "Z"] >
| < WORD: ( <DIGIT> | <ALPHABET> | <CAPSALPHABET>)+ >
}

String comment() :
{
  Token token;
}
{
 token=( <WORD> )+
 {
   return token.toString();
 }
}
0
votes

I think the usual procedure here is to use lexical states with MORE and either SKIP or SPECIAL_TOKEN. You can see an example of this in the way comments are handled by the Java grammar that comes with the JavaCC source distribution.