How to get the child node's child ?

SyntaxEditor for WPF Forum

Posted 12 years ago by Peter Luo - AIMNEXT
Version: 12.1.0560


I have a question about the AST, for example I have the following grammar:

var @Identifier = new Terminal("Identifier");
// Some other Terminals

var typeSpec = new NonTerminal("TypeSpecifiers");
var funcDef = new NonTerminal("FunctionDefinition");
var declarator = new NonTerminal("Declarator");
var paraList = new NonTerminal("ParameterList");
var block = new NonTerminal("Block");
// Some other NonTerminals

funcDef.Production = typeSpec + declarator + block["funcBlock"] 
     > Ast<FunctionDefinition>()
        .SetProperty(x => x.Block, AstFrom("funcBlock"))
        .SetProperty(x => x.Name, ???)
        .SetProperty(x => x.ParameterList, ???);

declarator.Production = (XXXX | @Identifier) + (paraList | XXXXX) | (XXXX).....

 The  "FunctionDefinition" is a type-specific ast node type, and have some properties, I want to set the "@Identifier"(in the "declarator") to the "Name" property, and set the "paraList " to the "ParameterList" property...

I do not want to refactor the grammar.

How to do it ?

Thanks a lot!

Comments (3)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Peter,

Well in that case you need two results from declarator.  The easiest thing to do would be to move your declarator production directly to be inline within the funcDef production.  That way you could reference the results of the declarator production terms and put them right in the FunctionDeclaration you are building.  You could mark a comment where declarator was saying that you did so.

Otherwise you'll have to make declarator return some custom object that has both values in it.  Then you'd probably need to make some custom ITreeConstructionNode that knew how to return one value or the other from your custom object and use them in your funcDef tree construction.

Actipro Software Support

Posted 12 years ago by Peter Luo - AIMNEXT


I defined "Declarator" type-specific ast node, and two properties in it named "Name" and "ParamList", bu another question:

declarator.Production = (XXXX | @Identifier["declIdent"]) + (paraList["declParam"] | XXXXX) | (XXXX) 
    > Ast<Declarator>()
       .SetProperty(x => x.Name, ???)
       .SetProperty(x => x.ParamList, ???);

 How to set the "Name" and "ParamList" properties, I tried "AstFrom" and "AstConditionalFrom", but seems not work.

Thank you very much!

[Modified 12 years ago]

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Peter,

Any time you add in an alternation (x | y) you make a new scope.  So names you define there (like declIdent) won't be visible to the production's tree constructor.

You'd need to give your entire alternation a label... I believe something like this:

(XXXX > null | @Identifier["i"] > AstFrom("i")).SetLabel("declIdent")

Actipro Software Support

The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.