From: | Hans-Peter Diettrich <DrDiettrich1@aol.com> |
Newsgroups: | comp.compilers |
Date: | 21 Sep 2006 10:33:15 -0400 |
Organization: | Compilers Central |
References: | 06-09-02906-09-042 06-09-048 06-09-060 06-09-078 06-09-093 |
Keywords: | parse, C |
Posted-Date: | 21 Sep 2006 10:33:15 EDT |
Tommy Thorn wrote:
> to work, most people think that C is an LALR(1) language or simpler (*).
> (*) It's not. The lexer has to know which names are types, otherwise it
> becomes impossible to tell if the statement
> x * y;
> is a declation of a pointer to x or an expression multiplying x and y.
A more practical example, found in this group already:
a = (b)-c;
is (b) a type cast, or an expression?
Sorry, I don't remember who "invented" this fine example :-(
As a solution for this problem in my LL(1.5) C parser, I leave the
distinction to a stage between the lexer and parser, inside the
preprocessor. Only in this stage "words" are mapped into keywords,
typenames and other identifiers. The according "symbol" table is
initialized with the C keywords, the preprocessor adds to it all
#defined names, and the parser adds all encountered typedef names. This
procedure works fine, provided that typedefs always have global scope,
as is required in C. C++ makes it more complicated, with namespaces and
local typedefs :-(
DoDi
Return to the
comp.compilers page.
Search the
comp.compilers archives again.