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

Operator Usage Question

Apr 8, 2009 at 5:40 PM
Hello everyone,
I'm trying to create a Lua parser and compiler just for fun.
A quick question I hope you have an answer to: if I were to model the following expression with Irony, how would I do?

chunk ::= {stat [';']} [laststat [';']]

I'm a bit confused about the usage of MakePlusRule, MakeStarRule, MakeTransientRule (especially about the latter, actually).

Thanks in advance for any insight, and thank you Roman for your work with Irony.
Apr 8, 2009 at 7:11 PM
Edited Apr 8, 2009 at 7:11 PM
Do you mean Grammar.MarkTransient() method? It sets the transient flag on non-terminal, so in parse tree construction it will be eliminated and all of its children will be popped up into it's owner child list. This can be used to eliminated intermidiate non-terminal nodes that serve only for expressing the grammar rules but serve no useful purpose in the tree. For example:

Expression.Rule = Term + bin_op + Term;
Term.Rule = identifier | number | parenth_expression.

So for expression 'x + 5' the parser would generate root expression node with 3 child nodes: Term node with single child x (identifier node), bin_op node with single child +, and Term with child '5'. If you add


.. then parser would eliminate Term nodes from result tree, and "x" and "5" nodes would be directly under the root expression node.

As for your expression, you have to provide some info about meaning of all these braces and brackets - I'm not familiar with this BNF syntax. Square brackets mean optional part I guess? then what is curly braces?
Apr 8, 2009 at 8:40 PM
Edited Apr 8, 2009 at 9:58 PM
Hello Roman, thanks for taking the time to answer!

You're right, I should have explained better; that expression is copied straight from the Lua website. I think

chunk ::= {stat [';']} [laststat [';']]

means that a chunk is composed by
1 to n stat elements
0 or 1 laststat elements,
both followed optionally by a semicolon symbol, if I'm not mistaken.

Thank you!

Apr 9, 2009 at 12:53 AM

You should do something like this:

  chunk.Rule = stat_list + semi_opt + last_stat_list_opt + semi_opt;
  semi_opt.Rule = Empty|";";
  stat_list.Rule = MakePlusRule(stat_list, stat);
  last_stat_list_opt.Rule = MakeStarRule(last_stat_list_opt, last_stat);


Apr 9, 2009 at 9:28 AM
Thanks again, Roman.

Apr 9, 2009 at 12:01 PM
Edited Apr 9, 2009 at 12:02 PM

Sorry to be a pain, but I came up with this expression and I was wondering if it's still correct.

chunk.Rule = MakeStarRule(stat + (colon | Empty)) + MakePlusRule(lastStat + (color | Empty)); 


Apr 9, 2009 at 5:39 PM
That wouldn't work simply because MakeStarRule and MakePlusRule functions require at least two parameters, the first of which is the list being setup. So you have to create list non-terminals explicitly and setup their rules with these methods.
Apr 9, 2009 at 8:45 PM
That makes perfect sense. I should stop posting when at work, I miss obvious things.
And thank you again!
Apr 10, 2009 at 5:50 AM
Check out if you're interested in seeing a Lua language service implemented in Irony.  I'm sure there are plenty of areas for improvement but myself and my coworkers have been using it for about a month and the syntax checking has been pretty accurate.
Apr 10, 2009 at 1:16 PM
Interesting, I'll be sure to check it out. Thanks!