This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: typedef name question


I am not sure whether following case is a gcc bug or not :


typedef int foo;


foo foo_fct(int a) {

foo foo; /* line 3: a variable foo with type foo */

   return (foo)foo;        /* line 4: gcc reports syntax error */
}


If gcc could , somehow, figure out the line 3 is a variable declaration, maybe it should be able to handle line 4 , which is a cast expression.



Regards,


Mike


----- Original Message ----- From: "Ian Lance Taylor" <iant@google.com>
To: "Michael Gong" <mwgong@cs.toronto.edu>
Cc: <gcc-help@gcc.gnu.org>
Sent: Tuesday, January 23, 2007 10:28 AM
Subject: Re: typedef name question



"Michael Gong" <mwgong@cs.utoronto.ca> writes:

To clarify myself a bit, is my following understanding correct ?

For this case:
int main() {
int foo;
foo abc;
}
The Gcc's grammar does parse it, ie. an "identifier" can follow
another "identifier".
Then in the semantic analysis, gcc checks to make sure the first
identifier "foo" must represent a type. In this case, it is not. So an
error is reported.

I think you may be applying compiler theory to a real world compiler.


A better way to describe what happens would be to say that the lexer
looks up each identifier in the symbol table to see whether it is a
type before the parser proper ever sees it.

A truer way to describe what happens would be to observe that gcc uses
a recursive descent parser, not a grammar.

Ian



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]