This project is read-only.

I don't think it can get much simpler than this. Just sub-class one of AstNode<TContxt> or ListAstNode<TContext> for your NonTeminals, compile and run.

#region License - Microsoft Public License - from PG Software Solutions Inc.
/***********************************************************************************
 * This software is copyright © 2012 by PG Software Solutions Inc. and licensed under
 * the Microsoft Public License.
 * 
 * Author:         Pieter Geerkens
 * Organization:   PG Software Solutions Inc.
 * *********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

using Irony.Parsing;
using PGSoftwareSolutionsInc.PGIrony;

   // best viewed with: Tab Size = 3; Indent Size = 3; Keep Tabs = On.
namespace PGSoftwareSolutionsInc.Util.PlayWaveLib {
   [Language("StatementList", "1.0", "Simple Statement-List grammar example")]
   public class AstGrammarTest : PGIrony.Grammar<MyAstContext> {
      public AstGrammarTest() : base(false,LanguageFlags.CreateAst) {
         GrammarComments = 
            @"Simple Statement-List grammar example, showing use of PGIrony library.";

         #region 1-Terminals - in PascalCase
            var Id           = new IdentifierTerminal("Identifier");
            var IntNum       = new NumberLiteral("",   NumberOptions.IntOnly);
            var RealNum      = new NumberLiteral("",   NumberOptions.None);

            var Assignment = ToTerm(":=");
            var Terminator = ToTerm(";");
            var OpenParen  = ToTerm("(");
            var CloseParen = ToTerm(")");
            var Comment    = new RegexBasedTerminal("Comment",      @"//.*\n");

            MarkPunctuation(Terminator, Assignment, OpenParen, CloseParen, Comment);
         #endregion

         #region 2-NonTerminals - in camelCase
            var binaryOp   = new PGIrony.TransientNonTerminal("BinOp");
            var unaryOp    = new PGIrony.TransientNonTerminal("UnaryOp");
            var numeric    = new PGIrony.TransientNonTerminal("Numeric");
            var expr       = new NonTerminal<ExpressionAstNode>();
            var stmt       = new NonTerminal<StatementAstNode>();
            var stmtList   = new NonTerminal<StatementListAstNode>();
         #endregion

         #region 3-Rules
            Root           = stmtList
                           ;
            stmtList.Rule  = MakePlusRule(stmtList, stmt)
                           ; 
            stmt.Rule      = Id + Assignment + expr + Terminator
                           | Comment
                           ;
            expr.Rule      = numeric
                           | Id
                           | OpenParen + expr + CloseParen
                           | expr + binaryOp + expr
                           | unaryOp + expr + ReduceHere()
                           ;
            unaryOp.Rule   = ToTerm("+") | "-" 
                           ;
            binaryOp.Rule   = ToTerm("*") | "/" | unaryOp
                           ;
            numeric.Rule   = IntNum
                           | RealNum
                           ;
            stmt.ErrorRule = SyntaxError + Terminator
                           ;

            RegisterBracePair("(", ")");

            // operator associativites in decreasing order of precedence
            RegisterOperators(20, Associativity.Left, "*", "/");
            RegisterOperators(10, Associativity.Left, "+", "-");
         #endregion
      }
      /// <summary>Provide MyGrammar with appropriate AstContext.</summary>
      protected override MyAstContext GetContext(LanguageData language) {
         return new MyAstContext(language);
      }
   }

   // NB: Node description in Grammar Explorer tree-view automatically 
   // generated by stripping "AstNode" from GetType().Name.
   public class ExpressionAstNode      : AstNode<MyAstContext> {}
   public class StatementAstNode       : AstNode<MyAstContext> {}
   public class StatementListAstNode   : ListNode<MyAstContext> {}
}

AstGrammarTest.dll in Irony.GrammarExplorer.

Last edited Nov 7, 2012 at 6:30 PM by pgeerkens, version 4

Comments

davidmichener Jul 10, 2013 at 11:14 PM 
I had the same compiler errors that DimitijG had on the following 3 lines:

var expr = new NonTerminal<ExpressionAstNode>();
var stmt = new NonTerminal<StatementAstNode>();
var stmtList = new NonTerminal<StatementListAstNode>();

Using Telerik JustDecompile against the sample DLL from the Downloads page, showed the following code which worked for me:
var expr = new NonTerminal("Expr", typeof(ExpressionAstNode));
var stmt = new NonTerminal("Stmt", typeof(StatementAstNode));
var stmtList = new NonTerminal("StmtList", typeof(StatementListAstNode));

DmitrijG Nov 12, 2012 at 7:03 PM 
I want simply to repeat the test. Such NonTerminal causes an error, because is not generic.

var expr = new NonTerminal<ExpressionAstNode>();

You say "... Just sub-class one of AstNode<TContxt> or ListAstNode<TContext> for your NonTeminals ....
But I want forst of all only to run your test not my own !
What must I change ?