Convert Antlr Syntax Tree to Useful Objects

I'm currently thinking about how best to take the AST generated by Antlr and convert it to useful objects that I can use in my program.

The purpose of my grammar (besides learning) is to create an executable (interpreted at runtime) language.

For example, how can I take an attribute subtree and instantiate a specific attribute class. for instance

The following code is in my language:

Print(message:"Hello stackoverflow")

will produce the following AST:

alt text

Currently, I believe that a factory class can read a tree, retrieve the name ( message) and type ( STRING) (" Hello stackoverflow"). Now, knowing the type, I can create the correct class (for example, the StringAttribute class) and pass the required attribute data - nameand value.

, (Print), Print, , .

:

Program(args:[1,2,3,4,5])
{
    If(isTrue:IsInArray(array:{Program.args} value:5))
    {
        Then {
            Print(message:"5 is in the array")
        } Else {
            Print(message:"More complex " + "message")
        }
    }
}

alt text

/ . .

( ):

  1. LL
  2. Antrl3
+4
4

9 " " " " Terence Parr.

, , ( ):

  • ;
  • ( () );

, spring ( UML-):

  • class Interpreter
    • global: MemorySpace
    • : Stack <Function>
    • ...


  • class MemorySpace
    • vars: Map < String, Object >
    • ...


  • class Function
    • local: MemorySpace
    • execute(): void
    • ...
+4
+2

AST, , , , , .

0

This Tutorial is based on Flex and Bison, but at the end it details how it converts its AST to LLVM build code, which can be useful.

0
source

All Articles