开发者

Parser in JavaCC and SKIP instruction

I'm using JavaCC to build a complex parser. At one point, I would like to skip all the chara开发者_开发知识库cter 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


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();
 }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜