可视化LALR语法

Ada*_*deg 9 visualization lalr graphviz coffeescript

我想想象一个语法文件(实际上是咖啡脚本的Jison语法).因此输入文件是Bison/Yacc样式的语法文件.预期的输出可以是Graphviz点文件或类似的东西.

我不一定在寻找像GOLD这样的完整IDE .但是能够处理LALR输入很重要,这就是优秀的ANLTRWorks没有考虑到的原因.

我还检查了维基百科上解析器比较,但它只包括IDE支持,但不包括可视化.

这是我实际想要可视化的coffeescript语法文件.

Gun*_*her 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]
    ],
    ...
Run Code Online (Sandbox Code Playgroud)

以上可以输入到Jison-to-W3C语法转换器,产生如下语法:

Root     ::= ( Body | Block TERMINATOR )?
Body     ::= Line ( TERMINATOR Line | TERMINATOR )*
Line     ::= Expression
           | Statement
...
Run Code Online (Sandbox Code Playgroud)

从这里我们可以让Railroad Diagram Generator创建语法图:

CoffeeScript语法图

...

请注意,转换器仅评估语法的"bnf"部分,因此不考虑令牌定义.这可以通过对W3C样式语法进行一些手动后处理来改进.