Hi Clifford,
Yes I meant elements.map{|e| e.to_array}
Anyways, those issues are resolved now. UnaryOp was a set of terminals which
I had defines as "abc"/"cde"/"efg"...
The issue got resolved when I changed this to ("abc"/"cde"/"efg"...). Now I
am able to use UnaryOp.to_array. Not sure how this matters, since we are
defining the method inside {..}
Another issue I had was in accessing methods for optional non-terminals.
This failed regardless of if C was in the parsed text or not.
For eG;
Rule B
A C?
{
def to_array
return C.to_array
end
}
end
rule C
D "abd"
{
def to_array
return text_value
end
}
end
But works if I change this to
Rule A
B C
{
def to_array
return C.to_array
end
}
end
rule C
D? "abd"?
{
def to_array
return text_value
end
}
end
I am just noting it here, since it might help some one else who is new to
treetop.
On Mon, Oct 3, 2011 at 2:39 PM, Clifford Heath <clifford.he...@gmail.com>wrote:
> On 04/10/2011, at 6:29 AM, Swapna wrote:
> > I am having issues while writing methods for these rules. An example code
> is provided
> > below.
> > rule Expression
> > Instruction / comment
> > {
> This block only applies to comment, not to Instruction. Use parentheses
> around (Instruction/comment) to make it apply to both.
> > def to_array
> > return self.elements.to_array
> You don't need to say "self".
> "elements" is already an array. Did you mean "elements.map{|e|
> e.to_array}"?
> > end
> > }
> > end
> > rule Instruction
> > Unary / Tri / Binary
> Use parentheses.
> > {
> > def to_array
> > return self.elements.map {|e| e.to_array}
> > end
> > }
> > end
> > rule Unary
> > UnaryInst space?
> > def_to_array
> > self.elements[0].to_array # I get an error here
> saying
> > self.elements is nil. But my parser successfully parses the
> > instruction. I am unable to call the function beyond this point.
> I don't understand that. elements[1] might be nil, because it's optional,
> but not elements[0].
> Normally you should use the element names (or tags) to access the elements,
> so instead of saying "elements[0]" you can just say "UnaryInst".
> Try these things and report back.
> Clifford Heath.
> > end
> > end
> > rule UnaryInst
> > UnaryOp sat?
> > {
> > def to_array()
> > return 1
> > end
> > }
> > end
> > Looks like something basic. Thanks in advance for the help.
> > --
> > You received this message because you are subscribed to the Google Groups
> "Treetop Development" group.
> > To post to this group, send email to treetop-dev@googlegroups.com.
> > To unsubscribe from this group, send email to
> treetop-dev+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> http://groups.google.com/group/treetop-dev?hl=en.
> --
> You received this message because you are subscribed to the Google Groups
> "Treetop Development" group.
> To post to this group, send email to treetop-dev@googlegroups.com.
> To unsubscribe from this group, send email to
> treetop-dev+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/treetop-dev?hl=en.