Examples:
:= is a token
i=j+++k;
calls for variable j to be postincremented (not for k to be preincremented)
by the maximal munch rule [no doubt named to be consistant with "bits", "bytes", and "nybbles"]:
If the input stream has been parsed into tokens up to a given character, the next token is taken to include the longest string of characters which could possibly constitute a token.
Kernighan and Ritchie, The C Programming Language, App. A, § 2
Examples:
<variable> = <expression>
if <boolean expression> then <statement>
Examples:
if <boolean expression> then <statement> else <statement>
Modula-2:
IF <boolean expression> THEN <statement sequence> ELSE <statement sequence> END
AND and OR is
the same in Pascal and Modula-2, but the semantics are different:
Modula-2 uses "lazy" (short-circuit, McCarthy) evaluation.
Examples:
<simpleExpr> ::= <term> | <sign><term> | <simpleExpr> <addOp> <term> <addOp> ::= + | - | or <unsigned integer> ::= <digit> | <unsigned integer><digit>
where:
::=' means "is defined as"
|' means "or"
<term>, are nonterminal symbols.
<sign> and <term> in the first
example above is intended to be significant. However, it is not always clear in the examples in the
textbook whether there are spaces between some pairs of elements or not.)
Note that the last example above is a recursive definition. This is the standard way of specifying sequences of elements in BNF, and for describing nested syntax, as in:
<statement> ::= <unconditional statement>
| if <expression> then <unconditional statement>
| if <expression> then <unconditional statement> else <statement>
BNF was first used in the Algol-60 Report. (Backus had used a previous version to describe Algol-58.)
Extended BNF improves the readability and conciseness of BNF through extensions:
<unsigned integer> ::= <digit>+
<identifier> ::= <letter><alphanumeric>*
Braces can be used for grouping elements, allowing us to avoid having to define intermediate classes
such as <alphanumeric>.
<identifier> ::= <letter>{<letter>|<digit>}*
Note: Many authors use braces to mean zero or more repetitions of the enclosed elements (i.e., a Kleene star is assumed).
Square brackets may be used to indicate optional elements (i.e., zero or one occurrence of the enclosed elements).
<integer> ::= [ + | - ]<unsigned integer>
Do not use a Kleene cross or star with square brackets.
<identifier> ::= <letter>{ <letter>
<digit>} *
<integer> ::=[ +
-] <unsigned integer>
Syntax diagrams show legal strings in diagram form
Syntax diagrams were first used in the Pascal User Manual and Report by Jensen and Wirth.
Give the equivalent of the syntax diagram above in EBNF.
<pop> ::= [<bop>, <pop>] | <bop>
<bop> ::= <boop> | (<pop>)
<boop> ::= a | b | c
Note that, since this is BNF, not EBNF, square brackets are terminal symbols.
For each of the strings listed below, indicate all the syntactic categories of which it is a member, if any.
c
(a)
[b]
([a,b])
[(a),b]
[(a),[b,a]]