简体   繁体   English

lex-无法识别的规则-使用正则表达式

[英]lex - unrecognized rule - using regex

I am new to working with lex and just wanted to try and see whether I could try making a file that would locate comments in a C file using regular expressions. 我刚接触lex,只是想尝试看看是否可以尝试制作一个使用正则表达式在C文件中定位注释的文件。

%%

(/\*([^*]|[^]|(\*+([^*/]|[^])))*\*+/)|(//.*){   
    return 5;
}
.   ;

%%

int yywrap(void)
{
    return1;
}

So the idea here would be that everytime a comment is located, it will "return 5", otherwise, nothing happens. 因此,这里的想法是,每次找到评论时,它都会“返回5”,否则,什么也不会发生。

The problem is, the regular expression is not being recognized and i'm met with the error: 问题是,正则表达式无法识别,并且遇到错误:

line 3: unrecognized rule line 3: unrecognized rule line 3: unrecognized rule line 3: unrecognized rule ...etc 第3行:无法识别的规则第3行:无法识别的规则第3行:无法识别的规则第3行:无法识别的规则... etc

Any help would be appreciated, thanks in advance. 任何帮助将不胜感激,在此先感谢。

In (f)lex regular expressions, as in standard Posix regexes, you can include ] in a character class as the first character in the class. 在(f)lex正则表达式中,就像在标准Posix正则表达式中一样,您可以在字符类中包括]作为类中的第一个字符。 (That's true whether or not you use a positive ( [...] ) or negative ( [^...] ) character class.) (无论您使用正( [...] )还是负( [^...] )字符类都是如此。)

So the first [^] starts a character class, which is then terminated by the next ] , and the next one is unterminated. 因此,第一个[^]开始一个字符类,然后由下一个]终止,下一个则不终止。 It's not clear to me what you expected [^] to mean, so I can't offer an alternative suggestion. 我不清楚您希望[^]表示什么,因此我无法提出其他建议。

Also, you need a space before the action, in order to define the end of the regular expression. 另外,为了定义正则表达式的结尾,您需要在操作之前留一个空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM