From: | Hans-Peter Diettrich <DrDiettrich1@aol.com> |
Newsgroups: | comp.compilers |
Date: | 16 Dec 2006 15:18:05 -0500 |
Organization: | Compilers Central |
References: | 06-09-02906-09-042 06-09-048 06-09-060 06-09-078 06-09-093 |
Keywords: | parse, C, C++ |
Posted-Date: | 16 Dec 2006 15:18:05 EST |
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.