Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Labels |
| Date | 2015-08-15 14:49 -0400 |
| Organization | Self |
| Message-ID | <55CF89BD.5050703@verizon.net> (permalink) |
| References | <21f52d2c-4f30-4ae1-9f0a-c1c82527b884@googlegroups.com> |
On 08/15/2015 12:46 PM, David Kleinecke wrote:
> One of the curious little problems encountered in making a C compiler
> is how to recognize labels.
>
> There is no problem in a two-pass compiler - but let me assume a one
> pass compiler. Then I am reading along compiling statements and I
> start a new statement and the first token in the statement is an
> identifier. Is this token a label?
>
> The obvious way to tell is to read another token and check whether
> the new token is a colon. If it is a colon then this is indeed a
> label. But if the new token is not a colon then I am two tokens deep
> into an expression. This is not the expected situation. The expected
> situation is that only the first token has been read.
I'm not sure what you mean by that - but if "the expected situation" is
different from what's actually possible, then that argues for a change
to your expectations.
If you parse an identifier in a context where the start of a labeled
statement is permitted, than it must be either
a) a declaration making use of a previously defined typedef
b) an expression statement starting with the previously declared name of
an object or a function.
c) a labeled statement.
If the identifier has not been declared in this scope as either a
typedef, object, or function name, it must be a label name. Therefore,
if such an identifier is not followed by a ':', it's syntax error.
If the identifier has been declared in this scope as one of those
things, it could still also be a label. That's because label names have
their own name space (6.2.3). Therefore, it is indeed necessary to read
the ':' to identify it as a label name rather than a declaration or an
expression.
However, is that really a big problem? In precisely the same context, if
the first token is a '(', you can be sure that you're in the expression
of an expression-statement, but you have to read at least one additional
token to determine whether what you're looking at is a parenthesized
expression, or a cast-expression. It seems to me from my previous
efforts in this area that it's pretty normal to have ambiguities that
require the reading of two or more tokens before they are cleared up.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-15 09:46 -0700
Re: Labels James Kuyper <jameskuyper@verizon.net> - 2015-08-15 14:49 -0400
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-15 18:48 -0700
Re: Labels Bartc <bc@freeuk.com> - 2015-08-15 18:27 +0100
Re: Labels Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-15 23:44 +0100
Re: Labels James Kuyper <jameskuyper@verizon.net> - 2015-08-15 19:04 -0400
Re: Labels Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-16 00:47 +0100
Re: Labels James Kuyper <jameskuyper@verizon.net> - 2015-08-15 20:20 -0400
Re: Labels Bartc <bc@freeuk.com> - 2015-08-16 01:56 +0100
Re: Labels Keith Thompson <kst-u@mib.org> - 2015-08-15 19:47 -0700
Re: Labels James Kuyper <jameskuyper@verizon.net> - 2015-08-16 11:59 -0400
Re: Labels Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-16 22:42 +0100
Re: Labels James Kuyper <jameskuyper@verizon.net> - 2015-08-16 20:39 -0400
Re: Labels Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-17 03:16 +0100
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-15 19:01 -0700
Re: Labels Bartc <bc@freeuk.com> - 2015-08-16 01:11 +0100
Re: Labels Keith Thompson <kst-u@mib.org> - 2015-08-15 19:13 -0700
Re: Labels Bartc <bc@freeuk.com> - 2015-08-16 10:34 +0100
Re: Labels Keith Thompson <kst-u@mib.org> - 2015-08-16 13:58 -0700
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-16 20:42 -0700
Re: Labels Keith Thompson <kst-u@mib.org> - 2015-08-16 21:24 -0700
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-17 18:01 -0700
Re: Labels Bartc <bc@freeuk.com> - 2015-08-17 11:04 +0100
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-15 19:04 -0700
Re: Labels Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-16 23:55 +0100
Re: Labels David Kleinecke <dkleinecke@gmail.com> - 2015-08-15 19:00 -0700
csiph-web