1

I have a treetop grammar like below:

grammar Addme

  rule AddExpr
    Num '+' Num
  end

  rule Num
    [0-9]+ <ExprNumber>
  end

end

This is working when I parse the expression:

g = AddmeParser.new
t = g.parse("1234+56789")

. . . there is a syntax node that matches "1234" with type ExprNumber. But, if I add parentheses to the rule like this:

rule Num
    ([0-9]+) <ExprNumber>
end

It will not match the class ExprNumber. Why would this happen?

Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
suhao399
  • 628
  • 7
  • 11
  • The [documentation](http://treetop.rubyforge.org/semantic_interpretation.html) explains this a little bit. It may be because when it is in parentheses it is treating it like there should be a matching module instead of a class. – Josh Voigts Apr 27 '16 at 15:45
  • Thank you very much. I don't really get the idea of parentheses in this case? How it prevent type subclass – suhao399 Apr 27 '16 at 16:00
  • The node has already been created inside the parentheses. A module can be mixed in, but not a class. – cliffordheath Apr 27 '16 at 21:13
  • I check the treetop grammar, after parentheses the node-subclass definition is not allowed there so it does not generate the subclass. I am closing this now. – suhao399 Apr 29 '16 at 04:07
  • This is still listed as an open question. I copied my comment to the answer below. Please accept this so it stops appearing as unanswered. – cliffordheath Jan 29 '19 at 06:56

1 Answers1

0

The node has already been created inside the parentheses. A module can be mixed in, but not a class.

cliffordheath
  • 2,536
  • 15
  • 16