'C', is it's grammar context sensitive ?
Cedric Ramsey
ramsey at NCoast.ORG
Fri Aug 24 02:49:05 AEST 1990
Hello again ! This question is directed towards the 'C' and compiler
gurus out there. I was studying the grammar for the 'C' language and I couldn't
help but notice that for declarations the grammar is context sensitive.
Look at this here:
declaration: declaration-specifiers init-declarator-list_opt;
declaration-specifiers:
storage-class-specifier declaration-specifiers_opt
type-specifier declaration-specifiers_opt
type-qualifier declaration-specifiers_opt
type-specifier: one of
void char short int long float double signed unsigned
struct-or-union-specifier enum-specifier typedef-name
typedef-name: IDENTIFIER
Since the 'typedef-name' is an identifier is impossible to determine that
it is a type defintion without looking at the context. I guess that one could
do a pre-scan of the source code and build typedef trees but I thought that
'C' was context free grammar. If you try to build a parser using the above
grammar, which I did, a declaration tree for following grammar might derive:
sometype id;
declaration
/ \
declaration-specifiers init-declarator-list
/ \
type-specifier NULL
/ \
typdef-name type-specifier
| \
IDENTIFIER = 'sometype' typedef-name
|
IDENTIFIER = 'id'
Instead of:
declaration
/ \
declaration-specifiers init-declarator-list
/ / \
type-specifier init-declarator NULL
/ \ |
typdef-name NULL declarator
| / \
IDENTIFIER = 'sometype' pointer direct-declarator
| |
NULL IDENTIFIER = 'id'
Do you see point? I hope you do. Like I said before the solution to this
problem, I believe, is to do a pre-pass to collect typedef definitions or
just rearrange the grammar some-kind of adhoc way. I think that the prescan
idea is best best it keeps the grammar simple and easy to follow. What do
use guys think ?
More information about the Comp.lang.c
mailing list