我怎样才能有一个“隐式乘法"?野牛统治? [英] How can I have an "implicit multiplication" rule with Bison?

查看:84
本文介绍了我怎样才能有一个“隐式乘法"?野牛统治?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数学表达式解析器开发Bison文件.到目前为止,基本上都还可以,但是我面临着隐式乘法的问题.

I'm working on a Bison file for a mathematical expression parser. Up to now it's mostly fine, but I'm facing a problem with implicit multiplications.

您知道,我想支持2x sin(4x) cos(4x)之类的表达式.它应该像2 * x * sin(4 * x) * cos(4 * x)一样进行解析.这里没什么不好的,但是请考虑以下规则:

You see, I'd like to support expressions like 2x sin(4x) cos(4x). It should parse like 2 * x * sin(4 * x) * cos(4 * x). Nothing too bad here, but consider the following set of rules:

expr
    : /* snip */
    | '-' expr      { /* negate expression */ }
    | expr '-' expr { /* subtract expressions */ }
    | expr expr     { /* multiply expressions */ }

具有该隐式乘法规则会与减法规则产生歧义:x - log(x)log(x)减去x还是x-log(x)?

Having that implicit multiplication rule creates an ambiguity with the subtraction rule: is x - log(x) the subtraction of log(x) to x or the multiplication of x by -log(x)?

我愿意准备一个简单的解决方案,例如除非减去,否则就是乘法",但我不知道该如何告诉野牛.

I'd be ready to settle for an easy solution, like "it's a multiplication unless it's subtracting", but I don't know how to tell that to Bison.

推荐答案

具有该隐式乘法规则会导致与减法规则产生歧义:x-log(x)是log(x)减去x还是x与-log(x)相乘?

Having that implicit multiplication rule creates an ambiguity with the subtraction rule: is x - log(x) the subtraction of log(x) to x or the multiplication of x by -log(x)?

甚至是x - l * o * g * x?或者只是x - log * x?

所以不是一个简单的问题.假设您仅通过查看log就能知道它是一个函数.然后,您可以在词法分析器中消除歧义,然后剩下如果有疑问,看起来像中缀运算符的运算符就是中缀运算符".这是一个快速的解决方案:

So not quite a simple problem. Suppose you can tell just by looking at log that it is a function. Then you can disambiguate in your lexer, and you're left with "in case of doubt, an operator that looks like an infix operator is an infix operator". Here's a quick solution:

term   : ID
       | NUMBER
       | '(' expr ')'      { $$ = $2; }
       | FUNC '(' expr ')' { $$ = new_expr($1, 'c', $3); }
       ;

factor : term
       | term factor       { $$ = new_expr($1, '*', $2); }
       ;

prefix : factor
       | '-' factor        { $$ = new_expr(0, '-', $2); }
       ;

muldiv : prefix
       | muldiv '/' prefix { $$ = new_expr($1, '/', $3); }
       | muldiv '*' prefix { $$ = new_expr($1, '*', $3); }
       ;

expr   : muldiv
       | expr '+' muldiv { $$ = new_expr($1, '+', $3); }
       | expr '-' muldiv { $$ = new_expr($1, '-', $3); }
       ;

这个特殊的语法不允许--x,尽管它对y--x非常满意,这意味着y-(-x).如果您要接受--x,则可以将第二个prefix生产更改为'-' prefix.

This particular grammar disallows --x, although it's perfectly happy with y--x, which means y-(-x). If you want to accept --x, you could change the second prefix production to '-' prefix.

就我个人而言,我更希望能够键入sin 2xlog 3n,但这开始变得有些棘手. sin 2x cos 2x是什么意思?大概是(sin(2*x))*(cos(2*x)).但是log nlog n并不意味着log(n*log(n))吗?这一切都可以实现;它只需要考虑所有可能性.

Personally, I'd prefer to be able to type sin 2x and log 3n but that starts to get a bit tricky. What does sin 2x cos 2x mean? Presumably, it means (sin(2*x))*(cos(2*x)). But does log nlog n not mean log(n*log(n)) ? This can all be achieved; it just requires thinking through all the possibilities.

这篇关于我怎样才能有一个“隐式乘法"?野牛统治?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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