简体   繁体   English

Flex(词法分析器)无法识别或运算符

[英]Flex (lexical analyzer) not recognizing or operator

I have a problem with flex. 我对flex有问题。 It doesn't recognize the or operator in this rule: 它在此规则中无法识别or运算符:

[0-9A-Za-z]+{CORRECT} | {CORRECT}[0-9A-Za-z]+ [0-9A-Za-z]+{CORRECT}[0-9A-Za-z]+ {...}

If I split it into three rules then it is recognized: 如果我将其划分为三个规则,那么它将被识别:

[0-9A-Za-z]+{CORRECT}  {...}
{CORRECT}[0-9A-Za-z]+ { ...}
[0-9A-Za-z]+{CORRECT}[0-9A-Za-z]+ {...}

To explain myself better the pattern I am trying to recognize is: 为了更好地说明自己,我尝试识别的模式是:

CORRECT [1-9]*_[1-9]*0

And in order for flex to recognize the CORRECT pattern only when it is not surrounded by other characters I have to add these three rules. 为了使flex仅在不被其他字符包围时才识别正确的模式,我必须添加这三个规则。

Full flex code: 完整的弹性代码:

%option noyywrap

%{
#include <stdio.h>
int num_lines=1;

%}

CORRECT [1-9]*_[1-9]*0

%%
{CORRECT} { printf("CORRECT TOKEN:%s\n",yytext); }

[0-9A-Za-z]+{CORRECT}  { printf("ERROR %d:Unidentified symbol: %s\n",num_lines,yytext);}
{CORRECT}[0-9A-Za-z]+ { printf("ERROR %d:Unidentified symbol: %s \n",num_lines,yytext);}
[0-9A-Za-z]+{CORRECT}[0-9A-Za-z]+ { printf("ERROR %d:Unidentified symbol: %s  \n",num_lines,yytext); }

"\n" { num_lines++; }

 " "
 "\t"
 "\r"

 . { printf("ERROR %d:Unidentified symbol: %s \n",num_lines,yytext);}

 %%

 int main(int argc,char **argv)
 {
++argv,--argc;
if(argc>0) 
    yyin=fopen(argv[0],"r");
else
    yyin=stdin;
yylex();
 }

Whitespace is significant in a lex pattern. 空格在lex模式中很重要。 a | b a | b is not the same as a|b . a | ba|b In the troublesome pattern, you have whitespace that I don't think you intended. 在麻烦的模式中,您有空白,我认为您不是故意的。

That said, in my opinion, your 3-pattern solution is easier to read and maintain. 也就是说,我认为,您的3模式解决方案更易于阅读和维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM