Naming terminals

Oct 16, 2012 at 11:30 PM
Edited Oct 16, 2012 at 11:31 PM

Given the following very simple grammar

var expression = new NonTerminal("<expression>");
var nature = new NonTerminal("<nature>");
var cake = new NonTerminal("<cake>");
var drink = new NonTerminal("<drink>");

this.Root = expression;

expression.Rule = Symbol("I") + nature + cake + "and" + drink;
nature.Rule = Symbol("need") | "want";
cake.Rule = (Symbol("chocolate") | "white chocolate" | "wedding" | "fruit" | "carrot") + "cake";
drink.Rule = (Symbol("coke") | "pepsi" | "soda" | "water" | "beer") + Empty;

And parsing the following string "I want chocolate cake and coke"

I get the following child nodes in the AST

I [Symbol]
want [Symbol]
and [Symbol]
coke [Symbol]

It recognised the cake rule, however not anything else. I presume because cake is actually a NonTerminal unlike the simple drink rules.

How do I get the AST to reveal the name of the drinks, and nature rules?

Oct 16, 2012 at 11:50 PM

Have you defined Symbol somewhere?  It parses fine when I change Symbol to ToTerm(...).

Oct 17, 2012 at 1:20 AM
Edited Oct 17, 2012 at 1:23 AM

I was using Irony.Compiler from this CodeProject example.

var grammar = new MyGrammar();
var compiler = new LanguageCompiler(grammar);
var expression = compiler.Parse(source);

I see that to use ToTerm( ), I need to use Irony.Parsing instead.

var grammar = new MyGrammar();
var language = new LanguageData(grammar);
var parser = new Parser(language);
var parseTree = parser.Context.CurrentParseTree;

So that works just great, thank you for that @pgeerkins!

Is there a reason Compiler behaves differently to Parsing?

Oct 17, 2012 at 1:22 AM

Besides the fact that Compiler is 3 1/2 years old; probably not much.



Nov 21, 2012 at 11:17 AM
Edited Nov 21, 2012 at 1:19 PM

I've tried this grammar in "GrammarExplorer".  At the beginning it worked fine and the syntax highlighting worked. But as soon as i finish my sentence it throws an exception.

What is wrong with the grammar?


adding the following Line will fix the problem.

LanguageFlags = LanguageFlags.NewLineBeforeEOF;

Dec 30, 2012 at 4:36 AM

Then has nothing drastically changed in the code base during the Sept 30th 2012 release?


Did *exactly* the above steps, and it's failing on:


      //create the node
      //We know that either NodeCreator or DefaultNodeCreator is set; VerifyAstData create the DefaultNodeCreator
      var config = term.AstConfig;
      if (config.NodeCreator != null) {
        config.NodeCreator(Context, parseNode);
        // We assume that Node creator method creates node and initializes it, so parser does not need to call 
        // IAstNodeInit.Init() method on node object. But we do call AstNodeCreated custom event on term.
      } else {
        //Invoke the default creator compiled when we verified the data
        parseNode.AstNode = config.DefaultNodeCreator();
        //Initialize node
        var iInit = parseNode.AstNode as IAstNodeInit;
        if (iInit != null)
          iInit.Init(Context, parseNode);
      //Invoke the event on term


Thanks. Banging head on wall.