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

Inline Kleene operators

Jan 27, 2013 at 6:05 AM

I am working on my first Irony project.  In the interests of making converting an existing BNF to Irony code easier I thought to create helper functions to handle Kleene star and plus operations as well as the optional clause.  As a result (from the Python definition), 

simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE


simple_stmt.Rule = small_stmt + Star(";" + small_stmt) + Optional(";") + Eos;

with, e.g.

BnfExpression Star(BnfExpression expr)
    NonTerminal result = new NonTerminal("<* " + expr.ToString() + ">", typeof(NotSupportedNode));
    result.Rule = MakeStarRule(result, expr);
    return result;

I added the NotSupportedNode reference in the constructor in an attempt to get rid of the problem with the resulting non-terminal having a null DefaultNodeCreator, but now the resulting "Unknown" NT resulting from the addition operation has the same issue.  I can't mark the intermediate NT as transient either.

Has anyone tried to do this?


Jan 27, 2013 at 6:51 AM
Edited Jan 27, 2013 at 6:52 AM

simple answer - don't do that. The simple_stmt.Rule definition you have is the style I try to discourage. Too many things are packed into one rule. It is ok for formal language definition, but not so good for parser, and especially for building interpreter. For interpreter, every NonTerminal must have AST node, and you can't assign AST node type to these 'auto' nonterminals in the middle of expression. And your troubles are just manifestation of this problem. So, no convenience methods to cut the corners, at least not this one. Unless you're building just parser, not interpreter. 

Question - are implementing Python with interpreter? - then you do need Ast node type or creator for each nonterminal

   - or you want to have just parser - in this case you don't need any AST nodes at all, but need to disable (skip) the flag CreateAst in LanguageFlags, so that Irony does not complain about missing AST node types

Jan 27, 2013 at 7:20 AM

I am intending to build an interpreter, but for now I am just trying to understand how Irony works.  Creating all of the intermediate nodes from the base grammar is an additional source of error so I was hoping to avoid that.  It also adds a lot of work when expanding the language.  Using a processor that consumes the raw BNF to generate the Irony code seems like a reasonable step now.  I have worked with more yacc/lex-style generators in the past so breaking down the source BNF into simple chunks is something I'd rather have the machine do.

Jan 27, 2013 at 7:27 AM

then remove LanguageFlags.CreateAst flag, and do whatever you want - Irony should be able more-less handle this. It is the interpreter/AST restriction that is the source of this recommendation

Jan 27, 2013 at 7:30 AM

Ok, thanks.  As I said, I am still learning how the product works.