I am using treetop to construct a grammar but am having problems with
what seems to be a circular dependency.
Consider the following structure:
test.rb
lib/A.treetop
lib/B.treetop
lib/AB.treetop
lib/all.rb
And the sources for the files
# test.rb
require File.dirname(__FILE__) + "/lib/all"
# lib/A.treetop
grammar A
include AB
end
# lib/B.treetop
grammar B
include AB
end
# lib/AB.treetop
grammar AB
include A
include B
end
# lib/all.rb
require 'treetop'
require File.dirname(__FILE__) + '/A'
require File.dirname(__FILE__) + '/B'
require File.dirname(__FILE__) + '/AB'
It's very obvious that I've introduced a circular dependency.
Now when I run test.rb
ruby test.rb
I receive the following error:
/home/eric/.rvm/gems/ruby-1.9.2-rc2/gems/treetop-1.4.8/lib/treetop/
compiler/grammar_compiler.rb:42:in `class_eval': uninitialized
constant A::AB (NameError)
....
meaning that it could not include the 'AB' grammar in the 'A' grammar
since it's after it in the 'all.rb' file.
However, this circular dependency is necessary.
Consider this pseudo-grammar where it would be applicable:
# Statement file
include ForStatement
include SwitchStatement
statement -> for_statement / switch_statement / other_statement
# ForStatement file
include Statement
for_statement -> 'for (' expr ')' statement
As you can see, the for loop can contain other statements inside it.
But I cannot accomplish this unless all the rules are in the same
file.
Can someone please point me in the right direction on how to
accomplish this if it's even possible?