# TreeTop: dealing with Grammars in multiple files?

**URL:** <https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364>\
**Category:** Ruby\
**Created:** [February 7, 2008, 2:43am UTC](https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364 "2008-02-07T02:43:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [February 7, 2008, 2:43am UTC](https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364/1 "2008-02-07T02:43:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [February 7, 2008, 3:05am UTC](https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364/2 "2008-02-07T03:05:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [February 29, 2008, 4:51am UTC](https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364/3 "2008-02-29T04:51:45Z")

</div>

On 2/6/08, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![philthompson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@philthompson](https://www.ruby-forum.com/u/philthompson)\
**Post date:** [February 29, 2008, 5:31am UTC](https://www.ruby-forum.com/t/treetop-dealing-with-grammars-in-multiple-files/128364/4 "2008-02-29T05:31:00Z")

</div>

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…
