Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Antlr syntax tree into useful objects
    text
    copied!<p>I'm currently pondering how best to take an AST generated using Antlr and convert it into useful objects which I can use in my program. </p> <p>The purpose of my grammar (apart from learning) is to create an executable (runtime interpretted) language. </p> <p>For example, how would I take an attribute sub-tree and have a specific Attribute class instanciated. E.g. </p> <p>The following code in my language:</p> <pre><code>Print(message:"Hello stackoverflow") </code></pre> <p>would product the following AST:</p> <p><a href="http://img36.imageshack.us/img36/1672/simpleast.png" rel="nofollow noreferrer">alt text http://img36.imageshack.us/img36/1672/simpleast.png</a></p> <p>My current line of thinking is that a factory class could read the tree, pull out the name (<code>message</code>), and type(<code>STRING</code>) value("<code>Hello stackoverflow</code>"). Now, knowing the type I could instanciate the correct class (e.g. A StringAttribute class) and pass in the required attribute data - the <code>name</code> and <code>value</code>.</p> <p>The same approach could be used for a definition factory, pulling out the definition name (<code>Print</code>), instanciating the Print class, and then passing in the attributes generated from the attribute factory.</p> <p>Things do get a bit more complicated with a more complicated program:</p> <pre><code>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") } } } </code></pre> <p><a href="http://img205.imageshack.us/img205/3452/complexast.png" rel="nofollow noreferrer">alt text http://img205.imageshack.us/img205/3452/complexast.png</a></p> <p>ANY/ALL help or thoughts are very welcome. Many thanks.</p> <p>Previous related questions by me (Could be useful):</p> <ol> <li><a href="https://stackoverflow.com/questions/2061166/how-do-i-make-a-treeparser-in-antrl3/2061277#2061277">How do I make a tree parser</a> </li> <li><a href="https://stackoverflow.com/questions/2113703/antlr-left-recursive-problem/2117334#2117334">Solving LL recursion problem</a></li> <li><a href="https://stackoverflow.com/questions/2129569/antrl3-conditional-tree-rewrites/2136770#2136770">Antrl3 conditional tree rewrites</a></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload