🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by wb Thu, 10 Sep 1992 11:34
Dynamic conflict solving in Yacc/Bison
#1053
Author: wb
Date: Thu, 10 Sep 1992 11:34
106 lines
2384 bytes
In an attempt to parse a language which needs dynamic priority
assignment (Algol68), I am trying the following hack. Does anybody know
a less risky method (the Bison manual warns about using $0)?

%{
#include	<stdio.h>
#include	<ctype.h>
%}

%start list
%token	DIGIT OP REDUCE
%right OP

%%

list:	/* empty */ 
	|	list stat '\n'
	|	list error '\n'
		{	yyerrok;	}
	;

stat:	{ $$='?'; } expr
		{	printf( "%d\n", $2 );	}
	;

expr:	expr OP expr
		{	$$ = binop($2, $1, $3);
			if( yychar==REDUCE ) yychar=OP;
			check( prio($0) );
		}
	|	number
		{
			check( prio($0) );
			$$ = $1;
		}
	;

number:	DIGIT
		{	$$ = $1;	}
	| number DIGIT
		{	$$ = 10 * $1 + $2;	}
	;

%%

check(p)  int p; {
	if( yychar == OP ) {
		if( prio( yylval) <= p ) yychar = REDUCE;
	}
}

prio(o) int o; {
	switch(o) {
	case '?' :  return 0;
	case '+' : return 5;
	case '-' : return 5;
	case '*' : return 6;
	case '&' : return 4;
	case '%' : return 6;
	case '/' : return 6;
	case '|' : return 4;
	default: printf( "error\n" );
	}
}
binop(o,x,y) int o,x,y; {
	switch(o) {
	case '+' : return x+y;
	case '-' : return x-y;
	case '*' : return x*y;
	case '&' : return x&y;
	case '%' : return x%y;
	case '/' : return x/y;
	case '|' : return x|y;
	default: printf( "error\n" );
	}
}

yylex() {
	int	c;
	while( (c=getchar()) == ' ' )
		;
	yylval = c;
	if( isdigit(c) ) {
		yylval = c - '0';
		return( DIGIT );
	}
	else if( c == '+' )	return( OP );
	else if( c == '-' )	return( OP );
	else if( c == '*' )	return( OP );
	else if( c == '&' )	return( OP );
	else if( c == '%' )	return( OP );
	else if( c == '|' )	return( OP );
	else if( c == '/' )	return( OP );
	return( c );
}

main()
{	return( yyparse() );	}

yyerror(s) char *s; {
	fprintf( stderr, "%s\n", s ); }
-- 
-- 
Dipl.-Math. Wilhelm Bernhard Kloke,
Institut fuer Arbeitsphysiologie an der Universitaet Dortmund
Ardeystrasse 67, D-4600 Dortmund 1, Tel. 0231-1084-257
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads