可视化 LALR 语法
我想可视化一个语法文件(实际上是咖啡脚本的 Jison 语法)。所以输入文件是Bison/Yacc风格的语法文件。预期的输出可能是 Graphviz 点文件或类似的文件。
我不一定要寻找完整的 IDE,例如 GOLD。但能够处理 LALR 输入非常重要,这就是为什么优秀的 ANLTRWorks 不这样做的原因t 考虑在内。
我还检查了 Wikipedia 上解析器的比较,但它仅包含 IDE 支持,但不是可视化。
这是我实际上想要可视化的 coffeescript 语法文件 。
I'd like to visualize a grammar file (actually the Jison grammar for coffee-script). So the input file is a grammar file of Bison/Yacc style. The expected output could be a Graphviz dot file or something similar.
I'm not necessarily looking for a complete IDE, like GOLD. But it's important to be able to handle a LALR input, that's why the excellent ANLTRWorks doesn't come into account.
I also checked a comparison of parsers on Wikipedia, but it includes only IDE support, but not visualization.
This is the coffeescript grammar file I actually want to visualize.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是创建语法图的说明。
grammar.coffee 的内容是可执行代码,其中必须运行才能获取实际的 Jison 语法。在用 Javascript 警报替换 Jison 调用后,我使用 Try CoffeeScript 页面来编译它。然后运行生成的 Javascript 来获取语法,其中
看起来像这样:
上面的内容可以输入到 Jison-to-W3C 语法转换器,结果
在这样的语法中:
从这里我们可以让铁路图生成器创建一个语法图:
. 。 。
请注意,转换器仅评估语法的“bnf”部分,因此它不会考虑标记定义。这可以通过对 W3C 风格语法进行一些手动后处理来改进。
Here are the instructions for creating a syntax diagram.
The content of grammar.coffee is executable code, which must be run for getting the actual Jison grammar. I used the Try CoffeeScript page to compile it, after having replaced the Jison call by a Javascript alert. Then ran the resulting Javascript to obtain the grammar, which
looks like this:
The above can be fed to the Jison-to-W3C grammar converter, resulting
in a grammar like this:
From here we can have the Railroad Diagram Generator create a syntax diagram:
. . .
Note that the converter only evaluates the "bnf" part of the grammar, so it does not take the token definitions into account. This could be improved by doing some manual postprocessing of the W3C-style grammar.
所以我再次尝试,立即发现了我最明显的错误 - 我发布的 json 错误地使用了单引号而不是双引号。让我详细介绍一下工作流程;这很简单,如果您已经在 NodeJS 上运行 CoffeeScript,那么您就可以开始了:
找到
node_modules/coffee-script/lib/coffee-script/grammar.js
模块您的文件系统;复制并复制将该文件的代码粘贴到 js2coffee 站点上 js->coffee 窗格的源窗格中(您可以跳过那个,但我发现编辑 CS 比摆弄 JS 更令人愉快)。
将翻译后的代码保存到
node_modules/coffee-script/lib/coffee-script/grammar.coffee
;去定位
在代码中;将其替换为
同时注意使用完全相同的缩进(第一行两个空格,其余四个空格)。
从命令行运行
coffee node_modules/coffee-script/lib/coffee-script/grammar.coffee > /tmp/coffee.grammar
;将生成的文件的代码复制并粘贴到语法转换器中;
将生成的 EBNF 语法从转换器复制并粘贴到 铁路图生成器 上的语法编辑器中;
转到“视图图”选项卡,然后 - 高兴!
做所有这些复制和模仿的事情有点苦差事,但对于任何一次性可视化来说肯定足够好了。我一直在网上搜索一个合理的 RR 图生成器,这个特定的生成器绝对是输出最漂亮的生成器之一。当您想到铁路图实际上是多么简单时,您会感到有点惊讶。
so i tried again and found my most blatant mistake right away—the json i had posted was incorrectly using single instead of double quotes. let me detail the workflow; it's simple enough, and if you're already running CoffeeScript on NodeJS you're ready to go:
locate the
node_modules/coffee-script/lib/coffee-script/grammar.js
module in your file system;copy & paste the code of that file into the source pane of the js->coffee pane on the js2coffee site (you could skip that, but i find it much more agreeable to edit CS than to fiddle with JS).
save the translated code to
node_modules/coffee-script/lib/coffee-script/grammar.coffee
;go and locate
in the code; replace it with
while taking care to use the exact same indentation (two space for the first line, four for the rest).
from the command line, run sth like
coffee node_modules/coffee-script/lib/coffee-script/grammar.coffee > /tmp/coffee.grammar
;copy and paste the code of the resulting file into the grammar converter;
copy and paste the resulting EBNF grammar from the converter into the grammar editor over at the railroad diagram generator;
go over to the View Diagram tab and — rejoice!
it's sort of a chore to do all of this copy'n'pastish stuff, but certainly good enough for any one-off visualization. i've been searching the web a lot for a reasonable RR diagram generator, and this particular one is definitely among the ones with the prettiest output. sort of surprising when you think of how simple railroad diagrams really are.