This project has moved and is read-only. For the latest updates, please go here.

Place holder in expression

Apr 8, 2010 at 6:40 AM
Edited Apr 8, 2010 at 6:41 AM


I am new to Irony. But I have to give some quick result.

I have expression which can contaion:

1. Number: I am getting.

2. indetifier: I am getting.

3. Function: I am getting.

4. Place holder: If in expression X | Y | Z | A appear in any case. Then grammar should identify it as place holder not as identifier.



Ashish Kundan

Apr 8, 2010 at 5:38 PM


Don't quite understand what is place holder - just another terminal, some special thing?

You can declare it as non-terminal with a rule:



var placeHolder = new NonTerminal("PlaceHolder"); 

placeHolder.Rule = ToTerm("X") | "Y" | "Z" | "A";


Additionally you'll have to set priority of X,Y,Z,A to be higher than that of Identifier or function, like this:

ToTerm("X").Priority = 100;

ToTerm("Y").Priority = 100;

and so on...

Alternatively you can lower priority of Identifier and Function



Apr 9, 2010 at 4:42 AM


Thanks a lot for youe reply.

Your understanding is correct. Solution that you provided is working. I was missing priority in my code.

Placeholder is just like an indicator. X mean someting, Y means something and  so on.

I defined node for place holder like:

public class PlaceHolderNode : AstNode {
      public string Name;
      public AstNode Argument;
      public override void Init(Irony.Parsing.ParsingContext context, Irony.Parsing.ParseTreeNode treeNode)
          base.Init(context, treeNode);
          Name = treeNode.ChildNodes[0].FindTokenAndGetText();
          AsString = Name;

It is accepting all. But I want this node to consider only x,y,z,a in upper and lower case.

Is there any help document available for Irony apart from source code.


Ashish Kundan

Apr 9, 2010 at 7:16 PM

Just add lower-case strings to expression for placeHolder.Rule


Apr 10, 2010 at 3:44 AM

Thanks Roman.

I need to identify date in an expression. Can we use regular expression to define its rule?



Ashish kundan

Apr 11, 2010 at 4:18 PM

It depends on what format the date is - are there any delimiters? You can use QuotedValueLiteral for VB-like dates : #01/02/2010#. Or FixedLengthLiteral if there are no quoting characters. Other possibility is CustomTerminal - you have to provide matching method implementation. Finally there's RegExBasedLiteral where you specify reg expr

Apr 12, 2010 at 7:57 AM

I got it. Thanks a lot.

Apr 14, 2010 at 8:48 AM



I want to understand what are the important things that we need to take care while defining nodes. e.g I want to define a node for Nonterminal which contain only stock codes like IBM,MSFT etc. Those node I want to identify in expression as stock code node.


Ashish Kundan

Apr 14, 2010 at 2:44 PM

Well, the only important thing for defining an AST node class is to make it implement IAstNodeInit interface - that would allow nodes to be automatically created and initialized by parser. IBrowsableAstNode lets node be displayed in AST tree view in Grammar Explorer. That's it - the rest is defined by semantics of your language or DSL, whatever you need them to do.

Apr 15, 2010 at 4:47 AM

Thanks again for the response and making such a wonderfull tool.

Apr 16, 2010 at 6:41 AM



I wote parser to parse function call. It is like :

var number = TerminalFactory.CreateCSharpNumber("number");
var expression = new NonTerminal("expression");
var identifier = TerminalFactory.CreateCSharpIdentifier("identifier");
var fncall = new NonTerminal("fncall",typeof(FunctionCallNode));
var funArgument = new NonTerminal("funArgument");
var ArgList = new NonTerminal("ArgList", NodeType.GetNodeType("ExpressionListNode"));
fncall.Rule = functioName + OpenParen + ArgList + CloseParen;
ArgList.Rule = MakeStarRule(ArgList, comma, funArgument);
funArgument.Rule = expression| fncall | number;


expression id fun call or binary expression.

Above grammar parser function having argument expression, fncall and number in any order.

But I want only those funtion to be parsed whose first argumnet is expression or fncall and second argumnet is number.



Apr 16, 2010 at 7:37 PM
Edited Apr 17, 2010 at 5:05 PM


Grammar looks ok. What do you mean "only those functions to be parsed whose..." - you want to accept as valid only certain functions and reject others as errors?

If so, then I would recommend the following. Leave the grammar as is, and parser should accept all function calls. Note I mean Parser. The  in the after-parse validation you can check the function arguments and issue an error if it does not fit your restrictions. I think there's an event there which fires when parser creates AST node - you can handle this event and validate function args. Alternatively you can do it after parsing the file - by running thru the AST tree and validating all FunctionCallNodes. Finally, you can subclass FunctionCallNode and validate in Init method.


Apr 17, 2010 at 4:46 PM

Yes Roman, your understanding is correct. Thanks for your quick reply.