莱克斯YACC - 表达式中的空格

发布于 2025-01-07 01:54:33 字数 1801 浏览 1 评论 0原文

我正在读取文件,由于某种原因,当我尝试表达式时出现语法错误 就像 5+5 但是,如果我这样做 5 + 5 它效果很好。我很困惑为什么它会这样做?

这是我的 lex 文件(我将省略在文件中读取的 main):

%{

 #include "y.tab.h"
 #include "stdio.h"
 #include <stdlib.h>

%}
%%
(\/\*([^*]|(\*+([^*/]|[\r\n])))*\*+\/)+ {}
\/\/[^\n]*\n          {}
fd                    { return FD; }
[\r\t\n]+             {}
[ ]*                  {}
bk                    { return BK;}
setc                  {return SETC;}
[-+]?[0-9]+           { yylval = atoi(yytext); return NUMBER;}
fd[0-9]+              { }
rt                    {return RT;}
pink                  {return COLOR_TYPE;}
magenta               {return COLOR_TYPE; }
if                    {return IF; }
ifelse                {return IFELSE; }
\[                    {return LBRACKET; }
\]                    {return RBRACKET; }
\<                    {return LESS_THAN; }
\>                    {return GREATER_THAN; }
\+                    {return PLUS; }
\-                    {return MINUS; }
\/                    {return DIVIDE; }
\*                    {return MULT; }
\(                    {return LPAREN;}
\)                    {return RPAREN;}
\=                    {return EQ;}

%%

这是我的 yacc 文件的一部分,用于处理表达式:

expr    : NUMBER     { printf("EXPR-->NUMBER: %d\n", $1);}
   |expr PLUS expr   {$$ = $1 + $3; printf("EXPR-->expression PLUS expression: %d\n", $$);}
   |expr DIVIDE expr {$$ = $1 / $3; printf("EXPR-->expression DIVIDE expression %d\n", $$);}
   |expr MULT expr   {$$ = $1 * $3; printf("EXPR-->expression MULTIPLY expression %d\n", $$);}
   |expr MINUS expr  {$$ = $1 - $3; printf("EXPR-->expression MINUS expression %d\n", $$);}
   |COLOR_TYPE       {printf("EXPR-->COLOR\n");}
   ;

问题是否出在 lex 文件中?

I am reading in a file and for some reason i get a syntax error when i try an expression
like 5+5 however, if i do this 5 + 5 it works good. I am baffled why it would do this?

Here is my lex file( i will leave out main that reads in a file):

%{

 #include "y.tab.h"
 #include "stdio.h"
 #include <stdlib.h>

%}
%%
(\/\*([^*]|(\*+([^*/]|[\r\n])))*\*+\/)+ {}
\/\/[^\n]*\n          {}
fd                    { return FD; }
[\r\t\n]+             {}
[ ]*                  {}
bk                    { return BK;}
setc                  {return SETC;}
[-+]?[0-9]+           { yylval = atoi(yytext); return NUMBER;}
fd[0-9]+              { }
rt                    {return RT;}
pink                  {return COLOR_TYPE;}
magenta               {return COLOR_TYPE; }
if                    {return IF; }
ifelse                {return IFELSE; }
\[                    {return LBRACKET; }
\]                    {return RBRACKET; }
\<                    {return LESS_THAN; }
\>                    {return GREATER_THAN; }
\+                    {return PLUS; }
\-                    {return MINUS; }
\/                    {return DIVIDE; }
\*                    {return MULT; }
\(                    {return LPAREN;}
\)                    {return RPAREN;}
\=                    {return EQ;}

%%

Here is part of my yacc file that deals with the expression:

expr    : NUMBER     { printf("EXPR-->NUMBER: %d\n", $1);}
   |expr PLUS expr   {$ = $1 + $3; printf("EXPR-->expression PLUS expression: %d\n", $);}
   |expr DIVIDE expr {$ = $1 / $3; printf("EXPR-->expression DIVIDE expression %d\n", $);}
   |expr MULT expr   {$ = $1 * $3; printf("EXPR-->expression MULTIPLY expression %d\n", $);}
   |expr MINUS expr  {$ = $1 - $3; printf("EXPR-->expression MINUS expression %d\n", $);}
   |COLOR_TYPE       {printf("EXPR-->COLOR\n");}
   ;

would the problem be in the lex file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2025-01-14 01:54:33

分词器(词法分析器)将这两个值返回给解析器:5+5。根据你的语法(和逻辑),这是无效的。

我认为,你最好改变你的词法分析器并将运算符的规则向上移动。 (这意味着至少高于返回 NUMBER 的规则)。

编辑: 经过一番思考(编辑 #2: 以及 Jerry Coffin 的非常有用的评论),我建议更改 NUMBER 的词汇规则到[0-9]+。为了使解析器仍然接受“+123”或“-123”等输入,您应该将其添加到语法中:

%left PLUS MINUS ...
%right UNARY

%%

expr   : number
       | expr PLUS expr
      ...
       ;

number : PLUS NUMBER %prec UNARY {$ = $2}
       | MINUS NUMBER %prec UNARY {$ = -$2}
       | NUMBER 
       ;

这将允许一元 +- 在任何数字之前,同时仍为运算符 +- 提供更高的优先级。

The tokenizer (lexer) returns these two to the parser: 5 and +5. Which by your grammar (and logically) is invalid.

I think, you'd be better off, to alter your lexer and move the rules for operators up. (That means at least above the rule returning NUMBER).

EDIT: After some thinking (EDIT #2: and a more than useful comment by Jerry Coffin), I suggest changing the lexical rule for NUMBER to [0-9]+. In order for the parser to still accept input like "+123" or "-123", you should add this to your grammar:

%left PLUS MINUS ...
%right UNARY

%%

expr   : number
       | expr PLUS expr
      ...
       ;

number : PLUS NUMBER %prec UNARY {$ = $2}
       | MINUS NUMBER %prec UNARY {$ = -$2}
       | NUMBER 
       ;

This will allow for an unary + or - before any number while still giving the operators + and - a higher precedence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文