Skip to content

Generating Code from Basic Lexer Parser Grammars

ohboyohboyohboy edited this page Sep 13, 2010 · 9 revisions

ANTLR already has pretty thorough documentation about its grammar syntax and code usage:

here

Thus, I am not going to try and explain ANTLR’s purpose or grammar syntax here. Instead, I am goign to try and focus on how to use the ruby antlr3 tools and runtime library to generate and use recognizers written in ruby.

1. Install the ruby antlr3 target:

gem install antlr3

2. Write a grammar, ensuring the language = Ruby; is set in the grammar options:

/**grammar file  Whatevs.g */
grammar Whatevs;

options {
     language = Ruby;
}

a_rule:  another_rule+;
... // and so on

3. Generate output using the antlr4ruby script that comes with the antlr3 gem

antl4ruby Whatevs.g

  • you will get several files:
    • WhatevsParser.rb: contains a parser class definition
    • WhatevsLexer.rb: contains a lexer class definitions
    • Whatevs.tokens: the token vocabulary produced by your grammar
Clone this wiki locally