莱克斯YACC - 表达式中的空格
我正在读取文件,由于某种原因,当我尝试表达式时出现语法错误 就像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分词器(词法分析器)将这两个值返回给解析器:
5
和+5
。根据你的语法(和逻辑),这是无效的。我认为,你最好改变你的词法分析器并将运算符的规则向上移动。 (这意味着至少高于返回
NUMBER
的规则)。编辑: 经过一番思考(编辑 #2: 以及 Jerry Coffin 的非常有用的评论),我建议更改
NUMBER
的词汇规则到[0-9]+
。为了使解析器仍然接受“+123”或“-123”等输入,您应该将其添加到语法中:这将允许一元
+
或-
在任何数字之前,同时仍为运算符+
和-
提供更高的优先级。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:This will allow for an unary
+
or-
before any number while still giving the operators+
and-
a higher precedence.