Lex - 将运算符识别为标记
我正在学习 lex 和海龟语言 我在让 lex 将运算符识别为标记时遇到问题 + , < , =
我将它们存储在文件中 我尝试过这样做,
\+ or "+" or '+' or [+]
使用单引号不会出现任何错误,但它也不匹配,或者我需要在文件中这样做吗?
这是我的文件
fd 3x00
bk
setc 100
int xy3 fd 10 rt 90
rt
lt
setxy
setx
sety
home
seth
pd
pu
ht
st
color
xcor
ycor
heading
random
:=
+
<
,这是我得到的程序
%{
#include <stdio.h>
#include <stdlib.h>
int number;
%}
%%
fd {printf("Keyword %s\n", yytext);}
[^\t\n\r] {}
[0-9]+[a-z]+[0-9]+ {printf("Illegal: 3x00\n");}
[\r\t\n]+ {}
bk {printf("Keyword: %s\n", yytext);}
setc {printf("Keyword: %s\n", yytext);}
[0-9]+ {printf("Number: %s\n", yytext);}
int {printf(" Keyword: %s\n", yytext);}
xy3 {printf("ID: %s\n", yytext);}
fd[0-9]+ {printf("Keyword: %s\n", yytext);
printf("Number %s\n", yytext);}
rt {printf("Keyword: %s\n", yytext);}
lt {printf("Keyword: %s\n",yytext);}
setxy {printf("Keyword: %s\n", yytext);}
setx {printf("Keyword: %s\n", yytext);}
sety {printf("Keyword: %s\n", yytext);}
home {printf("Keyword: %s\n", yytext);}
seth {printf("Keyword: %s\n", yytext);}
pd {printf("Keyword: %s\n", yytext);}
pu {printf("Keyword: %s\n", yytext);}
ht {printf("Keyword: %s\n", yytext);}
st {printf("Keyword: %s\n", yytext);}
color {printf("Keyword: %s\n", yytext);}
xcor {printf("Keyword: %s\n", yytext);}
ycor {printf("Keyword: %s\n", yytext);}
heading {printf("Keyword: %s\n", yytext);}
random {printf("Keyword: %s\n", yytext);}
:= {printf("Keyword: =\n" );}
\+ {printf("Keyword: + \n" );}
\< {printf("Keyword: < \n");}
%%
main( int argc,char** argv)
{
if(argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if(!file)
{
fprintf(stderr, "Could not open %s \n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
}
错误是警告,规则无法匹配。
我做错了什么?
谢谢
I am working on learning lex and the turtle language
I am having problems getting lex to recognize operators as tokens + , < , =
I have them stored in a file
I have tried doing it this way
\+ or "+" or '+' or [+]
I don't get any errors using single quotes but it doesnt match it either or would i need to do that in the file?.
Here is my file
fd 3x00
bk
setc 100
int xy3 fd 10 rt 90
rt
lt
setxy
setx
sety
home
seth
pd
pu
ht
st
color
xcor
ycor
heading
random
:=
+
<
and here is the program
%{
#include <stdio.h>
#include <stdlib.h>
int number;
%}
%%
fd {printf("Keyword %s\n", yytext);}
[^\t\n\r] {}
[0-9]+[a-z]+[0-9]+ {printf("Illegal: 3x00\n");}
[\r\t\n]+ {}
bk {printf("Keyword: %s\n", yytext);}
setc {printf("Keyword: %s\n", yytext);}
[0-9]+ {printf("Number: %s\n", yytext);}
int {printf(" Keyword: %s\n", yytext);}
xy3 {printf("ID: %s\n", yytext);}
fd[0-9]+ {printf("Keyword: %s\n", yytext);
printf("Number %s\n", yytext);}
rt {printf("Keyword: %s\n", yytext);}
lt {printf("Keyword: %s\n",yytext);}
setxy {printf("Keyword: %s\n", yytext);}
setx {printf("Keyword: %s\n", yytext);}
sety {printf("Keyword: %s\n", yytext);}
home {printf("Keyword: %s\n", yytext);}
seth {printf("Keyword: %s\n", yytext);}
pd {printf("Keyword: %s\n", yytext);}
pu {printf("Keyword: %s\n", yytext);}
ht {printf("Keyword: %s\n", yytext);}
st {printf("Keyword: %s\n", yytext);}
color {printf("Keyword: %s\n", yytext);}
xcor {printf("Keyword: %s\n", yytext);}
ycor {printf("Keyword: %s\n", yytext);}
heading {printf("Keyword: %s\n", yytext);}
random {printf("Keyword: %s\n", yytext);}
:= {printf("Keyword: =\n" );}
\+ {printf("Keyword: + \n" );}
\< {printf("Keyword: < \n");}
%%
main( int argc,char** argv)
{
if(argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if(!file)
{
fprintf(stderr, "Could not open %s \n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
}
Error i get is Warning, rule cannot be matched.
What am i doing wrong?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我看到了问题
+
和<
字符将匹配顶部规则 ([^\t\n\r]
)和\+
/\<
规则。由于它们的长度相同,词法分析器将采用第一个匹配项[^\t\n\r]
,因此它不会执行任何操作 ({}
),并且正如它所说,永远不能匹配\+
/\<
规则。排除[^\t\n\r]
规则并将其替换为。 {}
位于令牌列表的最后。I think I see the problem
The
+
and<
characters will match both the top rule ([^\t\n\r]
) and the\+
/\<
rule. Since they are the same length, the lexer will take the first match,[^\t\n\r]
, so it will do nothing ({}
) and it can never match the\+
/\<
rule, as it says. Take the[^\t\n\r]
rule out and replace it with. {}
at the very end of the token list.