Search code examples
c#ebnfirony

Translating EBNF into Irony


I am using Irony to create a parser for a scripting language, but I've come across a little problem: how do I translate an EBNF expression like this in Irony?

'(' [ Ident { ',' Ident } ] ')'

I already tried some tricks like

Chunk.Rule = (Ident | Ident + "," + Chunk);
CallArgs.Rule = '(' + Chunk + ')' | '(' + ')';

But it's ugly and I'm not even sure if that works the way it should (haven't tried it yet...). Has anyone any suggestions?

EDIT: I found out these helper methods (MakeStarList, MakePlusList) but couldn't find out how to use them, because of the complete lack of documentation of Irony... Has anyone any clue?


Solution

  • // Declare the non-terminals
    var Ident = new NonTerminal("Ident");
    var IdentList = new NonTerminal("Term");
    
    // Rules
    IdentList.Rule = ToTerm("(") + MakePlusRule(IdentList, ",", Ident) + ")";
    Ident.Rule = // specify whatever Ident is (I assume you mean an identifier of some kind).
    

    You can use the MakePlusRule helper method to define a one-or-many occurrence of some terminal. The MakePlusRule is basically just present your terminals as standard recursive list-idiom:

    Ident |  IdentList + "," + Ident
    

    It also marks the terminal as representing a list, which will tell the parser to unfold the list-tree as a convenient list of child nodes.