gpt4 book ai didi

python - 如何在 C++ 中标记字符串(如 Python 中的 ply.lex)?

转载 作者:行者123 更新时间:2023-11-28 02:44:56 25 4
gpt4 key购买 nike

如何使用 C++ 轻松标记文档或字符串?我熟悉 Python 中的 ply.lex 模块,但找不到 C++ 的任何内容。是否有可以使用 lexyacc 的 C++ 库?还是有其他更好的库或方法可以轻松做到这一点?

最佳答案

flexlex相当于 ply.lexflex 示例:

%{
#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com