Antlr3RubyTarget

Ruby target

Author

Martin Traverso (mtraverso [at] acm [dot] org)

Status

The Ruby code generation target functional as of v3.0b4. Lexer and Parser generation are supported so far. Tree Parsers are not yet supported, though.

TODO

  • Return parameters
  • Scopes
  • ASTs
  • Error recovery
  • Memoization
  • Tree grammars

Some automated tests have been ported from the Java code generation target test suite, but more tests are needed (at least one for each string template)

Sample grammar

grammar Calculator;
options {
  language = Ruby;
}

evaluate returns [result]: r=expression { result = r };

expression returns [result]: r=mult (
    '+' r2=mult {
        r += r2
    }
  | '-' r2=mult {
        r -= r2
    }
  )* { result = r };

mult returns [result]: r=log (
    '*' r2=log {
        r *= r2
    }
  | '/' r2=log {
        r /= r2
    }
  | '%' r2=log {
        r %= r2
    }
  )* { result = r };

log returns [result]: 'ln' r=exp { result = Math::log(r) }
    | r=exp { result = r }
    ;

exp returns [result]: r=atom ('^' r2=atom { r = r ** r2 } )? { result = r }
    ;

atom returns [result]:
    n=INTEGER { result = $n.text.to_i }
  | n=DECIMAL { result = $n.text.to_f } 
  | '(' r=expression { result = r } ')'
  | 'PI' { result = Math::PI }
  | 'E' { result = Math::E }
  ;

INTEGER: DIGIT+;

DECIMAL: DIGIT+ '.' DIGIT+;

fragment
DIGIT: '0'..'9';

WS: (' ' | '\n' | '\t')+ { channel = 99 };
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. May 25, 2007

    benedetto says:

    Thanks for the good work on integrating antlr with Ruby. When using antlr versio...

    Thanks for the good work on integrating antlr with Ruby. When using antlr version 3.0 (May 17. 2007) with the above grammar, however, I get the following error:

    error(10): internal error: group Ruby does not satisfy interface ANTLRCore: missing templates [lexerRuleRefAndListLabel]

    Is there anything I can do about it other than implementing the missing functions myself ?

    Ben