bin/189101: In FreeBSD yacc, %nonassoc doesn't work correctly

Makoto Kishimoto ksmakoto at dd.iij4u.or.jp
Tue Apr 29 09:10:00 UTC 2014


>Number:         189101
>Category:       bin
>Synopsis:       In FreeBSD yacc, %nonassoc doesn't work correctly
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Apr 29 09:10:00 UTC 2014
>Closed-Date:
>Last-Modified:
>Originator:     Makoto Kishimoto
>Release:        10.0-RELEASE-p1
>Organization:
privately
>Environment:
FreeBSD norikura.localdomain 10.0-RELEASE-p1 FreeBSD 10.0-RELEASE-p1 #0: Tue Apr  8 06:45:06 UTC 2014     root at amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
In yacc, an operator which specified %nonassoc should has non associativity,
and cause syntax error for runtime ambiguous input.

But FreeBSD's /usr/bin/yacc doesn't.
>How-To-Repeat:
( see attached sample )
$ echo -n "1-2-3" | ./calc_by  # FreeBSD BYacc
-4
$ echo -n "1-2-3" | ./calc_9y  # devel/9base's Yacc
syntax error
$ echo -n "1-2-3" | ./calc_bi  # GNU Bison
syntax error
>Fix:


Patch attached with submission follows:

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Makefile
#	calc.y
#	run_samples.sh
#
echo x - Makefile
sed 's/^X//' >Makefile << ''
XBYACC=/usr/bin/yacc
XBISON=/usr/local/bin/bison
XYACC9=/usr/local/9/bin/yacc
X
Xall: calc_by calc_bi calc_9y
X
Xcalc_by.c: calc.y
X	${BYACC} -o calc_by.c calc.y
X
Xcalc_bi.c: calc.y
X	${BISON} -o calc_bi.c calc.y
X
Xcalc_9y.c: calc.y
X	${YACC9} -o calc_9y.c calc.y
X
Xclean:
X	rm -Rf calc_by* calc_bi* calc_9y*

echo x - calc.y
sed 's/^X//' >calc.y << ''
X%{
X#include <ctype.h>
X#include <stdio.h>
X#include <stdlib.h>
X
Xint yyparse(void);
Xstatic int yylex(void);
Xstatic void yyerror(char *s);
X%}
X
X%token NUM
X
X%nonassoc '-'
X
X%%
X
Xprog	: expr { printf("%d\n", $1); }
X
Xexpr	: expr '-' expr { $$ = $1 - $3; }
X	| NUM
X	;
X
X%%
X
Xstatic int
Xyylex(void)
X{
X	static int lastchar = -2;
X
X	if (lastchar == -2) {
X		lastchar = getchar();
X	}
X
X	if (lastchar == EOF) {
X		return 0;
X	} else if (isdigit(lastchar)) {
X		int tmp = digittoint(lastchar);
X		while (isdigit(lastchar = getchar())) {
X			tmp = 10 * tmp + digittoint(lastchar);
X		}
X		yylval = tmp;
X		return NUM;
X	} else {
X		yylval = lastchar;
X		lastchar = getchar();
X		return yylval;
X	}
X}
X
Xstatic void
Xyyerror(char *s)
X{
X    fprintf(stderr, "%s\n", s);
X}
X
Xint
Xmain(void)
X{
X    yyparse();
X
X    return 0;
X}

echo x - run_samples.sh
sed 's/^X//' >run_samples.sh << ''
X#! /bin/sh
Xmake
Xecho -n "1-2-3" | ./calc_by
Xecho -n "1-2-3" | ./calc_9y
Xecho -n "1-2-3" | ./calc_bi

exit



>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list