本文介绍了如何在LEX/FLEX中编写非贪婪匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FLEX和BISON解析传统语言(类似于"C").除了匹配的字符串,一切都运行良好.

I'm trying to parse a legacy language (which is similar to 'C') using FLEX and BISON. Everything is working nicely except for matching strings.

这种比较奇怪的传统语言不支持在字符串文字中用引号引起来,因此以下都是有效的字符串文字:

This rather odd legacy language doesn't support quoting characters in string literals, so the following are all valid string literals:

"hello"
""
"\"

我正在使用以下规则来匹配字符串文字:

I'm using the following rule to match string literals:

\".*\"            { yylval.strval = _strdup( yytext ); return LIT_STRING; }

不幸的是,这是一个贪婪的匹配,因此它匹配如下代码:

Unfortunately this is a greedy match, so it matches code like the following:

"hello", "world"

作为单个字符串(hello", "world).

通常的非贪婪量词.*?在FLEX中似乎不起作用.有什么想法吗?

The usual non-greedy quantifier .*? doesn't seem to work in FLEX. Any ideas?

推荐答案

只禁止在引号之间使用引号.

Just prohibit having a quote in between the quotes.


\"[^"]*\"

这篇关于如何在LEX/FLEX中编写非贪婪匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:14