acceptable programs. For C, the usual BNF definition allows constructs such as
char a, b, c;
followed by
c = 3.2;
This combination is not acceptable to C compilers since it violates the constraint,
“a character variable cannot be assigned a real value.” Context-free grammars
cannot express the fact that type clashes may not be permitted. Such rules are
part of programming language semantics, since they have to do with how we
interpret the meaning of a particular construct.
Programming language semantics are a complicated matter. Nothing as
elegant and concise as context-free grammars exists for the specification of
programming language semantics, and consequently some semantic features
may be poorly defined or ambiguous. It is an ongoing concern both in
programming languages and in formal language theory to find effective methods
for defining programming language semantics. Several methods have been
proposed, but none of them has been as universally accepted and are as
successful for semantic definition as context-free languages have been for
syntax.
EXERCISES
1. Consult a book on C for formal defintions of the following constructs.
(a) literal
(b) for statement
(c) if-else statement
(d) do statement
(e) compound statement
(f) return statement
2. Find examples of features of C that cannot be described by context-free
grammars.
char a, b, c;
followed by
c = 3.2;
This combination is not acceptable to C compilers since it violates the constraint,
“a character variable cannot be assigned a real value.” Context-free grammars
cannot express the fact that type clashes may not be permitted. Such rules are
part of programming language semantics, since they have to do with how we
interpret the meaning of a particular construct.
Programming language semantics are a complicated matter. Nothing as
elegant and concise as context-free grammars exists for the specification of
programming language semantics, and consequently some semantic features
may be poorly defined or ambiguous. It is an ongoing concern both in
programming languages and in formal language theory to find effective methods
for defining programming language semantics. Several methods have been
proposed, but none of them has been as universally accepted and are as
successful for semantic definition as context-free languages have been for
syntax.
EXERCISES
1. Consult a book on C for formal defintions of the following constructs.
(a) literal
(b) for statement
(c) if-else statement
(d) do statement
(e) compound statement
(f) return statement
2. Find examples of features of C that cannot be described by context-free
grammars.
