对没有终端,只有非终端的规则使用yacc优先级 [英] Using yacc precedence for rules with no terminals, only non-terminals

查看:161
本文介绍了对没有终端,只有非终端的规则使用yacc优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我这个规则吗?

could some one help me on this i have this rule :

e   : T_NUM                                            { $$ = mk_int($1);}
| T_POP e[l]                                       { $$ = mk_app(mk_op(POP),$l);}
| T_NEXT e[l]                                      { $$ = mk_app(mk_op(NEXT),$l);}
| "{" e[x] "," e[y] "}"                            { $$ = mk_point($x,$y);}
| e T_PLUS e                                       { $$ = mk_app(mk_app(mk_op(PLUS),$1),$3);}
| e T_MINUS e                                      { $$ = mk_app(mk_app(mk_op(MINUS),$1),$3);}
| e T_DIV e                                        { $$ = mk_app(mk_app(mk_op(DIV),$1),$3);}
| e T_MULT e                                       { $$ = mk_app(mk_app(mk_op(MULT),$1),$3);}
| e T_LEQ e                                        { $$ = mk_app(mk_app(mk_op(LEQ),$1),$3) ;}
| e T_LE e                                         { $$ = mk_app(mk_app(mk_op(LE),$1),$3) ;}
| e T_GEQ e                                        { $$ = mk_app(mk_app(mk_op(GEQ),$1),$3) ;}
| e T_GE e                                         { $$ = mk_app(mk_app(mk_op(GE),$1),$3) ;}
| e T_OR e                                         { $$ = mk_app(mk_app(mk_op(OR),$1),$3) ;}
| e T_AND e                                        { $$ = mk_app(mk_app(mk_op(AND),$1),$3) ;}
| T_ID                                             { $$ = mk_id($1);}/*Reconnaissance d'identificateurs et de variables*/
| e T_EQ e                                         { $$ = mk_app(mk_app(mk_op(EQ),$1),$3) ;}
| T_NOT e[expr]                                    { $$ = mk_app(mk_op(NOT),$expr) ;}
| T_FUN T_ID[var] arg_list[expr]                   { $$ = mk_fun($var,$expr);env = push_rec_env($var,$$,env);} /*Définition de fonctions*/
| T_LET T_ID[x] T_EQUAL e[arg] T_IN e[exp]         { $$ = mk_app(mk_fun($x,$exp),$arg); env = push_rec_env($x,$$,env);}/*Fonction IN*/
| e[exp] T_WHERE T_ID[x] T_EQUAL e[arg]            { $$ = mk_app(mk_fun($x,$exp),$arg); env = push_rec_env($x,$$,env);}/*Fonction WHERE*/
| T_IF e[cond] T_THEN e[then_br] T_ELSE e[else_br] { $$ = mk_cond($cond, $then_br, $else_br) ;}
| '[' list[l] ']'                                  { $$ = $l;}/*OP sur Listes*/
| e[exp] T_PUSH e[l]                               { $$ = mk_app(mk_app(mk_op(PUSH),$exp),$l);} 
| '(' f_arg[fun] e[arg] ')'                        { $$ = mk_app($fun,$arg);}/*Exécution de fonctions à plusieurs variables*/
| '(' e ')'                                        { $$ = $2;}/*Ignorer les parentheses inutiles*/
;

我的问题是在'('f_arg [fun] e [arg]')'我想删除语法周围的'()',但这会产生大量冲突,所以如果有人可以帮我忙我如何才能更改此语法使其不带括号工作 有关信息,请参见f_arg:

my problem is that at '(' f_arg[fun] e[arg] ')' i would like to remove the '()' around the syntax but this generates tons of conflicts so if some one could give me a hand on how could i change this syntax to work without the parentheses for information the f_arg:

f_arg :e                                                            {$$ = $1;}
  |f_arg[fun] e[arg]                                            {$$ = mk_app($fun,$arg);}
  ;

感谢所有

完整代码可在

https://github.com/WillianPaiva/project_AS

对我的英语感到抱歉

推荐答案

问题是未修饰的函数应用程序对于您的所有二进制中缀和前缀省略符都含糊不清-如果将类似a b + c的输入解析为c1>或a (b + c)?由于函数应用程序中没有令牌,因此,没有其他帮助,常规的yacc/野牛优先级规则将无法正常工作.

The problem is that unadorned function application is ambiguous with respect to all of your binary infix and prefix oparators -- should an input like a b + c be parsed as (a b) + c or a (b + c)? Since there's no token involved in function application, the normal yacc/bison precedence rules won't work for it without extra help.

假设您要赋予函数应用程序最高的优先级(我认为是正常情况),那么您可以通过一些额外的工作来完成这项工作.

Assuming you want to give function application the highest precedence (the normal case, I beleive), you can make this work with some extra work.

将以下优先级规则添加到列表的末尾(最高优先级):

Add the following precedence rule to the end of your list (highest precedence):

%left FUNCTION_APPLICATION T_NUM T_ID '{' '('

制定您的功能规则:

| e[fun] e[arg] %prec FUNCTION_APPLICATION { $$ = mk_app($fun,$arg); }

根据语法中其他地方的内容,可能需要向优先级规则中添加更多标记和/或稍微重新排列.特别是,FIRST(e)中的每个令牌都需要一个优先级,并且如果它在函数应用程序中的优先级与在其他用途​​上的优先级不同,那么事情将不起作用(因为每个令牌只能有一个优先级).只要功能应用程序的优先级高于所有其他功能,事情就应该可以解决.

depending on what you have elsewhere in the grammar, you might need to add some more tokens to the precedence rules and/or rearrange things slightly. In particular, every token in FIRST(e) needs a precedence, and if its precedence for function application is different from its precedence for other uses, things won't work (as each token can only have one precedence). As long as function application is higher precedence than everything else, things should be resolvable.

这篇关于对没有终端,只有非终端的规则使用yacc优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆