开发者

Parser in JavaCC and SKIP instruction

开发者 https://www.devze.com 2023-01-19 05:28 出处:网络
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

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

精彩评论

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

关注公众号