[comp.lang.scheme.c] Need C language Description

cotner@bosco.berkeley.edu (07/29/88)

A friend of mine would like to write a small C compiler for a project.
(The rest of the class are writing Pascal compilers).  However, he needs
a formal language description of C, and so far neither one of us have been 
able to find one.

If any one out there in netland can suggest a reference we would both 
appreciate it.

10Q 
--carl
cotner@bosco.Berkeley.EDU


P.S. I usually don't read this newsgroup, so please e-mail

smryan@garth.UUCP (Steven Ryan) (07/30/88)

>                                                       However, he needs
>a formal language description of C, and so far neither one of us have been 
>able to find one.

You can get a language description out of K+R, but forget about a formal
definition unless you want to do it yourself.

jos@philapd.UUCP (Jos Vos) (08/01/88)

In article <12707@agate.BERKELEY.EDU> cotner@bosco.berkeley.edu writes:
>A friend of mine would like to write a small C compiler for a project.
>(The rest of the class are writing Pascal compilers).  However, he needs
>a formal language description of C, and so far neither one of us have been 
>able to find one.

In "C: A Reference Manual", written by Harbison & Steele, a LALR(1) grammar
is included in an appendix. You *should* be able to feed it to YACC, for
instance, but I have never tried it!

Of course (?), this is only a *syntactic* description, not a *semantic* one.

-- 
#  Jos Vos                             #  Internet  jos@philapd.UUCP  #
#  Philips TDS, Dept SSP               #                              #
#  P.O. Box 245                        #  UUCP  ..!mcvax!philapd!jos  #
#  7300 AE Apeldoorn, The Netherlands  #  Phone        +31 55 433181  #

markg@dbase.UUCP (NFS 3C501) (08/02/88)

In article <12707@agate.BERKELEY.EDU>, cotner@bosco.berkeley.edu writes:
> A friend of mine would like to write a small C compiler for a project.
> (The rest of the class are writing Pascal compilers).  However, he needs
> a formal language description of C, and so far neither one of us have been 
> able to find one.

A more complete description of the C language is the ANSI C Standard.  It is
currently available only in draft form.  I'm not sure where to get hold of it.
You might try ANSI (American National Standards Institute) located in New
York City somewhere or other on Broadway in Manhattan.  They do not distribute
draft versions of standards but could tell you who does distribute it.

ldg@druin.ATT.COM (Doug Gibbons) (08/03/88)

Attribute grammars are yet another type of formal language 
definition, combining context free syntax with context sensitive 
semantics. An MS thesis I did while at the University of Colorado 
described the syntax and static semantics of C using an attribute 
grammar written in Aladin. Since I was finishing just as X3J11 was 
starting, the grammar does not describe ANSIisms.

Below is a grammar excerpt describing a C shift expression. The CFG
production appears first, followed by the attribution and context
conditions which must hold true. With a little effort, you can get a feel
for what is going on here.  The "{in|out}" attributes are used to thread 
various symbol tables through the production. The interesting things
computed here are:

	SHIFT_EXP[1].at_type		the expression's type
	SHIFT_EXP[1].at_value		the expression's value
	SHIFT_EXP[1].at_eval_time	when value can be known
	SHIFT_EXP[1].at_const_exp	is it a constant expression?

-------------------------------------------------------------------------------


rule r_39 :
	SHIFT_EXP ::= SHIFT_EXP SHIFT_OP ADD_EXP 
static
%
% Do the UAC balancing on the operands. Both must be integral types.
% The type is that of the converted left operand. If the right operand
% is negative or greater than the size (in bits) of the type of the
% left after the UAC, then the result is undefined. 
%
	SHIFT_EXP[1].at_const_exp :=
		SHIFT_EXP[2].at_const_exp and ADD_EXP.at_const_exp;
	SHIFT_EXP[1].at_eval_time :=
		if f_is_defined(SHIFT_EXP[1].at_value) then
			sc_constant
		else
			sc_run_time
		fi;
	SHIFT_EXP[1].at_id_defs_out := ADD_EXP.at_id_defs_out;
	SHIFT_EXP[1].at_lval_class := sc_rval;
	SHIFT_EXP[1].at_tag_names_out := ADD_EXP.at_tag_names_out;
	SHIFT_EXP[1].at_tag_props_out := ADD_EXP.at_tag_props_out;
	SHIFT_EXP[1].at_type :=
			f_balance(SHIFT_EXP[2].at_type, ADD_EXP.at_type);
	SHIFT_EXP[1].at_value :=
		f_eval_binary (
			SHIFT_OP.at_op,
			SHIFT_EXP[2].at_value,
			ADD_EXP.at_value);
	SHIFT_EXP[2].at_id_defs_in := SHIFT_EXP[1].at_id_defs_in;
	SHIFT_EXP[2].at_tag_names_in := SHIFT_EXP[1].at_tag_names_in;
	SHIFT_EXP[2].at_tag_props_in := SHIFT_EXP[1].at_tag_props_in;
	ADD_EXP.at_id_defs_in := SHIFT_EXP[2].at_id_defs_out;
	ADD_EXP.at_tag_names_in := SHIFT_EXP[2].at_tag_names_out;
	ADD_EXP.at_tag_props_in := SHIFT_EXP[2].at_tag_props_out;
condition
	f_is_integral(SHIFT_EXP[2].at_type) and
	f_is_integral(ADD_EXP.at_type) 
message
	"error: shift requires integral operands";
condition
	not f_is_neg_i(SHIFT_EXP[1].at_value)
message
	"warning: result of negative shift is non-portable";
condition
	if f_is_defined(ADD_EXP.at_value) then
		f_is_gt_i (
			ADD_EXP.at_value,
			f_mach_val (
				f_mach_bit_sizeof(SHIFT_EXP[1].at_type),
				sc_uint))
	else
		true
	fi
message
	"warning: magnitude of right operand of shift causes undefined value";
		
end;
-------------------------------------------------------------------------------

-- Doug Gibbons			| ldg@druhi.ATT.COM or att!druhi!ldg
-- AT&T Bell Laboratories 
-- Denver CO

feldmark@hanako.stars.flab.Fujitsu.JUNET (08/04/88)

In article <458@ssp15.idca.tds.philips.nl> jos@philapd.UUCP writes:

> In "C: A Reference Manual", written by Harbison & Steele, a LALR(1) grammar
> is included in an appendix. You *should* be able to feed it to YACC, for
> instance, but I have never tried it!

I haven't looked at this book in particular, but if it is anything
like what appears in K&R, it is probably possible but requires
modifications.  A long time ago in a place far far away we had to
write a C compiler and were advised NOT to use what's in the back of
K&R because YACC couldn't process it correctly.  Most of us paid
little attention, made some modifications which I don' t remember, and
came out OK.

If you have the time to search for it and understand it, look for the
YACC source for  the UNIX C compiler. We didn't find out it was there
until we were almost finished, so it didn't help much, if you know
before you start, it might be useful.  :-)

Mark Feldman
Fujitsu Laboratories Ltd.
Artificial Intelligence Laboratory
feldmark@hanako.stars.flab.fujitsu.junet (Japan)
feldmark%hanako.stars.flab.fujitsu.junet@uunet.uu.net (USA)

--
feldmark@hanako.stars.flab.fujitsu.junet (Japan)
feldmark%hanako.stars.flab.fujitsu.junet@uunet.uu.net (USA)

bts@sas.UUCP (Brian T. Schellenberger) (08/06/88)

In article <1104@garth.UUCP> smryan@garth.UUCP (Steven Ryan) writes:
|>                                                       However, he needs
|>a formal language description of C, and so far neither one of us have been 
|>able to find one.
|
|You can get a language description out of K+R, but forget about a formal
|definition unless you want to do it yourself.

Well, H&S has a good formal BNF grammar (which I have used).  As for
semantics, I'd recommend ANSI.  It's close enough to finished to be
relavent, and it's the only formal (even if it is English-like) semantic
description I've ever seen of C.

H&S = "Harbison & Steele"  Available at local bookstores.
-- 
--Brian,                     __________________________________________________
  the man from              |Brian T. Schellenberger   ...!mcnc!rti!sas!bts
  Babble-On                 |104 Willoughby Lane     work: (919) 467-8000 x7783
____________________________|Cary, NC   27513        home: (919) 469-9389