Grammar - Rule Construct / Basic Rule Expression

Compiler

About

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.

To see how this expressions are translated into code, see the grammar to code page

Common Pattern

They are written in Basic PEG / BNF syntax

Note that EBNF quantifier like (…)?, (…)* and (…)+ are greedy

Sequence

rule ::= part1 part2 part3
rule ::= part1 part2 part3

Ordered choice

Ordered choice (also known as alternative) is expressed with the or logical matcher (ie |)

rule : (x|y|z)
/* example */
returnType : (type | 'void') ;
Rule := ( part1 | part2 | part3 );
Rule := ( part1 | part2 | part3 );

Zero-or-more

With the * quantifier (ie Match an alternative within subrule zero or more times. )

( part ) * 
( part ) * 
  • antlr
word : letter letter*
(part1 | part 2 | part 3)* 
rule := letter (letter)*

One-or-more

With the + quantifier (ie Match an alternative within subrule zero or more times )

( oneormore )+
( oneormore )+
  • antlr
annotations : (annotation)+ ;

Optional

Match part1 or nothing. This is the ? greedy quantifier (ie o or 1)

  • Bnf
part1?
part1?
  • Antlr
(part1)?
  • Square brackets also enclose optional elements:
rule_B ::= [ optional_token ] and_another_one?
  • Optionality may be also expressed with the or logical matcher (ie |). Example with optional space
<opt-whitespace> ::= " " <opt-whitespace> | ""

Here’s another antlr rule with an empty alternative that makes the entire rule optional:

superClass
    : 'extends' ID
    | // empty means other alternative(s) are optional
    ;

Predicate

  • And-predicate
rule_C := &required
rule_C := &required
  • Not-predicate
rule_C ::= !forbidden
  • Not-predicate with optionality
rule_recover ::= !(';' | id '=')

Grouping and repetition

rule_D ::= { can_use_braces + (and_parens) * }
rule_D ::= { can_use_braces + (and_parens) * }

One-or-more

With the + quantifier

letter+
letter+

Documentation / Reference





Discover More
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 - 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. URL URI See ...
Compiler
Compiler (Parser/Lexer) - Exception handling

exception handling in a parser. By default, parser will stop matching on any failure. But they may have a sort of recovery mode that allows to define an alternate rule or match . For idea, it's...
Compiler
Grammar-to-code mappings

This page shows some generated pseudo-code from rule constructs. Rule modifiers and grammar attributes (like pin, recoverWhile) will add some lines.
Compiler
Language - (Grammar|Production) Rule

Recursive rules (known as production): are essential for describing the syntax of programming languages, and are a part of every compiler. play an important role in describing the syntax of natural...



Share this page:
Follow us:
Task Runner