当时我正在写LOLCODE解释器,结果遇到了一个问题。在LOLCODE函数调用中,如下所示:
<func_name> <arg1> <arg2> ....
myfunc 1 2 3 让我们考虑一下,myfunc需要三个args。我不知道如何支持这样的建筑:
VISIBLE myfunc 1 2 3 4我希望野牛能这样解析它:
myfunc 1 2 3 -> function
4 -> expr我将函数调用声明为:
ID expr_list { $$ = new ExprFunctionCall($1, $2); } 其中expr_list是:
expr_list
: expr_list expr { $$->putExpr($2); }
| /* epsilon */ { $$ = new ExprList(); }
;如果我知道已声明的函数性,我如何告诉野牛该停在哪里?
发布于 2013-12-08 08:08:40
你不能在野牛里这么做。反正也不干净。Bison是一个解析上下文无关语言的工具,而您的工具依赖于上下文。
有时,您可以弯曲规则并在其中插入一些上下文依赖项,比如C和C++,但在您的示例中,可能值得研究其他解析器生成器。如果无论如何都要这样做,那么引入一个显式令牌来结束函数调用,并引入另一个助手符号来处理函数参数。这个特殊的令牌不是在源中找到的,它是由lexer根据解析器的请求注入的。
规则应该大致类似于这样(未经测试的语法很可能在第一次尝试时不起作用,但一般的想法是这样的):
call
: function expr_list { ... }
;
function
: ID { ... push function arity on some stack somewhere }
expr_list
: expr_list pre_expr expr { ... }
| pre_expr END_LIST { ... }
;
pre_expr
: /* epsilon */ { ... decrease function arity on the top of the stack
if it's zero,
tell the lexer to issue END_LIST token next
and pop the arity off the stack
}
;https://stackoverflow.com/questions/20450982
复制相似问题