Dashboard > ANTLR 3 > ... > Code Generation Targets > How to build an ANTLR code generation target
  ANTLR 3 Log In | Sign Up   View a printable version of the current page.  
  How to build an ANTLR code generation target
Added by Terence Parr, last edited by Luke A. Guest on May 23, 2007  (view change)
Labels: 
(None)

The following instructions show how to go about starting a new back-end for a new language "XYZ" using the Java back-end as a basis.

All the files in the antlr-3.0/runtime/Java/src/org/antlr/runtime directory are needed to create
a full back-end. You must also copy another group of templates such as antlr-3.0/src/org/antlr/codegen/templates/Java/*.stg. And create antlr-3.0/src/org/antlr/codegen/XYZTarget.java if you need to override anything in Target.java.

But it is in fact pretty easy to get started:

  • In src/org/antlr/codegen/templates/
    • create a directory XYZ
    • copy Java/Java.stg to XYZ/XYZ.stg
  • I recommend building the ANTLR tool 'in place'. Do not create a jar or compile/copy to a build directory. When you run it with 'java -cp path-to-src-dir ...' it will use the original *.stg file, which you'll edit a lot - so rebuilding the tool would be quite a PITA.
  • Create a directory antlr-3.0/runtime/XYZ. Here you can put anything you need (no need to clone Java 1:1).
  • Start with a simple lexer like:
lexer grammar T;
options { language = XYZ; }
ZERO: '0';

N.B: The name after the word "grammar" needs to be the same as the filename in which you save it, i.e. T.g.

  • Look at the generated code and try to figure out which templates in XYZ.stg you have to port to get valid XYZ code. What I did, is to comment out the Java code in all templates replacing it with something like FIXME([number]). Then you fix the templates until no FIXME remains in the
    output.
  • You'll need a basic implementation of a character stream and base recognizer/lexer to get the example running. Just implement the methods that are actually needed to get the example running w/o errors.
  • You'll either get the feeling "Wow, that was easy!" and move on (that happened to me) or "Eeek, what a pain!" and let someone else to the work.

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators