可视化 LALR 语法

发布于 2024-12-15 23:37:12 字数 666 浏览 0 评论 0原文

我想可视化一个语法文件(实际上是咖啡脚本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2024-12-22 23:37:12

以下是创建语法图的说明。

grammar.coffee 的内容是可执行代码,其中必须运行才能获取实际的 Jison 语法。在用 Javascript 警报替换 Jison 调用后,我使用 Try CoffeeScript 页面来编译它。然后运行生成的 Javascript 来获取语法,其中
看起来像这样:

{
  "tokens":" TERMINATOR TERMINATOR TERMINATOR STATEMENT INDENT OUTDENT INDENT OUTDENT IDENTIFIER NUMBER STRING JS REGEX BOOL = = INDENT OUTDENT : : INDENT OUTDENT RETURN RETURN HERECOMMENT PARAM_START PARAM_END -> =>  ,  , ... = ... . ?. :: :: INDEX_START INDEX_END INDEX_SOAK { }  , TERMINATOR INDENT OUTDENT CLASS CLASS CLASS EXTENDS CLASS EXTENDS CLASS CLASS CLASS EXTENDS CLASS EXTENDS SUPER SUPER  FUNC_EXIST CALL_START CALL_END CALL_START CALL_END THIS @ @ [ ] [ ] .. ... [ ] , TERMINATOR INDENT OUTDENT INDENT OUTDENT , TRY TRY TRY FINALLY TRY FINALLY CATCH THROW ( ) ( INDENT OUTDENT ) WHILE WHILE WHEN UNTIL UNTIL WHEN LOOP LOOP FOR FOR FOR OWN , FORIN FOROF FORIN WHEN FOROF WHEN FORIN BY FORIN WHEN BY FORIN BY WHEN SWITCH INDENT OUTDENT SWITCH INDENT ELSE OUTDENT SWITCH INDENT OUTDENT SWITCH INDENT ELSE OUTDENT LEADING_WHEN LEADING_WHEN TERMINATOR IF ELSE IF ELSE POST_IF POST_IF UNARY - + -- ++ -- ++ ? + - MATH SHIFT COMPARE LOGIC RELATION COMPOUND_ASSIGN COMPOUND_ASSIGN INDENT OUTDENT EXTENDS",
  "bnf":
  {
    "Root":
    [
      ["","return $ = new yy.Block;",null],
      ["Body","return $ = $1;",null],
      ["Block TERMINATOR","return $ = $1;",null]
    ],
    "Body":
    [
      ["Line","$ = yy.Block.wrap([$1]);",null],
      ["Body TERMINATOR Line","$ = $1.push($3);",null],
      ["Body TERMINATOR","$ = $1;",null]
    ],
    "Line":
    [
      ["Expression","$ = $1;",null],
      ["Statement","$ = $1;",null]
    ],
    ...

上面的内容可以输入到 Jison-to-W3C 语法转换器,结果
在这样的语法中:

Root     ::= ( Body | Block TERMINATOR )?
Body     ::= Line ( TERMINATOR Line | TERMINATOR )*
Line     ::= Expression
           | Statement
...

从这里我们可以让铁路图生成器创建一个语法图:

CoffeeScript 语法图

. 。 。

请注意,转换器仅评估语法的“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:

{
  "tokens":" TERMINATOR TERMINATOR TERMINATOR STATEMENT INDENT OUTDENT INDENT OUTDENT IDENTIFIER NUMBER STRING JS REGEX BOOL = = INDENT OUTDENT : : INDENT OUTDENT RETURN RETURN HERECOMMENT PARAM_START PARAM_END -> =>  ,  , ... = ... . ?. :: :: INDEX_START INDEX_END INDEX_SOAK { }  , TERMINATOR INDENT OUTDENT CLASS CLASS CLASS EXTENDS CLASS EXTENDS CLASS CLASS CLASS EXTENDS CLASS EXTENDS SUPER SUPER  FUNC_EXIST CALL_START CALL_END CALL_START CALL_END THIS @ @ [ ] [ ] .. ... [ ] , TERMINATOR INDENT OUTDENT INDENT OUTDENT , TRY TRY TRY FINALLY TRY FINALLY CATCH THROW ( ) ( INDENT OUTDENT ) WHILE WHILE WHEN UNTIL UNTIL WHEN LOOP LOOP FOR FOR FOR OWN , FORIN FOROF FORIN WHEN FOROF WHEN FORIN BY FORIN WHEN BY FORIN BY WHEN SWITCH INDENT OUTDENT SWITCH INDENT ELSE OUTDENT SWITCH INDENT OUTDENT SWITCH INDENT ELSE OUTDENT LEADING_WHEN LEADING_WHEN TERMINATOR IF ELSE IF ELSE POST_IF POST_IF UNARY - + -- ++ -- ++ ? + - MATH SHIFT COMPARE LOGIC RELATION COMPOUND_ASSIGN COMPOUND_ASSIGN INDENT OUTDENT EXTENDS",
  "bnf":
  {
    "Root":
    [
      ["","return $ = new yy.Block;",null],
      ["Body","return $ = $1;",null],
      ["Block TERMINATOR","return $ = $1;",null]
    ],
    "Body":
    [
      ["Line","$ = yy.Block.wrap([$1]);",null],
      ["Body TERMINATOR Line","$ = $1.push($3);",null],
      ["Body TERMINATOR","$ = $1;",null]
    ],
    "Line":
    [
      ["Expression","$ = $1;",null],
      ["Statement","$ = $1;",null]
    ],
    ...

The above can be fed to the Jison-to-W3C grammar converter, resulting
in a grammar like this:

Root     ::= ( Body | Block TERMINATOR )?
Body     ::= Line ( TERMINATOR Line | TERMINATOR )*
Line     ::= Expression
           | Statement
...

From here we can have the Railroad Diagram Generator create a syntax diagram:

CoffeeScript 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.

我不吻晚风 2024-12-22 23:37:12

所以我再次尝试,立即发现了我最明显的错误 - 我发布的 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

  • 去定位

    exports.parser = new Parser(
      令牌:tokens.join(" ")
      bnf:语法
      运算符:operators.reverse()
      开始符号:“根”
    )
    

    在代码中;将其替换为

    console.log JSON.stringify
      令牌:tokens.join“”
      bnf:语法
      运算符:operators.reverse()
      开始符号:“根”
    

    同时注意使用完全相同的缩进(第一行两个空格,其余四个空格)。

  • 从命令行运行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

    exports.parser = new Parser(
      tokens: tokens.join(" ")
      bnf: grammar
      operators: operators.reverse()
      startSymbol: "Root"
    )
    

    in the code; replace it with

    console.log JSON.stringify
      tokens: tokens.join " "
      bnf: grammar
      operators: operators.reverse()
      startSymbol: "Root"
    

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文