【发布时间】:2014-06-29 06:34:17
【问题描述】:
我正在尝试在 flex 中创建一个简单的状态机,它必须确保跨越多行的字符串必须具有 \ 用于换行。具体来说:
"this is \
ok"
"this is not
ok"
第一个有效。第二个不是。
我有以下状态机:
expectstring BEGIN(expectstr);
<expectstr>[^\n] {num_lines++;}
<expectstr>\ {flag = true;}
<expectstr>\n {printf("%s\n", flag ? "True" : False);}
但是当我尝试编译这个状态机时,flex 告诉我\ 的规则无法匹配。这是为什么呢?
我查看了this,但无法弄清楚。
【问题讨论】:
-
反斜杠是转义字符。像这样使用它 \\
-
@Eduardo 仍然说规则无法匹配。
-
反斜杠不是换行符。因此,首先出现的 not-a-newline 规则将匹配反斜杠。
-
@rici 这就是我所怀疑的。所以已经用
[^\n\\]替换了[^\n],但这也失败了。 -
@rici 经过深思熟虑,我将
[^\n]替换为[^\n|^\\],然后用<expectstr>\ ` which is working. But am I correct with[^\n | ^\]? Concretely, is my reasoning here that match everything that is not\n` 而不是 ``?
标签: c++ compiler-construction flex-lexer lexical-analysis