Yacc probs w/ANSI-C
Robert Steven Glickstein
bobg+ at andrew.cmu.edu
Wed Jun 21 10:13:43 AEST 1989
I've written a parser for ANSI-C using YACC (adhering strictly to the
grammar given in K&R, second edition) that doesn't work. Despite the
claim in section A13 that "this grammar is acceptable to the YACC
parser-generator"(modulo some changes, which I've made), it in fact is
not; it's not even LR(1). I get syntax errors in which the parser can't
decide if it's seeing a function-definition or a declaration (while
working on an external-declaration). Here's the very beginning of the
grammar:
file ::= external-declaration | file external-declaration
external-declaration ::= function-definition | declaration
function-definition ::= opt-declaration-specifiers
declarator opt-declaration-list compound-statement
declaration ::= declaration-specifiers
opt-init-declarator-list ';'
Now consider the input
extern struct foo bar[];
Every token up to (and including) the ']' represents a legal sequence to
begin either a function-definition or a declaration. My parser chooses
to begin parsing this as a function-definition, and it barfs when it
sees a ';' instead of a compound-statement. On the other hand,
struct foo *bar() {}
is also legal for both function-definitions and declarations (up to the
')'), but the parser chooses to parse this as a declaration, and barfs
when it sees a '{' instead of a ';'.
Can any YACC gurus advise me on how to proceed? I will gladly mail you
a copy of my YACC source if you can take a look at it.
Thanks,
-Bob Glickstein
More information about the Comp.lang.c
mailing list