Re: Annotating a symbol table in a C compiler

henry@zoo.toronto.edu (Henry Spencer)
Thu, 30 Apr 1992 15:35:08 GMT

          From comp.compilers

Related articles
Re: Annotating a symbol table in a C compiler henry@zoo.toronto.edu (1992-04-30)
| List of all articles for this month |

Newsgroups: comp.compilers
From: henry@zoo.toronto.edu (Henry Spencer)
Keywords: C, parse
Organization: U of Toronto Zoology
References: 92-04-162
Date: Thu, 30 Apr 1992 15:35:08 GMT

pkeddie@axion.bt.co.uk (Paul Keddie) writes:
>During a parse of a C program when are objects placed into the symbol
>table? Surely not as soon as an object has been seen. For example, if
>parsing has reached:
> typedef int XXX;
> XXX .............
>then you can't yet tell whether XXX is being used as typedef-name, or
>whether a (possibly new) goto label is being parsed (if the next token is
>':').


There are two separate issues here. One is when declarations take effect.
That is defined by the C standard. In this case, the declaration of XXX
as a type name takes effect at the end of the declarator, just before
that ";".


The other is the problem of parsing a language with multiple name spaces
and some truly painful syntax. The fact is, you *cannot tell* whether that
second XXX is a label without doing a bit of extra lookahead in search of
the ":". Labels live in their own separate name space; it is perfectly
legal to use XXX as both a type name and a label. Ugh.


>I guess that objects are placed into the table when enough context is
>known. But in a function definition like:
> int func( char fred);
>this would mean 'fred' is placed in the table ahead of 'func'. So how do
>you record that 'fred' is a parameter of 'func'?


Here `fred' is in a name space local to the declaration; it does *not* go
into the general symbol table. Assuming you mean to type


int func(char fred) {


then the answer is that you just have to keep track somehow. You'll have
to keep local variables (which is what parameters mostly are) distinct
anyway, since `fred' must vanish from the symbol table at the end of the
function definition. (Its type may persist as part of information about
`func', but its name vanishes.)
--
Henry Spencer @ U of Toronto Zoology, henry@zoo.toronto.edu utzoo!henry
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.