old-idea query: data sharing between parser and nodes

41 views
Skip to first unread message

Paul Madden

unread,
May 21, 2013, 7:17:22 PM5/21/13
to treet...@googlegroups.com
I've tried to do my homework, and I see that Clifford has discussed this several times, most notably here


where he mentions my exact need: attaching symbol-table references to tree nodes. My parser has a symbol-table stack, which I consult to make decisions in semantic predicates. But I need (or believe I need) to access the same symbol table during a post-parse translation task, so I'm currently (ab)using semantic predicates to attach the current symbol table to certain nodes I know will need it later. It'd be easier and cleaner to just do this for all nodes as they're constructed, but I haven't thought of a way to allow the node's constructor to access the parser's data structures.

I read with interest Clifford's comment in this thread:


But does this require modifying the Treetop code itself, to use InputProxy in place of the input string? I'd rather not do that, but maybe I don't understand the suggestion. Like the OP in the second thread there, I was previously using a class variable for data sharing, but don't want to do that, either.

I welcome any suggestions. I'm soooo late to the party, but I'd have voted for Clifford's proposed change if I'd been around. Any chance it might be revived?

thanks,
paul

Clifford Heath

unread,
May 21, 2013, 8:02:20 PM5/21/13
to treet...@googlegroups.com
On 22/05/2013, at 9:17 AM, Paul Madden <pma...@gmail.com> wrote:
> I read with interest Clifford's comment in this thread:
>
> https://groups.google.com/forum/?fromgroups#!topic/treetop-dev/FJkgreiy4IY
>
> But does this require modifying the Treetop code itself, to use InputProxy in place of the input string? I'd rather not do that, but maybe I don't understand the suggestion. Like the OP in the second thread there, I was previously using a class variable for data sharing, but don't want to do that, either.

You can see how I use it here:
<https://github.com/cjheath/activefacts/blob/master/lib/activefacts/cql/parser.rb>

Basically it's this:

class MyParser < TreetopGeneratedParser
class Context
...
end
class InputProxy

end
def parse(input, options = {})
input = InputProxy.new(input, context, self) unless input.respond_to?(:context)
super(input, options)
end
end

The "unless" is there in case a caller wanted to pass in a different kind of context.
as far as I know, that hasn't happened yet.

This makes it possible from anywhere in the parser (including in a sem-pred)
to say "input.context.whatever".

Clifford Heath.

Paul Madden

unread,
May 22, 2013, 11:11:15 PM5/22/13
to treet...@googlegroups.com
Thanks, Clifford -- I've applied this mechanism to my parser and it works perfectly.

paul
Reply all
Reply to author
Forward
0 new messages