How to get ANTLR for output hierarchical AST?

I have a Lua grammar , (minor changes to get it for output in C #, only namespace directives and a couple of parameter changes) and when I run it on some sample input, it returns me a tree with the root "nil" node and how child elements that look like a tokenized version of the input code. It seems that ANTLR tree-based grammars work on hierarchical trees, rather than “flat” ones, so I don't think I can use the output as it is.

Is there an easy fix for the grammar or do I need to rewrite it from scratch?

+3
source share
1 answer

Assuming your tree is just a 1-dimensional list of nodes, here you can create a hierarchy of parents / sisters:

In ANTLR, there are two operators for creating an AST:

!     excludes the node (token) from the (sub)tree;
^     makes a node the root of a (sub)tree.

If no operator is specified, nodes / tokens are added as children of the current root. This probably happened to you: everything you see is a one-dimensional list of nodes / tokens.

Example:

grammar Exp;

options {output=AST;}

// ... some rules ...

addition
  :  Integer '+'^ Integer ';'!
  ;

Integer
  :  '0'
  |  '1'..'9' '0'..'9'*
  ;

The rule additionwill create the following tree for the expression 6+9;:

   +
  / \
 /   \
6     9

As you can see: +- this is the root (after it it has ^), numbers are tokens (they do not have an operator), and half points are excluded (after it there is !).

. 7 " " ANTLR. .

, , . , , . : ANTLR.

!

+6

Source: https://habr.com/ru/post/1726424/


All Articles