我为什么不能创建支持中缀、后缀和前缀函数等的语言? [英] Any reason I couldn't create a language supporting infix, postfix, and prefix functions, and more?

查看:49
本文介绍了我为什么不能创建支持中缀、后缀和前缀函数等的语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑创建一种非常适合创建 DSL 的语言,允许定义中缀、后缀、前缀甚至由多个单词组成的函数.例如,您可以如下定义中缀乘法运算符(其中已经定义了 multiply(X,Y)):

I've been mulling over creating a language that would be extremely well suited to creation of DSLs, by allowing definitions of functions that are infix, postfix, prefix, or even consist of multiple words. For example, you could define an infix multiplication operator as follows (where multiply(X,Y) is already defined):

a * b => multiply(a,b)

或后缀平方"运算符:

a squared => a * a

或者一个 C 或者 Java 风格的三元运算符,它涉及两个穿插变量的关键字:

Or a C or Java-style ternary operator, which involves two keywords interspersed with variables:

a ? b : c => if a==true then b else c

很明显,这种语言有很多歧义,但如果它是静态类型的(使用类型推断),那么大多数歧义都可以消除,剩下的歧义可以被认为是语法错误(由在适当的地方添加括号).

Clearly there is plenty of scope for ambiguities in such a language, but if it is statically typed (with type inference), then most ambiguities could be eliminated, and those that remain could be considered a syntax error (to be corrected by adding brackets where appropriate).

是否有一些我没有看到的原因会使这变得极其困难、不可能,或者只是一个简单的坏主意?

Is there some reason I'm not seeing that would make this extremely difficult, impossible, or just a plain bad idea?

许多人向我指出了可能会执行此操作或类似操作的语言,但我实际上对如何为其实现自己的解析器的指针感兴趣,或者如果这样做,我可能会遇到的问题.

A number of people have pointed me to languages that may do this or something like this, but I'm actually interested in pointers to how I could implement my own parser for it, or problems I might encounter if doing so.

推荐答案

这并不难做到.您需要为每个运算符分配一个fixity(中缀、前缀或后缀)和一个优先级.将优先级设为实数;你以后会感谢我的.优先级较高的运算符比优先级较低的运算符绑定得更紧密;在同等优先级下,您可以要求使用括号消除歧义,但您可能更愿意允许某些运算符关联,以便您可以编写

This is not too hard to do. You'll want to assign each operator a fixity (infix, prefix, or postfix) and a precedence. Make the precedence a real number; you'll thank me later. Operators of higher precedence bind more tightly than operators of lower precedence; at equal levels of precedence, you can require disambiguation with parentheses, but you'll probably prefer to permit some operators to be associative so you can write

x + y + z

没有括号.一旦确定了每个运算符的固定性、优先级和关联性,您就需要编写一个运算符优先级解析器.这种解析器写起来相当简单;它从左到右扫描令牌并使用一个辅助堆栈.龙书里有解释,但我一直没觉得很清楚,部分是因为龙书描述了一个非常普遍的运算符优先级解析案例.但我不认为你会觉得很难.

without parentheses. Once you have a fixity, a precedence, and an associativity for each operator, you'll want to write an operator-precedence parser. This kind of parser is fairly simply to write; it scans tokens from left to right and uses one auxiliary stack. There is an explanation in the dragon book but I have never found it very clear, in part because the dragon book describes a very general case of operator-precedence parsing. But I don't think you'll find it difficult.

另一种你要小心的情况是当你有

Another case you'll want to be careful of is when you have

prefix (e) postfix

其中 prefixpostfix 具有相同的优先级.这种情况还需要括号来消除歧义.

where prefix and postfix have the same precedence. This case also requires parentheses for disambiguation.

我的论文使用前缀和后缀运算符解析表达式后面有一个示例解析器,你可以下载代码,但它是用 ML 编写的,所以它的工作原理对于业余爱好者来说可能并不明显.但是对固定性等整个业务进行了非常详细的解释.

My paper Unparsing Expressions with Prefix and Postfix Operators has an example parser in the back, and you can download the code, but it's written in ML, so its workings may not be obvious to the amateur. But the whole business of fixity and so on is explained in great detail.

这篇关于我为什么不能创建支持中缀、后缀和前缀函数等的语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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