简体   繁体   English

在设计数字编程语言时,并置乘法的陷阱有哪些?

[英]What are the pitfalls of multiplication by juxtaposition when designing a numeric programming language?

In lieu of a standard definition (happy to replace if exists), I'll define syntax for multiplication by juxtaposition using a space between symbols or expressions so that: 代替标准定义(很高兴替换,如果存在的话),我将使用符号或表达式之间的空格来定义通过并置进行乘法的语法,以便:

c = a b  --> c = a * b

(Note: I'm specifically not allowing c=ab --> c = a * b as ab would be the 2-character name of a variable ab ). (注意:我特别不允许c=ab --> c = a * b因为ab是变量ab的2个字符的名称)。

This ought to work nicely with parenthesis and order of operations so that 这应该很好地配合括号和运算顺序,以便

c = a (b + q)       --> c = a * (b + q)
c = (a + p) (b + q) --> c = (a + p) * (b + q)
c = a/p b           --> c = a/p*b

It seems that very few languages (eg, Wolfram Alpha), allow such implied multiplication. 似乎很少有语言 (例如Wolfram Alpha)允许这种隐式乘法。 I'm wondering why ? 我想知道为什么吗?

Is this just a pain to parse? 这只是一种痛苦的解析吗?

Are there ambiguities? 是否有歧义?

A keyword-search skim of "A practical approach to type-sensitive parsing" [Sailor & McCorsky 1994] seems to imply that this is possible. 关键词搜索“一种实用的类型敏感分析方法” [Sailor&McCorsky 1994]似乎暗示这是可能的。

Regarding vote to close due to "primarily opinion-based" : I am not looking for opinions. 关于由于“主要基于意见”而关闭的投票 :我不是在寻求意见。 I'm looking for theoretical reasons why parsing this would be more complex or impossible due to ambiguities. 我正在寻找理论上的原因,为什么由于模棱两可而使解析这件事变得更加复杂或不可能。

It's not impossible to parse but there are a number of issues which need to be resolved. 解析不是不可能的,但是有许多问题需要解决。

First, x(a+b) could be a product or a function call, depending on whether x is a scalar or a function. 首先,取决于x是标量还是函数, x(a+b)可以是乘积或函数调用。 To resolve the ambiguity, you need to know what x is, which pretty well requires mandatory declare before use . 要解决歧义,您需要知道x是什么,这很好地要求在使用前强制声明 Even if your language is strongly typed and you don't mind the requirement to declare first, you still ned a somewhat clunky back-channel from the parser to the lexical scanner, or some other parsing hack. 即使您的语言是强类型的,并且您不介意先声明的要求,您仍然需要从解析器到词法扫描器的笨拙反向通道,或其他一些解析技巧。 (For example, Awk -- which uses juxtaposition for string concatenation -- treats it as a function call if there is no space between the function name and the ( .). You can defer the parse but you might find that you need to reparse the entire expression in order to get precedences right. (例如,Awk-使用并置进行字符串连接-如果函数名称和( 。)之间没有空格,则将其视为函数调用。您可以推迟解析,但可能会发现需要重新解析整个表达式,以便获得正确的优先级。

A similar ambiguity with unary operators. 一元运算符也有类似的歧义。 It's easy enough to insist that a -b is a subtraction, so that the product would be written with parentheses -- a(-b) -- but it complicates the grammar. 坚持认为a -b是减法很容易,因此乘积将用括号( a(-b)书写,但是会使语法复杂化。

Precedence can be confusing for people reading the code, too. 优先顺序对于阅读代码的人也可能造成混淆。 Consider n!/k!(nk)! 考虑n!/k!(nk)! ; ; if that is the correct way to write the expression, juxtaposition must have a higher precedence than division, but there are those who think that juxtaposition and product should have the same precedence. 如果那是编写表达式的正确方式,则并列的优先级必须高于除法,但是有些人认为并列和乘积的优先级应相同。 (This group and includes some of the authors of the SI measurements standard, which recommends not using juxtaposition to write measurements where the expression type has a multiplied denominator.) (该组并包括SI度量标准的一些作者,该建议不建议使用并置来编写表达类型具有乘母数的度量。)

In short, a syntax supposedly designed for improved readability can produce confusion and even subtle bugs. 简而言之,一种为提高可读性而设计的语法可能会引起混乱,甚至会产生一些细微的错误。 That, plus the increase in parsing complexity, raises the question of how much ch real benefit there is in leaving out a few * s. 加上解析复杂性的增加,提出了一个问题,即省略几* s有多少实际好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM