views:

178

answers:

3

Most things that look like operators are methods in Ruby; 1 + 2 is syntactic sugar for 1.+(2).

Even though + and * are methods that a program can redefine, Ruby has special magic to evaluate 1 + 2 * 3 as 1.+(2.*(3)) instead of 1.+(2).*(3).

I wonder where this special magic lives in Ruby--if it is hard-wired into the interpreter.

Ari.

A: 

Yes, it's hardwired, so you can't add new operators or change the precedence of existing operators.

sepp2k
+1  A: 

"Operator Expressions" in the language documentation gives a table with the operators that can be overridden as methods. You can't make up your own operators — the mapping of operators to their symbol names lives inside the parser.

jleedev
Thank you for the link--I a familiar with it and should have included it in the question. I am looking for a more specific answer than "presumably lives inside the parser." Perhaps I can phrase it so: if I wanted to fork my own version of Ruby where `*` had lower precedence than `+`, which file(s) would I change.Ari.
iter
+7  A: 

In all Ruby Implementations, Operator Precedence is handled by the parser. Since pretty much all existing Ruby Implementations use the same parser, or a parser generated from the same YACC grammar, parse.y in YARV is the file you want to look at. (In JRuby, for example, that file is essentially the same: src/org/jruby/parser/Ruby19Parser.y. Same for IronRuby: Merlin/Main/Languages/Ruby/Ruby/Compiler/Parser/Parser.y.)

The only four Ruby Implementations that do not either use the YARV parser directly or use a YACC clone generated parser from YARV's parse.y, are Cardinal, tinyrb, RubyGoLightly and XRuby.

Cardinal is a Ruby implementation for the Parrot virtual machine, and since Parrot includes the Parrot Grammar Engine, Cardinal naturally uses that. The interesting file is src/parser/grammar.pg. PGE is a hybrid recursive-descent parser/operator precedence parser, which means that operator precedence shows up pretty nicely in the grammar file.

Tinyrb uses a PEG parser utilizing Ian Piumarta's leg library. As is typical for PEG parsers, there is no operator precedence table, rather the precedence is implicit in the hierarchical structure of the grammar. See vm/grammar.leg for details. RubyGoLightly is derived from tinyrb, except it uses Go instead of C as the implementation language, but it uses the same PEG grammar.

XRuby uses ANTLR for its parser. Here, the interesting file is src/com/xruby/compiler/parser/ruby.g.

Rubinius uses the Melbourne parser, which is essentially YARV's parser packaged as a C extension. MagLev uses ruby_parser (see below).

Apart from the Ruby Implementations, there are also other Ruby parsers available.

Ryan Davis's ruby_parser is derived from the YARV YACC grammar. It uses racc as the parser generator. See lib/ruby_parser.y.

Caleb Clausen's RedParse uses Caleb's own hand-written compiler-interpreter. The interesting file is lib/redparse/babyparser.rb.

That's all the parsers I know, that actually handle operator precedence. There is another parser built into RDoc, and there used to be one in YARD (it now uses RedParse), but those only handle just enough of Ruby's syntax to find modules, classes and methods, comments and extract method parameter lists. They don't deal with operator precedence.

Jörg W Mittag
Thank you. Your answer is very detailed and has excellent pointers to files including line numbers. It greatly exceeds my expectations.I look up some of your other answers and learn much about Ruby implementations. Thank you for sharing your knowledge.
iter
You're welcome!
Jörg W Mittag