Java,C ++,C#等如何解决这种特殊的句法模糊性与<和>? [英] How do Java, C++, C#, etc. get around this particular syntactic ambiguity with < and >?
问题描述
我曾经认为C ++是一个奇怪的,与<
和>
的所有含糊不清,但在尝试实现解析器之后,我想我找到了一个打破 每个 语言的示例,该语言使用<
和>
用于泛型类型:
I used to think C++ was the "weird" one with all the ambiguities with <
and >
, but after trying to implement a parser I think I found an example which breaks just about every language that uses <
and >
for generic types:
f(g<h, i>(j));
这可能在语法上被解释为泛型方法调用( g
),或者可以解释为给出 f
两次比较的结果。
This could be syntactically either interpreted as a generic method call (g
), or it could be interpreted as giving f
the results of two comparisons.
如何做这些语言(特别是Java,我认为应该是LALR(1) -parsable?)解决这种语法歧义?
How do such languages (especially Java, which I thought was supposed to be LALR(1)-parsable?) get around this syntactic ambiguity?
我无法想象任何非hacky / context-free方式来解决这个问题,而且我对这种语言如何无上下文感到困惑,更不用说LALR(1)-parsable ...
I just can't imagine any non-hacky/context-free way of dealing with this, and I'm baffled at how any such language can be context-free, let alone LALR(1)-parsable...
(值得注意的是,即使是GLR解析器也无法为此语句返回单个解析而没有上下文! !)
(It's worth noting that even a GLR parser can't return a single parse for this statement with no context!!)
推荐答案
java中的泛型方法调用将是< h,i> g(j)所以没有歧义:)
a generic method call in java would be <h,i>g(j) so there is no ambiguity :)
这篇关于Java,C ++,C#等如何解决这种特殊的句法模糊性与<和>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!