# How to write a ruby compiler using Java?

**URL:** <https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535>\
**Category:** Ruby\
**Created:** [July 27, 2007, 10:30am UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535 "2007-07-27T10:30:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [July 27, 2007, 10:30am UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535/1 "2007-07-27T10:30:28Z")

</div>

Hi,

I am using Java to write a Ruby compiler. I build up the editor by  
Java SWing.  
Now the tough task for me is syntax parser. Does someone have some  
suggestions about how to check the validity of ruby codes in the editor  
and how to mark the grammar-incompliant points? Are there some plugins I  
can make use of, or do I need to parse the grammar by myself? If I want  
to parse the grammar by myself, where is the complete grammar definition  
for ruby lang? E.G., like C99(ISO 9899:1999) for C lang. Seems there is  
not a complete grammar definition for ruby lang at  
[http://www.ruby-lang.org](http://www.ruby-lang.org).  
Many Thanks.

Best Rgds,  
Shiwei

---

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [July 27, 2007, 10:40am UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535/2 "2007-07-27T10:40:29Z")

</div>

2007/7/27, shiwei zhang [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am using Java to write a Ruby compiler. I build up the editor by  
> Java SWing.  
> Now the tough task for me is syntax parser. Does someone have some  
> suggestions about how to check the validity of ruby codes in the editor  
> and how to mark the grammar-incompliant points? Are there some plugins I  
> can make use of, or do I need to parse the grammar by myself? If I want  
> to parse the grammar by myself, where is the complete grammar definition  
> for ruby lang? E.G., like C99(ISO 9899:1999) for C lang. Seems there is  
> not a complete grammar definition for ruby lang at [http://www.ruby-lang.org](http://www.ruby-lang.org).

Why not join [http://jruby.codehaus.org/](http://jruby.codehaus.org/)? There you might also find  
answers to your questions.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [July 27, 2007, 11:24am UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535/3 "2007-07-27T11:24:32Z")

</div>

shiwei zhang wrote:

> not a complete grammar definition for ruby lang at  
> [http://www.ruby-lang.org](http://www.ruby-lang.org).  
> Many Thanks.

There are basically two known good grammars for Ruby. One is the  
original Ruby 1.8 YACC-based parser grammar and variations of it (JRuby,  
Rubinius, and [ruby.net at Directnic](http://Ruby.NET/IronRuby) all use variations or ports of that  
grammar). The other is an ANTLR grammar used by the XRuby project. Only  
the YACC grammars are known to be “compatible” at present, but XRuby’s  
ANTLR grammar seems to be very close.

If you are looking for a grammar/AST to use for an editor, your best bet  
is probably JRuby’s. Not only do we have the most popular grammar and  
AST for Java-based editors, but we have an additional set of utility  
code to allow inspecting, reversing, and manipulating the AST. NetBeans,  
Eclipse, and JEdit-based Ruby editors are based on our parser and AST,  
as is (I believe) the IntelliJ Ruby editor. We also have been building  
the JRuby R.-to-bytecode compiler based on that AST.

- Charlie

---

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [July 27, 2007, 4:14pm UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535/4 "2007-07-27T16:14:01Z")

</div>

Charles Oliver N. wrote:

> > complete grammar definition for ruby lang? E.G., like C99(ISO

Just out of curiosity, where do the “Zen” tools (ParseTree, Ruby2Ruby,  
etc.) fit into this scheme of things?

Also, is there such a thing as Test Driven/Behavior Driven parser  
development? Should there be? 😉

---

<div class="post-metadata">

**Author:** ![shiwei\_zhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb8c5e/32.png) [@shiwei\_zhang](https://www.ruby-forum.com/u/shiwei_zhang)\
**Post date:** [July 27, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/how-to-write-a-ruby-compiler-using-java/110535/5 "2007-07-27T21:13:41Z")

</div>

M. Edward (Ed) Borasky wrote:

> Just out of curiosity, where do the “Zen” tools (ParseTree, Ruby2Ruby,  
> etc.) fit into this scheme of things?

Most of the tools you’re thinking of depend on C code that can access or  
expose the Ruby 1.8 AST. There is a port of ParseTree for JRuby called  
JParseTree, and it’s mostly complete but not being used for anything.  
Several of the other libraries, like Ruby2Ruby, depend mostly? only? on  
ParseTree.

RubyInline depends on both C code/compilers and the Ruby 1.8 APIs, and  
as yet there is no port.

I believe others in the zen family would qualify as “stock” extensions.  
Of course Ryan can give a more specific overview; my perspective is  
mostly that of an alternative implementer.

- Charlie
