How do I work with the AST in Irony now?

后端 未结 2 861
[愿得一人]
[愿得一人] 2021-02-15 12:20

I have a grammar that works and parses in the Irony console just fine, but I don\'t get anything in the AST treeview. I was following along with the BASIC->Javascript article f

2条回答
  •  眼角桃花
    2021-02-15 13:00

    In Irony parsing is done in 2 phases. First it creates a parse tree and then it creates your AST tree.

    You are only seeing the first step. In order for Irony to create the AST you can:

    1. Tell it how to to map your NonTerminals to AST nodes:

      E.g. looking at the Irony sample grammer ExpressionEvaluatorGrammar we see:

      var BinExpr = new NonTerminal("BinExpr", typeof(BinaryOperationNode));`    
      

      Here the we are telling Irony to map the BinExpr NonTerminal to a BinaryOperationNode which is our AST node.

    2. Make it generate the AST when parsing:

      When you set this flag the AST tree will be generated when you parse.

      this.LanguageFlags = LanguageFlags.CreateAst;
      

    The root of your AST tree will then be:

    parseTree.Root.AstNode
    

    I found this source a great starting point.

提交回复
热议问题