【发布时间】:2014-11-23 10:14:38
【问题描述】:
我想为动态类型语言创建解析器。
在我的野牛文件中,我有一个 runtimetyped 的规则,它是一个变量名或函数调用。
runtimetyped : T_ID { $$ = create_identifier($1); }
| call { $$ = $1; }
;
我还想在编译时做一些基本的类型检查。 f.e.我不想允许这样的事情
x = "string" + 42 <= true;
在源代码中,我想创建一个编译时错误。
但是像
这样的东西s = "string";
i = 42;
b = true;
x = s + i <= b;
应该会产生运行时错误。
我的方法是在语法中使用不同的表达方式:
expression : bool_expression
| math_expression
| string_expression
;
这些expressions 中的任何一个都是由terms、factors 等构建的。factor 也可以始终是 runtimetyped,这会导致 reduce/reduce 错误。
math_factor : numeric_literal { $$ = $1; }
| runtimetyped { $$ = $1; }
| T_LPAREN math_expression T_RPAREN { $$ = $2; }
;
bool_factor : T_BOOL { $$ = create_bool($1); }
| runtimetyped { $$ = $1; }
| compare { $$ = $1; }
| T_LPAREN bool_expression T_RPAREN { $$ = $2; }
;
string_expression : T_STRING { $$ = $1; }
| runtimetyped { $$ = $1; }
| string_expression T_STROP string_expression { $$ = create_expression($2, $1, $3); }
;
我使用bison -v parser.y 运行它。
谁能给我一个关于如何解决这个冲突和/或究竟是什么导致冲突的提示。
提前致谢。
【问题讨论】:
标签: compiler-construction bison yacc dynamic-typing reduce-reduce-conflict