TreeTop: dealing with Grammars in multiple files? - RUBY

This is a discussion on TreeTop: dealing with Grammars in multiple files? - RUBY ; 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....

+ Reply to Thread
Results 1 to 4 of 4

TreeTop: dealing with Grammars in multiple files?

  1. Default 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.


  2. Default Re: TreeTop: dealing with Grammars in multiple files?

    Phil Tomson wrote:
    > 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.

  3. Default Re: TreeTop: dealing with Grammars in multiple files?

    On 2/6/08, Clifford Heath <no@spam.please.net> wrote:
    > Phil Tomson wrote:
    > > 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.



    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


  4. Default Re: TreeTop: dealing with Grammars in multiple files?

    Phil Tomson 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 Heath.

+ Reply to Thread