Defining a custom grammar to be used with Irony

Jun 13, 2013 at 7:26 AM
I want to have a grammar which would have following functionality.
1 - Loops , Conditional Statements
2 - Math Calculations
3 - Some extra methods to do Integration,Derivation , Double Derivation etc.

Should I develop a completely new grammar or just change the existing grammar like ExpressionEvaluatorGrammar ?

If I want to change ExpressionEvaluatorGrammar to support conditional and loop statements , can i just copy similar rules from C# grammar to ExpressionEvaluatorGrammar or do i need to do anything else ?
Jun 13, 2013 at 2:10 PM
Edited Jun 13, 2013 at 2:44 PM
Update : I have created a new Grammar as following .
public class MathEngineGrammar : InterpretedLanguageGrammar
        public MathEngineGrammar()
            : base(false)
            NumberLiteral Number = TerminalFactory.CreateCSharpNumber("Number");
            var statementList = new NonTerminal("statementList",typeof(StatementListNode));
            var statement = new NonTerminal("statement");
            var if_statement = new NonTerminal("if_statement");

            var expression = new NonTerminal("expression");
            var binexpr = new NonTerminal("binexpr", typeof(BinaryOperationNode));            
            var binop = new NonTerminal("binop", "operator");

            statementList.Rule = MakeStarRule(statementList, null, statement);
            statement.Rule = expression | if_statement;
            if_statement.Rule = ToTerm("if") + "(" + expression + ")" + "{"+expression+"}";

            expression.Rule = binexpr | Number;            
            binexpr.Rule = expression + binop + expression;
            binop.Rule = ToTerm("+") | "-" | "/" | "*";

            this.Root = statementList;

            MarkPunctuation("(", ")");

            RegisterOperators(1, "+", "-");
            RegisterOperators(2, "*", "/");

            this.MarkTransient(expression, binop, statement, if_statement);
            //LanguageFlags = LanguageFlags.CreateAst;
I am getting app.Evaluate(richTextBox1.Text) as null

On debugging i found LastScript.Root.AstNode for statement is coming null ( of course it should as I have not defined any node for it) in EvaluateParsedScript function.

Do I need to create a node for "statement" , "if_statement" ?
The c# grammar example also has not defined any node for statement or if_statement , then how is it working ?
Jun 13, 2013 at 4:45 PM
I suggest you start with miniPyhon grammar - it is an extension of expression evaluator; start adding missing grammar constructs and Ast nodes for them - yes, you need to define special Ast node class for any grammar construct.
Jun 13, 2013 at 6:26 PM
Edited Jun 13, 2013 at 6:26 PM
Thank You Roman , for taking time to reply despite your busy schedule.

I hope you can clear just one doubt of mine.

Where is the Ast Node defined for if_statement in c# grammar?
var if_statement = new NonTerminal("if_statement");
And also I don't find any conditional statement implementation in MiniPython grammar , that's what i need for my grammar and can't figure out how to do it.
Jun 13, 2013 at 6:48 PM
no, probably there isn't any (for If). There's 'IIF' implementation, but this is a special form, not AST node.
Look in interperter's AST folder, whatever nodes are defined there - that's all. Look at Refal sample as well, it does have a full implementation of AST node set for a language. For If statement, it shouldn't be that difficult, look at other nodes and do similar thing
Jun 13, 2013 at 7:48 PM
Edited Jun 13, 2013 at 7:51 PM
I also need to implement loops in addition to conditional statements.These are already implemented in c# grammar right ?

Sorry for repeating the question again but I still do not understand how the conditional statements and loops are implemented in the c# grammar ?

Following are some lines I am facing trouble to understand.

var if_statement = new NonTerminal("if_statement");
var statement_list = new NonTerminal("statement_list");
var statement = new NonTerminal("statement", "statement"); // I used this same statement but got Null reference exception

These statments don't have any AST Nodes . Then how are these working ?
Jun 13, 2013 at 8:14 PM
you should read some basics on parsing - use wikipedia, about abstract and concrete trees. There are 2 phases - parse (concrete) tree, just a tree structure of input, nothing else, no execution/interpretation/compiling. That's what c# grammar is set for, so no AST nodes there. If you want to execute (like expr evaluator), then you create AST tree from parse tree. Irony does this if you provide AST node types for grammar terms. Then you evaluate the AST tree. EAch AST node implements particular evaluation for a syntax construct - like binary expression or if-statement, etc