How do I write a non-greedy match in LEX / FLEX?

我的未来我决定 提交于 2019-12-05 00:37:20
horsh

Just prohibit having a quote in between the quotes.

\"[^"]*\"

Backslash escaped quotes

The following also allows it:

\"(\\.|[^\n"\\])*\" {
        fprintf( yyout, "STRING: %s\n", yytext );
    }

and disallows for newlines inside of string constants.

E.g.:

>>> "a\"b""c\d"""
STRING: "a\"b"
STRING: "c\d"
STRING: ""

and fails on:

>>> "\"

When implementing such C-like features, make sure to look for existing Lex implementations, e.g.: http://www.lysator.liu.se/c/ANSI-C-grammar-l.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!