How to nest the Syntax Tree with type-specific AST nodes

SyntaxEditor for WPF Forum

Posted 12 years ago by SvenG - VIPA
Version: 11.2
Avatar

Hi ActiproTeam,

I have an assembler like codesyntax consisting of multiple instructions, that in turn consist of an Operator and an optional Operand. Here's an example:

AND VARIABLE1
 AND(
  OR VARIABLE3
  OR(
  ...
  )
 )

 

I have defined the following Grammar (simplified):

operator.Production = @operatorTypeOne > Ast<AstNodeOperand>()
					|@operatorTypeTwo > Ast<AstNodeOperand>()
					|@operatorTypeThree > Ast<AstNodeOperand>()
					
operand.Production = @operandTypeOne > Ast<AstNodeOperand>()
					|@operandTypeTwo > Ast<AstNodeOperand>()
					|@operandTypeThree > Ast<AstNodeOperand>()

instruction.Production = operator.SetLabel("AstOperator") + operand.SetLabel("AstOperand")
                  > Ast<AstNodeInstruction>().SetProperty(x => x.Operand, AstFrom("AstOperand")).SetProperty(x => x.Operator, AstFrom("AstOperator"));

						 

 this results in an Ast consisting of a linear list of Instructions

+- AstInstruction (AstOperator - "AND"; AstOperand - VARIABLE1)
+- AstInstruction (AstOperator - "AND("; AstOperand - null)
+- AstInstruction (AstOperator - "OR"; AstOperand - VARIABLE3)
...

 

my issue is that I want to nest the instructions based on the content of the operator. E.g. if the Operator contains a left parenthesis, i.e. "AND(", "OR(" I want to create a new level in my Ast. And if the Operator contains a right Parenthesis I want to to go up to the parent level again.
The nested Ast then should look something like that:

+- AstInstruction (AstOperator - "AND";	AstOperand - VARIABLE1)
+- AstInstruction (AstOperator - "AND(";	AstOperand - null)
|	|
|	+- AstInstruction (AstOperator - "OR";  AstOperand - VARIABLE3)
|	|	
|	+- AstInstruction (AstOperator - "OR(";  AstOperand - null)				 
|		|
|		+- AstInstruction (AstOperator - "OR(";  AstOperand - null)				 
|		...
|
+- AstInstruction (AstOperator - ")";	AstOperand - null)

 

I have tried to create a Custom Tree Node by subclassing ParentTreeConstructionNodeBase class and overriding the "CreateNode" method, but havent found a way to navigate in the structure (ie to implement "create a new hierarchy if the instruction's operator contains a left parenthesis). Unfortunately the sample in the help file is very trivial and didn't really help me.

How can I achieve to nest the Ast? Could you please point me in the right direction?

Many thanks in advance

Sven 

Comments (1)

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

Hi Sven,

It looks like your grammar needs to be changed a bit so that after your operator, your operand can either be a variable name like you have it now or another instruction.  You'd go the instruction route if there was a parenthesis.  Your operand class should probably be a base for a variable reference and for instruction since both could work there.


Actipro Software Support

The latest build of this product (v24.1.1) 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.