Metalanguages are obscure
Mike
but can be figured out.
cs_logo

CS 636 / 338: The Structure of Programming Languages

EBNF
From class 5/1/04
Email A.Fischer at work Email A.Fischer at home

Extended Backus-Naur Form (EBNF) is a metalanguage for defining the syntax of a context-free language. It is almost universally used to define programming langues. A context-free grammar can define a language with several kinds of symbols that must occur in matching pairs (like BEGIN and END) and can be nested, to any depth. A context-free language cannot define the parts of a language that depend on the context in which a symbol is written. For example, it cannot define the type-matching rules of C.

A small set of meta-symbols are part of EBNF itself. These are:

In addition to the metasymbols, an EBNF grammar has four parts:

For example, here is an EBNF grammar for a silly language called "blab":

In rule 1, note the difference between the parentheses that are metasymbols and the ones that are terminal symbols. This rule also illustrates the typical pattern for defining matched pairs of symbols (such as parentheses.)

Rule 2 illustrates repetition by nesting. This rule could produce any number of a's followed by the same number of z's.

Rule 3 illustrates repetition by recursion. Such rules must have two alternatives, a non-recursive case and the recursive case. If the recursion is at the right end of the rule, it is easy for a compiler to parse. Recursions on the left are harder to work with. Here are a few widgets:    b    b x b    b x b x b    b x b x b x b

Rule 4 gives us one c or a series of any number of c's separated by semicolons. Rules like this are used very often in practice.

The shortest two blab sentences are:    b ( c ) b     and     b ( a z ) b
A longer sentence is:    b ( c; c; c; c; c; c; c; c; c; c; c; c; c ) b
A sentence with nested repetition is:    b ( a a a z z z ) b

<< Back
Last updated: 5/2/04