Article View: comp.lang.misc
Article #1053Dynamic conflict solving in Yacc/Bison
From: wb
Date: Thu, 10 Sep 1992 11:34
Date: Thu, 10 Sep 1992 11:34
106 lines
2384 bytes
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
Technical Details
Message-ID:
<1992Sep10.113422.25625@arb-phys.uni-dortmund.de>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!ifado!wbShow Full Headers
Complete Article Headers
Newsgroups: comp.lang.misc Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!ifado!wb From: wb@arb-phys.uni-dortmund.de () Subject: Dynamic conflict solving in Yacc/Bison Message-ID: <1992Sep10.113422.25625@arb-phys.uni-dortmund.de> Sender: wb@arb-phys.uni-dortmund.de (Wilhelm B. Kloke) Organization: Institut f. Arbeitsphysiologie a.d. Uni Dortmund Date: Thu, 10 Sep 92 11:34:22 GMT Lines: 106