如何使用 C++ 轻松标记文档或字符串?
我熟悉 Python 中的 ply.lex
模块,但找不到 C++ 的任何内容。是否有可以使用 lex
和 yacc
的 C++ 库?还是有其他更好的库或方法可以轻松做到这一点?
最佳答案
flex
和 lex
相当于 ply.lex
。 flex
示例:
%{
#include <stdio.h>
%}
%option noyywrap
%%
"+" { puts("token: +"); }
"-" { puts("token: -"); }
[0-9]+ { printf("token: %s\n", yytext); }
" " { /* empty */ }
[\n|\r\n\t] { /* empty */ }
. { fprintf(stderr, "Tokenizing error: synatx error '%c'\n", *yytext);
yyterminate(); }
%%
int main(int argc, char **argv)
{
yylex();
return 0;
}
编译:
> flex example.l
> gcc -Wall lex.yy.c -o lex
> lex
100 - 2 + 34
token: 100
token: -
token: 2
token: +
token: 34
>
关于python - 如何在 C++ 中标记字符串(如 Python 中的 ply.lex)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748068/