用于JavaScript / ECMAScript数组文字生产的LOOKAHEADs [英] LOOKAHEADs for the JavaScript/ECMAScript array literal production
问题描述
我目前正在实现JavaScript / ECMAScript 5.1 使用JavaCC解析器,并且 ArrayLiteral 生产。
I currently implementing a JavaScript/ECMAScript 5.1 parser with JavaCC and have problems with the ArrayLiteral production.
ArrayLiteral :
[ Elision_opt ]
[ ElementList ]
[ ElementList , Elision_opt ]
ElementList :
Elision_opt AssignmentExpression
ElementList , Elision_opt AssignmentExpression
Elision :
,
Elision ,
我有三个问题,我将一个接一个地问他们。
I have three questions, I'll ask them one by one.
这是第二个。
我已将此生产简化为以下形式:
I have simplified this production to the following form:
ArrayLiteral:
"[" ("," | AssignmentExpression ",") * AssignmentExpression ? "]"
请查看第一个问题是否正确:
Please see the first question on whether it is correct or not:
现在我已经尝试在JavaCC中实现它,如下所示:
Now I have tried to implement it in JavaCC as follows:
void ArrayLiteral() :
{
}
{
"["
(
","
| AssignmentExpression()
","
) *
(
AssignmentExpression()
) ?
"]"
}
JavaCC抱怨,
或 AssignmentExpression
(其内容)。显然,需要 LOOKAHEAD
规范。我花了很多时间试图弄清楚 LOOKAHEAD
的问题,尝试了类似的事情
JavaCC complains about ambiguous ,
or AssignmentExpression
(its contents). Obviously, a LOOKAHEAD
specification is required. I have spent a lot of time trying to figure the LOOKAHEAD
s out, tried different things like
-
LOOKAHEAD(AssignmentExpression(),)
在(...)*
中 -
LOOKAHEAD(AssignmentExpression()])
in(...)?
LOOKAHEAD (AssignmentExpression() ",")
in(...)*
LOOKAHEAD (AssignmentExpression() "]")
in(...)?
和其他一些变体,但我无法摆脱JavaCC警告。
and a few other variations, but I could not get rid of the JavaCC warning.
我不明白为什么它不起作用:
I fail to understand why this does not work:
void ArrayLiteral() :
{
}
{
"["
(
LOOKAHEAD ("," | AssignmentExpression() ",")
","
| AssignmentExpression()
","
) *
(
LOOKAHEAD (AssignmentExpression() "]")
AssignmentExpression()
) ?
"]"
}
好, AssignmentExpression ()
本身是模棱两可的,但结尾的, 或
]
在 LOOKAHEAD
中应该清楚应该选择哪个选项-还是我在这里弄错了?
Ok, AssignmentExpression()
per se is ambiguous, but the trailing ","
or "]"
in LOOKAHEAD
s should make it clear which of the choices should be taken - or am I mistaken here?
对于此产品,正确的 LOOKAHEAD
规范是什么样的?
What would a correct LOOKAHEAD
specification for this production look like?
更新
不幸的是,这不起作用:
This did not work, unfortunately:
void ArrayLiteral() :
{
}
{
"["
(
","
|
LOOKAHEAD (AssignmentExpression() ",")
AssignmentExpression()
","
) *
(
AssignmentExpression()
) ?
"]"
}
警告:
Warning: Choice conflict in (...)* construct at line 6, column 5.
Expansion nested within construct and expansion following construct
have common prefixes, one of which is: "function"
Consider using a lookahead of 2 or more for nested expansion.
第6行是(
code> LOOKAHEAD 。常见的前缀函数
只是 AssignmentExpression 的可能起始位置
。
Line 6 is (
before the first LOOKAHEAD
. The common prefix "function"
is simply one of the possible starts of AssignmentExpression
.
推荐答案
这是另一种方法,它的优点是可以识别哪些逗号表示未定义的元素而没有
Here is yet another approach. It has the advantage of identifying which commas indicate an undefined elements without using any semantic actions.
void ArrayLiteral() : {} { "[" MoreArrayLiteral() }
void MoreArrayLiteral() : {} {
"]"
| "," /* undefined item */ MoreArrayLiteral()
| AssignmentExpression() ( "]" | "," MoreArrayLiteral() )
}
这篇关于用于JavaScript / ECMAScript数组文字生产的LOOKAHEADs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!