TreeTop: dealing with Grammars in multiple files?

I know that you can include one grammar into another like so:

grammar foo
include Bar_grammar
end

But what if Bar_grammar is defined in a different file? I tried:

load_grammar “Bar_grammar”
grammar Foo
include Bar_grammar
end

but that doesnt’ work.

Phil T. wrote:

include Bar_grammar
end

but that doesnt’ work.

I haven’t done this, but I think I might have said this was the
way to compose grammars. I suspect I was wrong, and that what you
need to do is to require both grammars, then re-open one of the
generated parser classes (which includes the module for that
grammar), and simply include the other grammar.

BTW, load_grammar is deprecated - in fact I thought it had been
removed. Are you using an old gem? The current incantation is
to call Treetop.load, or just to simply require the .treetop
file (which gets Polyglot to load it for you).

The file to be required may be called with either a .tt or .treetop
extension.

Clifford H…

On 2/6/08, Clifford H. [email protected] wrote:

grammar Foo
grammar), and simply include the other grammar.

BTW, load_grammar is deprecated - in fact I thought it had been
removed. Are you using an old gem? The current incantation is
to call Treetop.load, or just to simply require the .treetop
file (which gets Polyglot to load it for you).

The file to be required may be called with either a .tt or .treetop
extension.

sorry for the long delay… just got back to this problem.

So you’re saying that I could have two files each which defines a
grammar:
#keywords.treetop
grammar Keywords

end

#Foo.treetop
grammar Foo

end

OK, so now I’ve got keywords.treetop and Foo.treetop, so are you
saying that I would then do something like:

#main.rb
require “keywords”
require “Foo”

And then open the Foo module like so:

module Foo
include Keywords
end

… or something like that??

Phil

Phil T. wrote:

And then open the Foo module like so:

module Foo
include Keywords
end

… or something like that??

Exactly that. Each rule becomes a method, so including one
module in the other makes all rules available to that parser.

Let us know how it goes, but in principle it should work fine.

Clifford H…