如何简化JavaScript / ECMAScript数组文字的生成? [英] How to simplify JavaScript/ECMAScript array literal production?
问题描述
我目前正在实现JavaScript / ECMAScript 5.1 使用JavaCC解析器,并且 ArrayLiteral 生产。
ArrayLiteral:
[Elision_opt]
[ElementList]
[ElementList, Elision_opt]
ElementList:
Elision_opt AssignmentExpression
ElementList,Elision_opt AssignmentExpression
Elision:
,
Elision,
我有三个问题,我将一个接一个地问他们。
我试图简化/重写上面描述的 ArrayLiteral
生产,最后到达以下生产(伪语法):
ArrayLiteral:
[(, | AssignmentExpression,)* Assi gnmentExpression? ]
我的第一个问题:这个重写正确吗?
另外两个折弯:
是的,可以正确捕获所呈现的语法。
但是,更好的重写方法是:
[ AssignmentExpression? (, AssignmentExpression?)*]
因为在OP中的重写不是LL( 1)-您必须先阅读完整的 AssignmentExpression
才能区分各种可能性,而通过阅读第一个令牌,您就可以找出使用哪种选择。 / p>
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 tried to simplify/to rewrite the ArrayLiteral
production depicted above and finally arrived to the following production (pseudo-grammar):
ArrayLiteral:
"[" ("," | AssignmentExpression ",") * AssignmentExpression ? "]"
My first question: is this rewriting correct?
Two other quetsions:
- LOOKAHEADs for the JavaScript/ECMAScript array literal production
- How to implement a negative LOOKAHEAD check for a token in JavaCC?
Yes, that correctly captures the grammar presented.
However, a better rewrite would be:
"[" AssignmentExpression ? ( "," AssignmentExpression ? ) * "]"
because the rewriting in the OP is not LL(1) -- you cannot distinguish the possibilities without reading the entire AssignmentExpression
-- whereas with this one you can figure out which alternative to use simply by looking at the first token.
这篇关于如何简化JavaScript / ECMAScript数组文字的生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!