Antlr - (Grammar|Lexicon) (g4)

Card Puncher Data Processing

About

Grammar in the context of Antlr.

The grammar definition of Antlr is called a Lexicon because the grammar is used by the lexer (hence the lexer grammar)

See:

Name

The file name containing grammar X must be called X.g4

File

The name of the file containing the grammar must match the grammar-name and have a .g4 extension.

The name of the grammar must be declared at the top of the file.

The syntax can be specified in a file with syntax:

  • only for the lexer called the lexer grammar
  • only for the parser called the parser grammar
  • or for the lexer and parser combined. (Default). The tree grammar in one file is the most common approach.

Syntax

Code Source Structure. See antlr/antlr4/blob/master/doc/grammars.md

/** Optional javadoc style comment */
grammar_type? grammar Name;
options {...}
import ... ;

tokens {...}
channels {...} // Only lexer grammars can contain custom channels specifications
@actionName {...}

rule1 // parser and lexer rules, possibly intermingled
...
ruleN

where:

Tokens

Token definition needed by a grammar for which there is no associated lexical rule. The tokens section defines a set of tokens to add to the overall set.

Rule

There is two kinds of rule:

Name Case Type Description Example from the getting started
uppercase letter lexer rule (known as Token name, they defines the token that the lexer will produce ID : [a-z]+ ; defines an ID token that is made of letters from a to z
lowercase letter parser rule They defines how the relation between the token and therefore how the parser tree is build r : 'hello' ID ; defines a pattern with the world hello and the token ID defines just above

Channels

Only lexer grammars can contain custom channels specifications

channels {
  WHITESPACE_CHANNEL,
  COMMENTS_CHANNEL
}

Action

Actions are code blocks written in the target language.

Keywords / Reserved words

Reserved Words:

  • import: for grammar import
  • fragment
  • lexer
  • parser
  • grammar
  • returns
  • locals
  • throws
  • catch
  • finally
  • mode
  • options
  • tokens

Further, do not use:

  • the word rule as a rule name.
  • any keyword of the target language as a token, label, or rule name. For example, rule if would result in a generated function called if. That would not compile obviously.

Option

Grammar options

Tree parser

Antlr - Parse Tree (AST)

Documentation / Reference





Discover More
Card Puncher Data Processing
ANTLR - Tool

The ANTLR tool helps create the grammar and generate its corresponding lexer+parser code. It can be run from the following application: ANTLR Intellij plug-in ANTLRWorks antlr4 - the command...
Card Puncher Data Processing
Antlr (ANother Tool for Language Recognition)

ANTLR is lexer generator. It translates: a grammar to a lexer and parser. ANTLR is implemented in Java and generates lexer and parser in the following languages: Java, Ruby, Python, C,...
Card Puncher Data Processing
Antlr - (Lexical) Rule

in Antlr. Antlr has two types of rule: Name Case Type Description Example from the getting started uppercase letter lexer rule (known as Token name, they defines the token that the lexer...
Card Puncher Data Processing
Antlr - ANTLRWorks GUI

ANTLRWorks is a standalone GUI application that run Antlr tool (to helps with the grammar and generation of lexer+parser code)
Card Puncher Data Processing
Antlr - Actions

Actions are code blocks in the grammar written in the target language. Grammar actions add code to the generated class. You can use actions in a number of places within a grammar, but the syntax...
Card Puncher Data Processing
Antlr - Generated class

From the grammar The lexer rules will create the lexer class The parser rules will create the parser class The classes generated will contain a method for each rule in the grammar. See from...
Idea Antlr Right Click Options
Antlr - Getting Started (Hello World)

A getting started page that brings you in the world of Antlr. antlr/antlr4/blob/master/doc/getting-started.mdantlr4 getting-started Create a grammar file called Hello.g4 and define the grammar...
Card Puncher Data Processing
Antlr - Grammar Option

Option for the grammar Grammar options are specified using the following syntax. antlr/antlr4/blob/master/doc/options.md
Idea Antlr Right Click Options
Antlr - Idea Plugin

The Idea plugin is a plugin for Idea that install the Antlr tool. Create a grammar file with the extension g4 and Idea should propose you to install the Antlr...
Idea Antlr Right Click Options
Antlr - Installation (Version 4)

Installation of Antlr tool on . on Idea Create a grammar file with the extension g4 and Idea should propose you to install the Antlr plugin Right click on your g4 file, you should see the...



Share this page:
Follow us:
Task Runner