简体   繁体   English

如何简化JavaScript / ECMAScript数组文字的生成?

[英]How to simplify JavaScript/ECMAScript array literal production?

I currently implementing a JavaScript/ ECMAScript 5.1 parser with JavaCC and have problems with the ArrayLiteral production. 我目前使用JavaCC实现JavaScript / ECMAScript 5.1解析器,并且ArrayLiteral生产遇到问题。

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生产,并最终到达以下生产(伪语法):

ArrayLiteral:
    "[" ("," | AssignmentExpression ",") * AssignmentExpression ? "]"

My first question: is this rewriting correct? 我的第一个问题:这次重写正确吗?

Two other quetsions: 其他两个问题:

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. 因为在OP中的重写不是LL(1)-您无法在不阅读整个AssignmentExpression情况下区分各种可能性-而通过此重写,您可以通过查看第一个令牌来找出使用哪种替代方案。

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

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