所以我在 javacc 和跳过注释方面遇到了问题。我有一个多行注释跳过,它本身可以包含多个注释(注释是 /*
和 */
中出现的任何内容),我也使用此代码段<"//" (~["\n"])* "\n">
跳过单行注释。两者的功能彼此独立,但当组合起来时,单行注释似乎打破了我的多行注释。
解析器不再识别多行注释,而是将其解析为 OTHER(/*
)、ID 等的组合。
下面是我的多行注释和单行注释跳过的代码:
SKIP:
{
"/*" {commentnesting++;} : IN_COMMENT
}
<IN_COMMENT> SKIP :
{
"/*" {commentnesting++;}
| "*/" {commentnesting--;
if(commentnesting == 0) {
SwitchTo(DEFAULT);
}
}
| <~[]>
}
SKIP :
{
<"//" (~["\n"])* "\n">
}
我的问题是:
- 当我相对较新的眼睛看来单行注释似乎具有完全不同的正则表达式时,单行注释如何导致多行注释中断?
- 他们是否有一种编写单行注释skip的方法,以便它执行与以前相同的功能,但当两者一起使用时不会破坏多行注释?
So I am having a problem with javacc and skipping comments. I have a multi line comment skip that can contain multiply comments within itself (A comment is anything that appears within /*
and a */
), I also use this code segement <"//" (~["\n"])* "\n">
to skip a single line comment. Both function independently of one another but when combined, the single line comment seems to break my multi line comment.
The parser no longer recognises the multi line comment and instead parses it as a combination of OTHER(/*
), ID etc.
Below is my code for the multi line comment and single line comment skip:
SKIP:
{
"/*" {commentnesting++;} : IN_COMMENT
}
<IN_COMMENT> SKIP :
{
"/*" {commentnesting++;}
| "*/" {commentnesting--;
if(commentnesting == 0) {
SwitchTo(DEFAULT);
}
}
| <~[]>
}
SKIP :
{
<"//" (~["\n"])* "\n">
}
My Questions are:
- How can the single line comment cause the multi line comment to break, when they to my relatively new eyes appear to have completely different regexes?
- Is their a way to write the single line comment skip , so that it performs the same function as before but doesn't break the multi line comment when the two are used together?
发布评论
评论(2)
看起来应该可行,但你似乎缺少一些尖括号。而不是:
...不应该是:
...与您的第二条规则类似吗?
Looks like that should work, except you seem to be missing some angle brackets. Instead of:
...shouldn't it be:
...and similarly with your second rule?
我最终做了类似的事情:
这有点hacky,但在注释包含其他注释甚至字符串的代码时它也有效:“/* */”....
I ended up doing something like:
It is a bit hacky but it also works when commenting code that contains other comments and even strings like: "/* */"....