仅在 yytext 中的 flex 中匹配字符串的一部分
我是新手,但我想知道我是否可以使用 Flex 用正则表达式解析某些内容,而 yytext 仅是匹配序列的一部分。例如:@abcd{efgh,
。我想匹配 abcd
一次,然后使用它 efgh,
但我需要使用 @ 和 { 来匹配它们。这是可能的还是我必须稍后在 C 中完全处理它?
I'm a newbie but I would like to know if I can, with flex, parse something with regex in a way yytext would only be part of the matched sequence. For example: @abcd{efgh,
. I would like to match abcd
once and use it then efgh,
but I need to use the @ and the { to match them. Is this possible or do I have to process it entirely later in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将以下上下文与“/”运算符一起使用。例如,
仅当字符串“abcd”后跟“efgh”时,才会匹配字符串“abcd”,后一个字符串保留在输入中,因此它将是下一个匹配标记(的一部分)。
我不确定你到底在问“@”和“{”什么——你想匹配它们但忽略它们,还是想将它们作为单独的标记返回?对于前者,您可以使用
"@abcd"/"{efgh"
然后使用 yytext+1 来获取“abcd”。后者更复杂,但可以使用 flex 的状态来完成。您可能会这样做:仅当“abcd”紧接在“@”自身匹配的“@”之后时才匹配“abcd”。
您可以在具有启动状态的 Flex 中执行许多复杂的操作,但通常尝试解析非常规语言是一个坏主意 - 您最好使用解析工具(例如 bison)来执行此操作。
You can use following context with the '/' operator. For eaxmple,
will match the string "abcd" only if its followed by "efgh", with the latter string being left on the input, so it will be (part of) the next matched token.
I'm not sure exactly what you're asking about with the "@" and "{" however -- do you want to match them but just ignore them, or do you want to return them as seperate tokens? For the former you could use
"@abcd"/"{efgh"
and then just use yytext+1 to get "abcd". The latter is more complex, but it can be done using flex's states. You might do something like:to match an "abcd" only if its immediately after an "@" that was matched by itself.
You can do lots of complex things in flex with start states, but generally trying to parse a non-regular language is a bad idea -- you're better off using a parsing tool such as bison for that.