Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added a couple of thoughts at the end and tidied the layout

Five minute introduction to ANTLR 3

What is ANTLR 3?

ANTLR – *AN*other *T*ool for *L*anguage *RE*cognition ANother Tool for Language REcognition – is a tool that helps you write language processing tools. It's commonly categorised as a compiler generator or compiler compiler in the tradition of tools such as Lex/Flex and Yacc/Bison). ANTLR takes a grammar description (which may define both a language and how to process the language's constructs) and emits multiple files in your chosen target language (e.g. Java, C/C++, C#, Python, Ruby...).

...

  • How to write lexer rules
  • How to write basic parser rules
  • How to direct tokens away from the parser (to ignore them)
  • How to insert executable code into a parser.

Some points to consider:

  • You can insert custom actions anywhere.
  • Most of your custom code winds up in the last stage of the parsing process. Here it was in the Parser; if you used an AST, it would be in the tree parser.

What next?

This covers the majority of the things you need to know to develop a grammar. You may want to work through another of the tutorials:

...

You could also:

Special constructs (reference)

...