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

Newbie question...

May 21, 2009 at 1:03 PM
Edited May 21, 2009 at 3:21 PM


I've downloaded the latest version of Irony and I'm trying to get familiar with it. My purpose is to use this tool in order to parse and evaluate expressions written in a custom grammar, so I would like to use Irony without any UI just to parse & evaluate expressions. First of all I'm working on ExpressionEvaluatorGrammar sample provided with Irony and I parsed a simple expression using following lines of code:

string expr = @"a = 150 + 28 + 1*2";

ExpressionEvaluatorGrammar grammar = new ExpressionEvaluatorGrammar();
Compiler compiler = new Compiler(grammar);
ParseTree tree = compiler.Parse(expr);

I got AST tree in tree variable and displayed the tokens recursively using:

Action<ParseTreeNode> traverse = null;
traverse = (n) =>
  if (n.Token != null)
    Debug.Print (n.Token.Text);

Having all these: tree, tokens...etc, can you please guide me how should I evaluate the expression?

Thank you very much in advance!


May 21, 2009 at 4:23 PM


Unfortunately, evaluation does not work yet in latest versions of Irony (on Source page), it hadn't been brought back to life after big refactoring work that happened lately. You'll have to use the old version in Downloads page. You can run expression evaluator in Grammar Explorer, and then see in code on the form how it is done


May 26, 2009 at 3:38 PM

Hi Roman,

Thanks for your feedback. I will check the code on the form and let you know if something is unclear. I started to define my own grammar and now I'm stuck when i'm trying to define something similar with regular expression.


NonZeroNumber ::= One | Two | Three | Four | Five | Six | Seven | Eight | Nine

NonZeroNumber ::= One | Two | Three | Four | Five | Six | Seven | Eight | Nine

Integer ::= NonZeroNumber ( Number )*

Numeric ::= ( NegativeSign )? ( Number )+ ( Period ( Number )+ )?

first two rules were easy, but for the rest I need some advices from you.

... Of course to have a numeric I may find a workaround, but I have many rules that has the same pattern as described above and I need to be sure that are supported by Irony..

I look forward for your feedback. Many thanks in advance.



May 26, 2009 at 5:00 PM


I'm afraid you're on the wrong track. Things like numbers, string literals are treated in Irony as single entities - Terminals; in some language definitions numbers composition are detailed in BNF-like rules, the same way as syntax rules are defined. But this is done mainly for explanation purposes. In language implementation the Scanner/Tokenizer will recognize them as single entities. In Irony you should use NumberLiteral, StringLiteral classes to define these elements. Look at sample grammars.


May 27, 2009 at 8:46 AM


I saw the code and is clear for me how to define numbers as Terminals in Irony. The above rules are described in order to gave a concrete example easily to understand. For instance I have to implement the following rule:

RightOperandNumeric ::= Numeric | ( Segment SubscriptNotThis )* Set ( ( ( SubscriptNotThis )? ( ( (FieldGroup SubscriptNotThis )? Field ) | ( FieldGroup ( SubscriptThis | SubscriptEveryOrCorresponding ) ) ) ) | ( ( SubscriptThis | SubscriptEveryOrCorresponding )? ) ) | SegmentsWithOverlappingSubscripts

How shall I proceed in this case? (I don't want to provide the composing rules for of all elements: Segment SubscriptNotThis, SubscriptNotThis, FieldGroup, ... etc because is a quite complex grammar and I don't want to waste your time, but I'm interested only if this kind of rules are supported by Irony...)



May 27, 2009 at 4:43 PM

Well, it's better to define all elements, and then compose them in final expression. I recommend not to make individual rules too complex, with several levels of expression nesting; it should be "OR" ("|") of several "+" expressions of elements.


May 28, 2009 at 8:39 AM

I have a clear description of all the rules from the grammar and I do not want to modify it. I'm interested if Irony support defining a rule such:

A ::= B | (C*)D

How do you recommend to implement such rule?



May 28, 2009 at 9:08 AM

here it is, you have to define extra non-terminal CStar:

CStar.Rule = MakeStarRule(CStar, C);

A.Rule = B | "(" + CStar + ")" + D;


May 28, 2009 at 9:19 AM

ok; Thank you very much. This is what I'm looking for.