English 中文(简体)
在javacc? <>/” (~[>)中正确评估单一线评论的另一个方法
原标题:Another way to correctly skip single line comments in javacc? <"//" (~[" "])* " "> causes multi line comment skip to break
  • 时间:2011-10-26 15:11:17
  •  标签:
  • regex
  • javacc

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 <"//" (~[" "])* " "> 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.

该名教授不再承认多线评论,而是将其与其他(<条码>/*、ID等)的组合加以区别。

以下是我关于多线评论和单一线评论技巧的守则:

SKIP:
{
    "/*" {commentnesting++;} : IN_COMMENT
}

<IN_COMMENT> SKIP :
{
    "/*" {commentnesting++;} 
    | "*/" {commentnesting--;
        if(commentnesting == 0) {
            SwitchTo(DEFAULT);
        }
    }
    | <~[]>
}

SKIP :
{
    <"//" (~["
"])* "
">
}

我的问题是:

  • 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?
最佳回答

认为这样做就行不通,但你似乎遗漏了某些观点。 而不是:

"/*" {commentnesting++;} : IN_COMMENT

......应当:

<"/*"> {commentnesting++;} : IN_COMMENT

......与你的第二个规则类似?

问题回答

我最后做的是:

TOKEN: {
  < COMMENT_END : "*/" >
}
SPECIAL_TOKEN: {
 < COMMENT_START : "/*" >  {         
        /*currently commented contents are dropped, but they can be attached to the special token*/
        do {
            Token nextToken = this.getNextToken();
                if ("*/".equals(nextToken.image)) {
                        break;
                }
        } while (true);
 }
}

它只是一种bit笑,但它在评论含有其他评论甚至诸如:“* */......”等文字的法典时也发挥了作用。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签