[antlr-interest] Ruby target question

Ana Nelson nelson.ana at gmail.com
Wed Mar 26 02:32:34 PDT 2008


Hi, John,

I looked at the source, which is in
src/org/antlr/codegen/templates/Ruby/Ruby.stg, and the require statement is
generated by:

require '<grammar.name>Lexer'

so it's hard-coded.

Modifying the load path seems like the better approach to me, but if you
wanted to store ALL your parsers in a parser/ directory then you could edit
the template.

-Ana



On 25/03/2008, John Woods <jqwoods at gmail.com> wrote:
>
>
> One solution that I found is to add the parser sub-dir to the load path
> before calling require
>
>
>    $ cat ./Main.rb
>
>    #!/usr/bin/ruby -w
>
>    $: << './parser'
>
>    require 'parser/MyGrammarLexer'
>    require 'parser/MyGrammarParser'
>
>
> That seems to work fine. But I'm still wondering if there's some fix to
> the grammar file that will generate the desired require statement in the
> ANTLR output.
>
>
>
> -----Original Message-----
> From: John Woods
> Sent: 03/25/2008 01:29 PM
> > I'm having trouble structuring my ANTLR generated ruby code into a
> > sub-directory of my project. I'm not sure if this is a problem with my
> > grammar file, or with the way I'm invoking the ANTLR tool, or with the
> > way I'm invoking the Ruby interpreter. Any pointers would be
> appreciated.
> >
> > I would like to locate my grammar file under a subdir named "parser":
> >
> >   $ cat ./parser/MyGrammar.rb
> >
> >   grammar MyGrammar;
> >   options {
> >       language = Ruby;
> >   }
> >   my_rule: MY_TOKEN+ ;
> >   MY_TOKEN: ('a'..'z'|'A'..'Z')+ ;
> >   WHITESPACE: (' '|'\t'|'\r'|'\n') { skip(); };
> >
> > Running ANTLR generates the expected files just fine:
> >
> >   $ ls ./parser/*
> >
> >   ./parser/MyGrammar__.g
> >   ./parser/MyGrammar.g
> >   ./parser/MyGrammarLexer.rb
> >   ./parser/MyGrammarParser.rb
> >   ./parser/MyGrammar.tokens
> >
> > In my ./Main.rb file I want to require both the parser and lexer code:
> >
> >   $ cat ./Main.rb
> >
> >   #!/usr/bin/ruby -w
> >   require 'parser/MyGrammarLexer'
> >   require 'parser/MyGrammarParser'
> >
> > The problem is that ./parser/MyGrammarParser.rb contains the following
> > line:
> >
> >   require 'MyGrammarLexer'
> >
> > Ruby fails to find MyGrammarLexer.rb when executing from another
> > directory. It works fine if I edit ./parser/MyGrammarParser.rb, changing
> > the above line to:
> >
> >   require 'parser/MyGrammarLexer'
> >
> > But obviously editing generated code is a no-no.
> >
> > Any suggestions?
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080326/3814db52/attachment.html 


More information about the antlr-interest mailing list