[antlr-interest] Ruby target question

John Woods jqwoods at gmail.com
Tue Mar 25 13:29:42 PDT 2008


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?


More information about the antlr-interest mailing list