[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Problems with untyped
From: |
John S O'Sullivan |
Subject: |
Problems with untyped |
Date: |
Thu, 4 Jul 2002 13:34:01 +0100 |
I would appreciate some trouble with a simple grammar I am trying to
develop. I have attached some sample code.
When I initially compiled this with support for integers it worked fine. I
added the capability to handle floats and ran into trouble. Bison complains
that the line
| IF expression '<' expression THEN statement { if ($2 < $4) $6;
}
has $6 untyped. I can't figure out what type $6 should be. It is a statement
so I assumed it did not need an associated type. I have read the Bison
manual , but find the brief references to typing unclear. I have also
trawled the mailing lists without success.
Grammar file
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-
%{
#include <stdio.h>
#include <string.h>
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
float vbltable[26];
%}
%token IF
%token ELSE
%token THEN
%token ENDIF
%union {
float dval;
int vblno;
}
%token <vblno> NAME
%token <dval> NUMBER
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS
%type <dval> expression
%%
statement_list: statement '\n'
| statement_list statement '\n'
;
statement: NAME '=' expression { vbltable[$1] = $3; }
| IF expression '<' expression THEN statement { if ($2 < $4)
$6; }
| expression { printf("= %f\n", $1); }
;
expression: expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression
{ if($3 == 0.0)
yyerror("divide by zero");
else
$$ = $1 / $3;
}
| '-' expression %prec UMINUS { $$ = -$2; }
| '(' expression ')' { $$ = $2; }
| NUMBER
| NAME { $$ = vbltable[$1]; }
;
%%
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-----------
Lex file
----------------------------------------------------------------------------
--------------------------------------------------
%{
#include "y.tab.h"
#include <math.h>
extern float vbltable[26];
%}
%%
"IF" { return IF; }
"THEN" { return THEN; }
"ELSE" { return ELSE; }
"ENDIF" { return ENDIF; }
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
yylval.dval = atof(yytext); return NUMBER;
}
[ \t] ; /* ignore white space */
[a-z] { yylval.vblno = yytext[0] - 'a'; return NAME; }
"$" { return 0; /* end of input */ }
\n |
. return yytext[0];
%%
- Problems with untyped,
John S O'Sullivan <=