[antlr-interest] Ruby target question

John Woods jqwoods at gmail.com
Tue Mar 25 13:44:00 PDT 2008


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?
> 



More information about the antlr-interest mailing list