[comp.lang.c] Assorted C Questions

gdtltr@vax1.acs.udel.EDU (Gary D Duzan) (09/03/89)

   In may spare time I am starting work on a fairly minimal C compiler for a
fairly minimal system. No flames, please, I am doing this for experience and
fun (not to mention the fact that this system has no real C compiler
available). I have just about finished my preprocessor/tokenizer and am
putting it through some tests. My first question is: how necessary are #if
directives? It seems to me that it adds too much complexity to the
preprocessor. I may add it later, but I think it is a bit too much trouble
at this stage.
   Now for a more important question about macros. Is this legal?

#define min(x,y)	(((x) < (y)) ? (x) : (y))

foobar(min,max)

Sometype min,max;

{}

   Will this foul up the preprocessor since min doesn't have the parameter
list in the function declaration?
   Also, what exactly is a #pragma? An example would help. I am working from
K&R Second Edition and it doesn't say much on the subject.

					Gary Duzan
					Time  Lord
				    Third Regeneration




-- 
      _o_                                                            _o_
    [|o o|]        "Two hearts are better than one." -- Yes        [|o o|]
     |_O_|      "Don't listen to me; I never do." -- Doctor Who     |_O_|

flaps@dgp.toronto.edu (Alan J Rosenthal) (09/04/89)

gdtltr@vax1.acs.udel.EDU (Gary D Duzan) writes:
>how necessary are #if directives?  It seems to me that it adds too much
>complexity to the preprocessor.

Indeed it does add a lot of complexity to the preprocessor.  I think that's why
most people no longer separate the preprocessor and the parser so much these
days.

I don't think expression handling in the preprocessor is SO important.  I
currently use a C implementation lacking it, and I don't really miss it much.
What I do miss is that I also can't say "#if THING" where THING is previously
#defined to be zero or non-zero.  Do put that in.

Of course, your implementation is not really C without expression handling in
the preprocessor.  You'll probably want to add it some day.

>Also, what exactly is a #pragma?  An example would help.

Examples:    #pragma listing
	     #pragma used-heavily(i, j, r)

If you just ignore all #pragma lines you'll be fine.  If you just abort at any
#pragma line, well, people differ on whether or not that's ok.

ajr

henry@utzoo.uucp (Henry Spencer) (09/06/89)

In article <4294@udccvax1.acs.udel.EDU> gdtltr@vax1.acs.udel.EDU (Gary D Duzan) writes:
>... how necessary are #if
>directives? It seems to me that it adds too much complexity...

They are a pain to implement, indeed; there are just enough differences
between #if expressions and normal ones to make trouble for an attempt to
use the same code.  However, I think you will find that the utility of
your compiler is *vastly* increased if it will compile most existing C
programs.  This means you have to implement pretty well all of the language.

>#define min(x,y)	(((x) < (y)) ? (x) : (y))
>foobar(min,max)
>Sometype min,max;
>{}
>
>   Will this foul up the preprocessor...

No; parameterized macro names are recognized as such *only* when followed
immediately by "(".

>   Also, what exactly is a #pragma? ...

It has a totally implementation-defined effect.  It's up to *you*.  If you
just ignore them completely, that would be fine.
-- 
V7 /bin/mail source: 554 lines.|     Henry Spencer at U of Toronto Zoology
1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (09/07/89)

In article <1989Sep6.160004.19253@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:

|  >#define min(x,y)	(((x) < (y)) ? (x) : (y))
|  >foobar(min,max)
|  >Sometype min,max;
|  >{}
|  >
|  >   Will this foul up the preprocessor...
|  
|  No; parameterized macro names are recognized as such *only* when followed
|  immediately by "(".

  My standard isn't handy, but all of the preprocessors I use allow
whitespace between the name and the ( because the local indent does it
that way.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/07/89)

In article <283@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes:
-In article <1989Sep6.160004.19253@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:
-|  >#define min(x,y)	(((x) < (y)) ? (x) : (y))
-|  >foobar(min,max)
-|  >Sometype min,max;
-|  >   Will this foul up the preprocessor...
-|  No; parameterized macro names are recognized as such *only* when followed
-|  immediately by "(".
-  My standard isn't handy, but all of the preprocessors I use allow
-whitespace between the name and the ( ...

True but not relevant here.  "followed immediately" should be read as
talking about tokens, i.e. whitespace is assumed to be merely separating
tokens.  The important thing is that the token after the use of min is
a , and the token after the use of max is a ) or ;.

henry@utzoo.uucp (Henry Spencer) (09/07/89)

In article <283@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes:
>|  No; parameterized macro names are recognized as such *only* when followed
>|  immediately by "(".
>
>  My standard isn't handy, but all of the preprocessors I use allow
>whitespace between the name and the ( because the local indent does it
>that way.

My mistake:  in the *definition* there must be no space there, but it's
legal in invocation.  (And the basic point remains:  the name is recognized
only when there is a '(' after it.)
-- 
V7 /bin/mail source: 554 lines.|     Henry Spencer at U of Toronto Zoology
1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (09/09/89)

In article <1989Sep7.162354.20722@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>(And the basic point remains: the [function-like macro] name is recognized
>only when there is a '(' after it.)

It should be noted that this feature is new to ANSI C, and should not be
exploited by programs that need to be portable to pre-ANSI environments.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/09/89)

In article <14556@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes:
-In article <1989Sep7.162354.20722@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
->(And the basic point remains: the [function-like macro] name is recognized
->only when there is a '(' after it.)
-It should be noted that this feature is new to ANSI C, and should not be
-exploited by programs that need to be portable to pre-ANSI environments.

It's more the acceptance of the un-() occurrence as a valid literal token
that is new.  PCC used to say "argument mismatch" when it encountered such
an instance, for eaxmple.