jison、nearley 和 pegjs 都是用于在 JavaScript 环境中构建自定义语言解析器的工具。它们允许开发者通过定义语法规则,自动生成能够处理特定文本格式(如 DSL、配置文件或查询语言)的代码。jison 采用传统的 Yacc/Bison 风格,适合熟悉编译原理的开发者;nearley 基于无限查找算法,擅长处理歧义文法;pegjs 则使用解析表达式文法(PEG),语法简洁直观,但需注意其维护状态。
在前端架构中,处理自定义配置文件、查询语言或领域特定语言(DSL)时,手写解析器往往容易出错且难以维护。jison、nearley 和 pegjs 提供了三种不同的自动化解决方案。虽然它们的目标一致,但底层算法、文法定义方式和维护状态截然不同。本文将从工程实践角度深度对比这三者。
文法定义的可读性直接影响维护成本。不同的工具采用了不同的形式化语言描述方式。
jison 沿用经典的 Yacc/Bison 风格,使用 %% 分隔定义区和代码区。
/* jison 示例 */
%{
/* 头部代码 */
%}
%%
expression
: expression '+' term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term
: NUMBER { $$ = Number($1); }
;
nearley 使用接近 EBNF 的简洁语法,通过 .ne 文件定义。
-> 符号。/* nearley 示例 */
expression -> expression "+" term {% (d) => d[0] + d[2] %}
| term
term -> NUMBER {% (d) => Number(d[0]) %}
pegjs 采用解析表达式文法(PEG),语法最为直观。
= 定义。{}。/* pegjs 示例 */
expression
= head:term "+" tail:term { return head + tail; }
/ term
term
= number:NUMBER { return Number(number); }
解析器的核心算法决定了它能处理多复杂的语言结构。
jison 基于 LALR(1) 算法。
/* jison 处理左递归需要小心,否则可能报错 */
list
: list ',' item { $$ = $1.concat($3); }
| item { $$ = [$1]; }
;
nearley 使用 Earley 算法变体。
/* nearley 轻松处理左递归 */
list -> list "," item
| item
pegjs 基于 PEG 算法。
/* pegjs 需要避免直接左递归,通常改写为右递归或使用迭代 */
list
= head:item ("," tail:item)* { return [head].concat(tail); }
解析器报错信息的质量直接影响开发效率。
jison 错误信息较为底层。
/* jison 错误恢复较复杂 */
error
: error ';' { yyerror('syntax error'); }
;
nearley 提供较好的错误提示。
nearley-railroad 可可视化文法。/* nearley 错误捕获 */
main -> sentence+ {% (d) => d %}
| _ {% (d) => throw new Error('Unexpected input') %}
pegjs 错误信息相对友好。
/* pegjs 自动抛出详细错误 */
start = expression ^EOF
/* 如果匹配失败,error.message 包含期望内容 */
选择库时必须考虑长期维护风险。
jison 已长期停滞。
nearley 仍在维护中。
pegjs 官方已停止维护。
peggy。peggy 修复了更多 bug。# 建议替代方案
npm install peggy
# 而非
npm install pegjs
| 特性 | jison | nearley | pegjs |
|---|---|---|---|
| 算法 | LALR(1) | Earley (无限查找) | PEG |
| 文法风格 | Yacc/Bison | EBNF 变体 | PEG 表达式 |
| 左递归 | 支持但需小心 | 原生支持 | 不支持 (需改写) |
| 歧义处理 | 不支持 | 支持 | 不支持 (有序选择) |
| 维护状态 | ❌ 停滞 | ✅ 活跃 | ⚠️ 停止 (推荐 peggy) |
| 适用场景 | 传统编译器 | 复杂/自然语言 | 简单 DSL |
jison 属于上一代工具 🕰️。除非你在维护十年前的老代码,否则不应在新架构中引入。它的冲突解决机制在现代开发中显得过于笨重。
nearley 是处理复杂文法的利器 🧩。如果你需要解析用户输入的宽松格式,或者文法存在天然歧义,它是唯一可靠的选择。配合可视化工具,调试体验较好。
pegjs 适合快速原型 🚀。对于简单的配置文件或查询语言,它的学习成本最低。但请务必考虑使用 peggy 替代,以获得持续的安全更新和 Bug 修复。
最终建议:在新项目中,优先评估 peggy(pegjs 的现代继承者)。若遇到 peggy 无法处理的歧义文法,再转向 nearley。避免使用 jison。
选择 nearley 如果你的文法存在歧义(ambiguous grammar)或者需要无限 lookahead 能力。它适合处理类似自然语言的结构或复杂的嵌套规则,且社区仍有活跃维护,适合需要高灵活性解析场景的项目。
选择 pegjs 如果你需要快速构建简单的 DSL 解析器且偏好 PEG 风格。但请注意,pegjs 官方已停止维护,建议评估其活跃分支 peggy。若必须使用原版,仅限于轻量级、无长期维护压力的内部工具。
选择 jison 仅当你需要维护基于 Yacc/Bison 的旧项目,或者团队对传统编译原理工具链非常熟悉。由于该库已长期未更新,新项目不建议使用,除非你有特定的兼容性需求且愿意承担维护风险。
nearley is a simple, fast and powerful parsing toolkit. It consists of:
nearley is a streaming parser with support for catching errors gracefully and providing all parsings for ambiguous grammars. It is compatible with a variety of lexers (we recommend moo). It comes with tools for creating tests, railroad diagrams and fuzzers from your grammars, and has support for a variety of editors and platforms. It works in both node and the browser.
Unlike most other parser generators, nearley can handle any grammar you can define in BNF (and more!). In particular, while most existing JS parsers such as PEGjs and Jison choke on certain grammars (e.g. left recursive ones), nearley handles them easily and efficiently by using the Earley parsing algorithm.
nearley is used by a wide variety of projects:
nearley is an npm staff pick.
Please visit our website https://nearley.js.org to get started! You will find a tutorial, detailed reference documents, and links to several real-world examples to get inspired.
Please read this document before working on nearley. If you are interested in contributing but unsure where to start, take a look at the issues labeled "up for grabs" on the issue tracker, or message a maintainer (@kach or @tjvr on Github).
nearley is MIT licensed.
A big thanks to Nathan Dinsmore for teaching me how to Earley, Aria Stewart for helping structure nearley into a mature module, and Robin Windels for bootstrapping the grammar. Additionally, Jacob Edelman wrote an experimental JavaScript parser with nearley and contributed ideas for EBNF support. Joshua T. Corbin refactored the compiler to be much, much prettier. Bojidar Marinov implemented postprocessors-in-other-languages. Shachar Itzhaky fixed a subtle bug with nullables.
If you are citing nearley in academic work, please use the following BibTeX entry.
@misc{nearley,
author = "Kartik Chandra and Tim Radvan",
title = "{nearley}: a parsing toolkit for {JavaScript}",
year = {2014},
doi = {10.5281/zenodo.3897993},
url = {https://github.com/kach/nearley}
}