Antlr - Parser Rule

Card Puncher Data Processing

About

Compiler - Parser Rule in Antlr.

Parser rule is the second type of rule for Antlr. They begin with a lowercase letter.

The lexer rules specify the tokens whereas the parser rules specify the tree.

Example

URL

URI

url : authority '://' login? host (':' port)? ('/' path)? ('?' search)? ;
authority : STRING;
hostname : STRING ('.' STRING)* ;
host_ip : DIGITS '.' DIGITS '.' DIGITS '.' DIGITS ;
host : hostname | hostnumber;
path: STRING ('/' STRING)* ;
user: STRING;
port: DIGITS;

Rule expression Construct

See Grammar - Rule Construct / Basic Rule Expression

Syntax

Parser rule names always start with a lowercase letter (whereas Lexer rule names (known als as Token name) must begin with an uppercase letter.)

Basic

A rule name followed by a single alternative terminated with a semicolon.

retstat : 'return' expr ';' ;

Alternative

Rules can also have alternatives separated by the logical matcher |

operator:
    stat: retstat
    | 'break' ';'
    | 'continue' ';'
    ;

Label and tree event

Parse-tree listener events are created when labeling the outermost alternatives of a rule using the # operator.

All alternatives within a rule must be labeled, or none of them.

Example: Two rules with alternatives

grammar T;
stat: 'return' e ';' # Return
    | 'break' ';' # Break
    ;
e   : e '*' e # Mult
    | e '+' e # Add
    | INT # Int
    ;

Alternative labels do not have to be at the end of the line and there does not have to be a space after the # symbol.

ANTLR generates a rule context class definition for each label. For example, here is the listener that ANTLR generates:

public interface AListener extends ParseTreeListener {
    void enterReturn(AParser.ReturnContext ctx);
    void exitReturn(AParser.ReturnContext ctx);
    void enterBreak(AParser.BreakContext ctx);
    void exitBreak(AParser.BreakContext ctx);
    void enterMult(AParser.MultContext ctx);
    void exitMult(AParser.MultContext ctx);
    void enterAdd(AParser.AddContext ctx);
    void exitAdd(AParser.AddContext ctx);
    void enterInt(AParser.IntContext ctx);
    void exitInt(AParser.IntContext ctx);
}

There are enter and exit methods associated with each labeled alternative. The parameters to those methods are specific to alternatives.

You can reuse the same label on multiple alternatives to indicate that the parse tree walker should trigger the same event for those alternatives. For example, here’s a variation on rule e from grammar A above:

e : e '*' e # BinaryOp
| e '+' e # BinaryOp
| INT # Int
;

ANTLR would generate the following listener methods for e:

void enterBinaryOp(AParser.BinaryOpContext ctx);
void exitBinaryOp(AParser.BinaryOpContext ctx);
void enterInt(AParser.IntContext ctx);
void exitInt(AParser.IntContext ctx);

Documentation / Reference





Discover More
Card Puncher Data Processing
Antlr - (Grammar|Lexicon) (g4)

Grammar in the context of Antlr. The grammar definition of Antlr is called a antlr/antlr4/blob/master/doc/lexicon.mdLexicon because the grammar is used by the lexer (hence the lexer grammar) See: ...
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 - 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 - Lexer Rule (Token names|Lexical Rule)

in Antlr. They are rules that defines tokens. They are written generally in the grammar but may be written in a lexer grammar file Each lexer rule is either matched or not so every lexer rule expression...
Antlr Idea Plugin
Antlr - Parse Tree (AST)

The tree parser is an AST that is created by the parser from a text input. with grun, see the -tree of -gui option of Example: Type your text and end with a End of File character (Ctrl+Z or...
Card Puncher Data Processing
Antlr - Parser

in Antlr. The parser is a generated class that is build from the grammar (specifically from the parser rule of the grammar). The job of the parser is to create a parse tree from: a parser start rule...
Card Puncher Data Processing
Antlr - Parser Grammar

The grammar file of the parser contains the parser rule. grammar file If you have two differents file and your lexer grammar is defined in another file, you need to define it with the tokenVocab...
Card Puncher Data Processing
Antlr - Start Rule

A start rule is the rule engaged first by the parser to create the tree. Any rule in the grammar can act as a start rule. ...
Compiler
Grammar - Rule Construct / Basic Rule Expression

This page lists common rule expression in order to describe the structure of the parsed document. A rule expression is a pattern expression that produces a boolean and are therefore predicate. grammar...



Share this page:
Follow us:
Task Runner