Are yacc parsers portable ?

Jim Blandy blandy at marduk.cs.cornell.edu
Sun Jul 31 05:03:05 AEST 1988


In article <3950010 at eecs.nwu.edu> naim at eecs.nwu.edu (Naim Abdullah) writes:
>Is the C parser produced by yacc, portable ? I was looking at it and it had
>stuff like:
	...
>	short yys[YYMAXDEPTH];
	...
>	yyps= &yys[-1];

>It seems to me that negative indices on arrays are probably not portable.


Yacc doesn't actually dereference this pointer; it only uses it to
initialize the stack pointer for its inc-push, pop-decr stack.  yyps
will get used in a phrase like   *++yyps = uhlrich   to push uhlrich
onto the stack, and   zwingli = *yyps--   to pop zwingli off.

Portable?  ANSI says that that pointer is undefined, but I can't
imagine it ever causing any problems.  Unless there is an architecture
where one can decriment a pointer, increment it, and not get the
original value back (I'd be surprised, but not terribly surprised),
it's plenty portable, since yacc never dereferences it.

They could use a push-inc decr-pop stack, for which the equivalent
initialization would be

	yyps = yys;

which looks cleaner and <ahem> portabler.  But the advantage to using
their approach is that yyps points AT the top item, not at the top
free space; this makes it easier to look at the top of the stack
without popping, which yacc does quite often.

Since the advent of LALR, parsing is cheap enough that it doesn't take
up much of the compiler's time anyway; I'd bet this doesn't make much
of a difference...



More information about the Comp.lang.c mailing list