C expression syntax in other languages?

Robert Firth firth at sei.cmu.edu
Wed Dec 17 23:58:10 AEST 1986


In article <3971 at watmath.UUCP> rbutterworth at watmath.UUCP (Ray Butterworth) writes:
>> One of the reasons I enjoy programming in C so much is its
>> expression syntax.  Are there any other languages that have
>> a similar expression syntax?  Do BCPL and B have it ?
>
>B has a very similar syntax.  If written carefully. some simple
>programs will compile and run correctly under either language.
>
...

Thanks, Ray; I found that a useful summary of B.  In return, I'll
submit a page on BCPL.  BCPL is the ancestor of B.  It is typeless,
admitting only one physical data type, basically the contents of
a machine word. There are various operations on this, eg

arithmetic: + - * / REM
comparison: = ~= > < >= <=
logical: ~ /\ \/ EQV NEQV << >>
indirection: !
application: ()

The ! operator is like C's "*", it defererences; the () calls the
preceding expression as a function.

The operator "@" takes the BCPL address of any variable; this has the
property that the addresses of adjacent "words" differ by 1. On byte
addressed machines, we have to scale the machine address to get the
"word" address.

The syntax is much closer to Algol than that of B or C:

	IF condition THEN statement ELSE statement
	FOR variable = expression TO expression BY constant DO statement

and so on.  The assignment symbol is ":=".  BCPL has the McCarthy
conditional (C's "?") in a form illustrated by

	q := p=NIL -> NIL, p!next

In addition, the logical and ("/\" or "&") and or ("\/" or "|") are
automatically short-circuit in control expressions

	IF p=NIL \/ p!next = NIL THEN ...

avoiding the need for "&&".  The language is not an expression language;
a statement delivers no value.  However, the construct

	VALOF { block }

is an expression; within the enclosed block the statement

	RESULTIS expression

terminates execution and exits the block.

Here's a piece of BCPL:

	|| Lookup ( v : ARRAY OF Item, x : Item ) : Index
	||
	|| Find the index of X in V and return it.  If X does not 
	|| occur in V, return 0.  V!0 is the length of the vector

	LET lookup(v,x) = VALOF
	{
	  FOR i=1 TO v!0 DO IF v!i=x RESULTIS i
	  RESULTIS 0
	}

Note that the keywords DO and THEN can be elided before another
keyword.

I find BCPL very easy to use, very readable (unlike, dare I say
it, C!) and sufficiently powerful for most systems programming
tasks.  It is also small enough to be readily implementable
(the portable front end is about 1200 lines code and a typical
back end is a bit less), and has a very precise defining
document.

Thank you for your attention.



More information about the Comp.lang.c mailing list