TreeTop: dealing with Grammars in multiple files?

P

Phil Tomson

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.
 
C

Clifford Heath

Phil said:
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.

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 Heath.
 
P

Phil Tomson

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.


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
 
C

Clifford Heath

Phil said:
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 Heath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top