简体   繁体   English

在词法分析中如何检测字符串?

[英]How to detect string during lexical analysis?

I am using some syntax to detect string during lexical analysis 我在词法分析期间使用某种语法来检测字符串

"".*""    return TOK_STRING; 

but this is not working. 但这不起作用。

I think you want 我想你要

\".*\"

but be aware that . 但是要注意 in flex does not match newlines. 在Flex中不匹配换行符。 And, as @chqrlie mentions in a comment, it does match " , so it will match to the end of the last string, and not the current one. 而且,正如@chqrlie在评论中提到的,它确实匹配 ,因此它将匹配到最后一个字符串的末尾,而不是当前字符串的末尾。

So a better pattern might be: 因此,更好的模式可能是:

\"[^"]*\"

( [^"] matches any character including newlines, except " ). [^"]匹配包括换行符在内的任何字符, ”“除外。

But then you have no way to include a " in a string. So you will have to decide what syntax that should be. If you wanted to implement SQL style, with doubled quotes representing a single quote inside a string, you could use 但是,那么您就没有办法在字符串中包含“” 。因此,您必须确定应该使用的语法。如果要实现SQL样式,双引号表示字符串中的单引号,则可以使用

\"([^"]|\"\")*\"

For the possibly more common backslash escape: 对于可能更常见的反斜杠转义:

\"([^"]|\\(.|\n))*\"

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

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