Defining Rules

Syntax

RuleName [ Newline ] ::= [ Symbols [ Newline ] | ] ...

 

Rule Definition

Details

Typically, rules in a grammar are declared using BNF (Backus-Naur Form) statements. This notation consists series of 0 or more symbols where nonterminals are delimited by the angle brackets '<' and '>' and terminals are delimited by single quotes or not delimited at all.

For instance, the following declares the common if-statement.

<Statement> ::= if <Expression> then <Statements> end if

The symbols 'if', 'then', 'end', and 'if' are terminals and <Expression> and <Statements> are nonterminals. 

If you are declaring a series of rules that derive the same nonterminal (i.e. different versions of a rule), you can use a single pipe character '|' in the place of the rule's name and the "::=" symbol. The following declares a series of 3 different rules that define a 'Statement'.

<Statement> ::= if <Expression> then <Statements> end if
             |   while <Expression> do <Statements> end while
             |  for Id = <Range> loop <Statements> end for

 

Note: When text is read by the Builder, all characters delimited by single quotes are analyzed as literal strings. In other words, any text delimited by single quotes is considered to be exactly as printed. This allows you to specify characters that would normally be limited by the notation. For instance, when defining a rule, angle brackets are used to delimit nonterminals. By typing '<' and '>', you can specify these two characters without worrying about the system misinterpreting them. A single quote character can be specified by typing two single quotes ''.

Additional Examples

The following two rules define a comma delimited list of Identifiers.

<List> ::= <List> ',' Identifier
         | Identifier

 

Operator precedence is an important aspect of most programming languages. The following rules define the common arithmetic operators.

<Expression> ::= <Expression> '+' <Mult Exp>
               | <Expression> '-' <Mult Exp>
               | <Mult Exp>

<Mult Exp>   ::= <Mult Exp> '*' <Negate Exp>
               | <Mult Exp> '/' <Negate Exp>
               | <Negate Exp>

<Negate Exp> ::= '-' <Value>
               | <Value>

<Value>      ::= ID
               | Integer
               | '(' <Expression> ')'