flex parse symbol - 复杂符号
flex parse symbol - complex symbol
我是 flex 的新手,我只是想解析一个数字:
%{
#include <iostream>
using namespace std;
extern int yylex();
%}
%option noyywrap
DIGIT [0-9]
ID [a-z][a-z0-9]*
NUM {DIGIT}*"."{DIGIT}* | {DIGIT}+
%%
{NUM} {printf("Number encountered : %s\n",yytext);}
%%
int main(int, char**) {
while (yylex());
}
当我用
替换 Number encountered
行时
{DIGIT}+ { printf( "Number %s\n", yytext);}
{DIGIT}*"."{DIGIT}* { printf( "Number %s\n", yytext);}
我得到输出,但现在我没有。
我的意思是它没有正确解析。它什么都不打印。
如何解决这个问题?
当前代码:
%{
#include <iostream>
using namespace std;
extern int yylex();
%}
%option noyywrap
WS [ \t\n]+
DIGIT [0-9]
NUM ({DIGIT}*"."{DIGIT}*|{DIGIT}+)
%%
NUM {printf("num : %s\n",yytext);}
%%
模式不能包含未转义的空格,因此您第一次尝试的模式无效。
删除 |
周围的空格。
简单的就是这样做:
NUMBER {N}+|{N}+"."{N}+|"."{N}+|{N}+"."
我是 flex 的新手,我只是想解析一个数字:
%{
#include <iostream>
using namespace std;
extern int yylex();
%}
%option noyywrap
DIGIT [0-9]
ID [a-z][a-z0-9]*
NUM {DIGIT}*"."{DIGIT}* | {DIGIT}+
%%
{NUM} {printf("Number encountered : %s\n",yytext);}
%%
int main(int, char**) {
while (yylex());
}
当我用
替换Number encountered
行时
{DIGIT}+ { printf( "Number %s\n", yytext);}
{DIGIT}*"."{DIGIT}* { printf( "Number %s\n", yytext);}
我得到输出,但现在我没有。
我的意思是它没有正确解析。它什么都不打印。
如何解决这个问题?
当前代码:
%{
#include <iostream>
using namespace std;
extern int yylex();
%}
%option noyywrap
WS [ \t\n]+
DIGIT [0-9]
NUM ({DIGIT}*"."{DIGIT}*|{DIGIT}+)
%%
NUM {printf("num : %s\n",yytext);}
%%
模式不能包含未转义的空格,因此您第一次尝试的模式无效。
删除 |
周围的空格。
简单的就是这样做:
NUMBER {N}+|{N}+"."{N}+|"."{N}+|{N}+"."