Lex:编写正则表达式时在“[”标记之前的预期表达式
Lex: expected expression before ‘[’ token when writing regular expressions
我是 lex/yacc 的新手,正在学习本教程:https://www.youtube.com/watch?v=54bo1qaHAfk
这是我的 lex 文件
%{
#include "main.h"
#include <stdio.h>
%}
%%
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
"&" {return RUN_DAEMON;}
"|" {return SYM_PIPE;}
">" {return RED_STDOUT;}
"<" {return RED_STDIN;}
">>" {return APP_STDOUT;}
[ \t\n]+ {;}
. {printf("unexpected character\n");}
%%
int yywrap(){
return 1;
}
但是在 运行 lex
命令之后我尝试用 gcc
编译 lex.yy.c
它用这个错误向我发送垃圾邮件
sbash.l: In function ‘yylex’:
sbash.l:7:5: error: expected expression before ‘[’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: error: ‘a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: note: each undeclared identifier is reported only once for each function it appears in
sbash.l:7:14: error: ‘_a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:17: error: ‘zA’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:20: error: ‘Z0’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:29: error: expected expression before ‘{’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:13:7: error: stray ‘\’ in program
[ \t\n]+ {;}
^
sbash.l:13:9: error: stray ‘\’ in program
[ \t\n]+ {;}
不幸的是,我什至用谷歌搜索都找不到问题所在(许多示例的表达式与我的代码完全相同)。
我的 lex 版本是 2.6.1,在 CentOS8
如 Flex manual chapter on flex input file format 中所述,模式规则必须从左边距开始:
The rules section of the flex input contains a series of rules of the form:
pattern action
where the pattern must be unindented and the action must begin on the same line. (Some emphasis added)
规则部分的缩进行只是按原样通过。特别是,第一条规则之前的缩进行被插入到 yylex
函数的顶部,这通常很有用。但是 flex 不会尝试验证以这种方式包含的代码是否有效;编译生成的扫描器时会检测到错误。
我是 lex/yacc 的新手,正在学习本教程:https://www.youtube.com/watch?v=54bo1qaHAfk
这是我的 lex 文件
%{
#include "main.h"
#include <stdio.h>
%}
%%
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
"&" {return RUN_DAEMON;}
"|" {return SYM_PIPE;}
">" {return RED_STDOUT;}
"<" {return RED_STDIN;}
">>" {return APP_STDOUT;}
[ \t\n]+ {;}
. {printf("unexpected character\n");}
%%
int yywrap(){
return 1;
}
但是在 运行 lex
命令之后我尝试用 gcc
编译 lex.yy.c
它用这个错误向我发送垃圾邮件
sbash.l: In function ‘yylex’:
sbash.l:7:5: error: expected expression before ‘[’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: error: ‘a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:7:6: note: each undeclared identifier is reported only once for each function it appears in
sbash.l:7:14: error: ‘_a’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:17: error: ‘zA’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:20: error: ‘Z0’ undeclared (first use in this function)
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^~
sbash.l:7:29: error: expected expression before ‘{’ token
[a-zA-Z][_a-zA-Z0-9]* {return IDENTIFIER;}
^
sbash.l:13:7: error: stray ‘\’ in program
[ \t\n]+ {;}
^
sbash.l:13:9: error: stray ‘\’ in program
[ \t\n]+ {;}
不幸的是,我什至用谷歌搜索都找不到问题所在(许多示例的表达式与我的代码完全相同)。
我的 lex 版本是 2.6.1,在 CentOS8
如 Flex manual chapter on flex input file format 中所述,模式规则必须从左边距开始:
The rules section of the flex input contains a series of rules of the form:
pattern action
where the pattern must be unindented and the action must begin on the same line. (Some emphasis added)
规则部分的缩进行只是按原样通过。特别是,第一条规则之前的缩进行被插入到 yylex
函数的顶部,这通常很有用。但是 flex 不会尝试验证以这种方式包含的代码是否有效;编译生成的扫描器时会检测到错误。