What does “fragment” mean in ANTLR?

A fragment is somewhat akin to an inline function: It makes the grammar more readable and easier to maintain. A fragment will never be counted as a token, it only serves to simplify a grammar. Consider: NUMBER: DIGITS | OCTAL_DIGITS | HEX_DIGITS; fragment DIGITS: ‘1’..’9′ ‘0’..’9’*; fragment OCTAL_DIGITS: ‘0’ ‘0’..’7’+; fragment HEX_DIGITS: ‘0x’ (‘0’..’9′ | … Read more

lexers vs parsers

What parsers and lexers have in common: They read symbols of some alphabet from their input. Hint: The alphabet doesn’t necessarily have to be of letters. But it has to be of symbols which are atomic for the language understood by parser/lexer. Symbols for the lexer: ASCII characters. Symbols for the parser: the particular tokens, … Read more

If/else statements in ANTLR using listeners

By default, ANTLR 4 generates listeners. But if you give org.antlr.v4.Tool the command line parameter -visitor, ANTLR generates visitor classes for you. These work much like listeners, but give you more control over which (sub) trees are walked/visited. This is particularly useful if you want to exclude certain (sub) trees (like else/if blocks, as in … Read more

ANTLR: Is there a simple example?

Note: this answer is for ANTLR3! If you’re looking for an ANTLR4 example, then this Q&A demonstrates how to create a simple expression parser, and evaluator using ANTLR4. You first create a grammar. Below is a small grammar that you can use to evaluate expressions that are built using the 4 basic math operators: +, … Read more

What is a ‘semantic predicate’ in ANTLR?

ANTLR 4 For predicates in ANTLR 4, checkout these stackoverflow Q&A’s: Syntax of semantic predicates in Antlr4 Semantic predicates in ANTLR4? ANTLR 3 A semantic predicate is a way to enforce extra (semantic) rules upon grammar actions using plain code. There are 3 types of semantic predicates: validating semantic predicates; gated semantic predicates; disambiguating semantic … Read more