LWN.net Logo

Why learn C? (O'Reilly Radar)

Why learn C? (O'Reilly Radar)

Posted Jul 1, 2012 22:30 UTC (Sun) by tjc (subscriber, #137)
In reply to: Why learn C? (O'Reilly Radar) by HelloWorld
Parent article: Why learn C? (O'Reilly Radar)

I can't think of a situation in C where an expression that begins with a type specifier is anything other than a declaration. I could be overlooking something, but I'm looking at the grammar in appendix A13 of K&R, and I don't see anything.


(Log in to post comments)

Why learn C? (O'Reilly Radar)

Posted Jul 1, 2012 22:49 UTC (Sun) by HelloWorld (guest, #56129) [Link]

> I can't think of a situation in C where an expression that begins with a type specifier is anything other than a declaration.
Uh, so? In a declaration like foo *bar, you still have to know whether foo is a typedef or a variable name in order to know whether it's a multiplication or a declaration.

Why learn C? (O'Reilly Radar)

Posted Jul 1, 2012 23:02 UTC (Sun) by tjc (subscriber, #137) [Link]

The compiler has this information available to it in the symbol table.

Why learn C? (O'Reilly Radar)

Posted Jul 1, 2012 23:20 UTC (Sun) by HelloWorld (guest, #56129) [Link]

I know, this is the well-known lexer hack. I still don't see how the syntax I've proposed is supposed to be any more problematic than conventional C syntax.

Why learn C? (O'Reilly Radar)

Posted Jul 2, 2012 20:36 UTC (Mon) by tjc (subscriber, #137) [Link]

The type specifier in a C declaration acts as a surrogate for the missing 'var' or 'func' keyword, which makes it possible for the parser to recognize the declaration without requesting a lot of extra tokens from the lexer.

Why learn C? (O'Reilly Radar)

Posted Jul 2, 2012 21:23 UTC (Mon) by HelloWorld (guest, #56129) [Link]

I think that this is only a problem when using top-down parsers. A bottom-up parser should handle this situation easily.

Why learn C? (O'Reilly Radar)

Posted Jul 3, 2012 15:46 UTC (Tue) by tjc (subscriber, #137) [Link]

Yes, it could be made to work, but it would have to be a backtracking parser, and it would probably be slow. Most bottom-up parsers (such as those produced by bison) are LALR(1), which only has one token look-ahead.

Why learn C? (O'Reilly Radar)

Posted Jul 1, 2012 22:59 UTC (Sun) by tjc (subscriber, #137) [Link]

I wish there were some way to edit posts after posting so that my mistakes are not permanently recorded for posterity. :)

I should have said, "I can't think of a situation in C where a statement that begins with a type specifier...."

Why learn C? (O'Reilly Radar)

Posted Jul 2, 2012 9:30 UTC (Mon) by nix (subscriber, #2304) [Link]

Quite, thus ruling out sizeof().

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds