Grammar - Rule Construct / Basic Rule Expression

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


Powered by ComboStrap