Shift减少冲突,财务计算器使用编译器
Shift reduce conflict, financial calculator using compiler
每当我添加'(' term ')' { $$=$2; }
时,我得到shift减少冲突我要做的操作是:(5) + (5)
,(5.5) + (5)
, ((5.5) + (5)) - (5)
, ((5.5) / (5.5)) * (5)
,等
我对shift - reduce解析有点困惑,我们的教授只教我们如何在示例语法中shift - reduce解析,但不教如何将其应用于cpp。
我用的是野牛。
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylex();
#define YYERROR_VERBOSE
void yyerror(const char *msg){
printf("ERROR(PARSER): %sn", msg);
}
double vars[26];
%}
%union{
double dvalue;
int varindex;
}
%token <dvalue> NUMBER
%token <varindex> NAME
%type <dvalue> expression
%type <dvalue> term
%type <dvalue> varornum
%%
statementlist : statement 'n'
| statement 'n' statementlist
;
statement : NAME '=' expression { vars[$1] = $1;}
| expression { printf("ANS: %fn", $1); }
;
expression: '(' expression ')' { $$ = $2;}
| expression '+' term { $$ = $1 + $3;}
| expression '-' term { $$ = $1 - $3;}
| term { $$ = $1; }
;
term : '(' term ')' { $$ = $2;}
| term '*' varornum { $$ = $1 * $3;}
| term '/' varornum {
if ($3==0) {
printf("ERROR: Divide %f by zeron", $1);
} else {
$$ = $1 / $3;
}
}
| varornum { $$ = $1; }
;
varornum : NUMBER { $$ = $1; }
| NAME { $$ = vars[$1];}
;
%%
main(){
yyparse();
}
您的语法中已经包含了'(' expr ')' { $$=$2; }
。添加'(' term ')' { $$=$2; }
会与此规则产生冲突。你只需要其中一个。expr
是term
还是term
和expr
?
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我需要知道编译器如何在cpp中使用析构函数
- 编译器如何区分std::vector的构造函数
- CLANG 编译器 说:变量"PTR"可能未初始化
- 告诉c++编译器该参数没有别名
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么所有C++编译器都会崩溃或挂起此代码
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- Shift减少冲突,财务计算器使用编译器
- 我正在使用c++制作计算器,但编译器给出了一个错误