Node to Token conversion

Aug 13, 2009 at 1:07 PM

I am a noobie to grammars/parser/lexers.etc.
I just converted and app I'm working on to the latest code drop from the last full release.  I had based my code on the examples that can be found on Code Project.  Here is my situation.

I have followed the design methodology of the JSBasic example and created nodes inheriting from AstNode and implementing to ToDOM function in my nodes, whose responsiblity is to convert the nodes from the root down to a class DOM.  At various points I am tyring to get the actual value for a node.  Here is what I was doing:

Token myToken = (Token)this.ChildNodes[0]; 
value = myToken.Text;

This no longer compiles as it complains about not being able to type case AstNode to Token.  I understand the value is in the ParseTreeNode.  How do I get back to a ParseTreeNode from the AstNode.  The AstNode may or may not have children.

Thanks.

Coordinator
Aug 13, 2009 at 4:24 PM

The code in this article no longer applies to latest sources. The AST part is not completed yet (default AST nodes), you have to build the nodes yourself. You are basically free to form your own node hierarchy.  

Yes, token is not longer AST node; try directly casting the child, but it depends on how you form the current node:

value = this.ChildNodes[0];

It works only if you have a "constant" as your first node, but might not work if first child is some expression or some non-terminal - it really depends on your grammar.