Re: Is cast operator unary or binary? How many operands?



In article <f6c6e3fe-d076-43be-9cb8-4d29da011908@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
JoseMariaSola <JoseMariaSola@xxxxxxxxx> wrote:
So both following expressions have one operator and one operand?
sizeof(int)
(int)x

Yes.

typename is considered an operand in the firs expressin but not in the
second one?

Yes. (In the second one, the type name is part of the operator.)

This is just a matter of terminology, in this case, as applied to
parsing. Since the terminology is made up by humans, it need not
be entirely consistent, or even make all that much sense, as long
as it lets one human communicate with another. :-)

The way to view this, to make sense out of it, is that the cast
operator is inherently made up of the token sequence '(',
type-name-keyword [%], ')'. Having consumed the type-name by this
point, the only "argument" left to call an "operand" is the value
being cast. The sizeof operator is inherently made up of the
keyword token 'sizeof', and if its operand is a type-name, the
type-name is syntactically required to have outer-level parentheses.
Since the parentheses are (again) consumed by the syntax, the
"argument" that is left, that can be called an "operand", is the
type-name (without, in this case, the parentheses, although it
makes no real difference anyway).

[% In C as originally defined by Dennis Ritchie, "typedef" did not
even exist. So all type names were keywords, which made parsing
C easy. All declarations were instantly obvious, as they always
started with a keyword, and since the total set of keywords was
small, everyone knew all of them at a glance. When typedef was
later added, some time in the late 1970s, the language became much
more complicated syntactically, since typedef identifiers are only
distinguishable from other identifiers by context. For instance,
the C89 -- but not C99 -- code fragment:

void f(x);

can mean either:

- declare f as function taking one argument of type int, and
returning void; or

- declare f as function taking one argument of type x, and
returning void.

The former makes use of C89's "implicit int"; the latter occurs if
and only if "x" is a typedef-name. C99 removes this particular
ambiguity by outlawing "implicit int", but other difficult parsing
situations remain. From the parser's point of view, typedef-ed
identifiers are "just like" keywords, except when they are not.
If one attempts to use a conventional scanner and parser generator
like (f)lex and (b)yacc, one must invent clever feedback and/or
feed-forward mechanisms by which the parser can tell the lexer
whether to claim a potential typedef name is a "type name" or an
"identifier name". (Or one could use a more powerful parsing
technique, but that means abandoning yacc.)

Because it is impossible to tell at a glance whether some identifier
is actually a typedef-name, C programmers often find themselves
using some sort of self-imposed (or company coding standard imposed)
typographic convention to "mark" all typedefs. For instance, some
people use _t suffixes, so that it is clear that greeble_t is a
type-name. Others use an initial capital letter, or all-uppercase.]
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
.



Relevant Pages

  • Re: problems with ANSI C grammar for yacc
    ... >So essentialy what I need to do, is to control which namespace I am ... >5: for identifiers of variables. ... avoid using "typedef", spelling out the word "struct" instead. ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)
  • Re: Testing for a type
    ... until after the preprocessor has finished. ... and `typedef' are merely source tokens to the preprocessor; ... aren't even recognized as keywords until later in compilation. ... the same header that declares the typeof interest: ...
    (comp.lang.c)
  • Re: regarding size_t
    ... size_t is a typedef for some unsigned integer type meant to be ... anything defined in any standard headers. ... Each header declares or defines all identifiers listed ...
    (comp.lang.c)
  • Re: regarding size_t
    ... size_t is a typedef for some unsigned integer type meant to be ... anything defined in any standard headers. ... Reserved identifiers ... so none of the cases that say identifiers are ...
    (comp.lang.c)
  • Re: calling a subroutine with optional arguments
    ... "New context-dependent keywords are introduced into a program by typedef, ... especially as reserved word languages ... but "argument designators" are not. ...
    (comp.lang.fortran)