Tree Constructors

SyntaxEditor for WPF Forum

Posted 13 years ago by Yury Asheshov
Version: 11.1.0545
For following EBNF:

box = (apple | orange)?

I generate three classes in Language Designer:

class Box : AstNodeBase
public IList<AbstractFBlock> Apples { ... }
public IList<AbstractFBlock> Oranges { ... }

class Apple : AstNodeBase

class Orange : AstNodeBase

How to define production for Box?

boxDefinition.Production =
(appleDefenition["apple"] > Ast<Apple>()).OnErrorContinue().SetLabel("apples")
|(orangeDefenition["orange"] > Ast<Orange>()).OnErrorContinue().SetLabel("oranges"))
> Ast<Box>()
.AddToCollectionProperty(s => s.Apples, AstChildrenFrom("apples"))
.AddToCollectionProperty(s => s.Oranges, AstChildrenFrom("apples"));
I'm not sure that is a correct way...

Comments (1)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Yury,

That scenario is tricky since as you parse nested productions (like the alternation options in your case), their results are combined into a single result collection. But then you are trying to separate them back out to put the various results in different properties.

We normally would recommend just putting them all in the same collection on Box since that is easier.

But if you need to separate them out, this is where you'd probably need to create a custom tree construction node that does the code for you. Basically you'd set a label for your alternation results like this:
boxDefinition.Production = (
    appleDefenition["apple"] > Ast<Apple>()
    | orangeDefenition["orange"] > Ast<Orange>()
> new MyTreeConstructionNode(AstChildrenFrom("fruit"));
Then create a MyTreeConstructionNode that implements TreeConstructionNodeBase. The CreateNode method is passed a match collection. You should create your Box class instance first. Then look for the "fruit" match in the matches collection and you can iterate over the items. Add each one as appropriate to your Box instance and return the Box AST node result.

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.