You are on page 1of 2

%{

#include<stdio.h>
#include<math.h>
#define YYSTYPE double
YYSTYPE last_value=0;
int yyerror(const char *s);
extern int yylex(void);
%}
%token NUMBER
%token LAST
%left '+' '-' '*' '/' '^'

%%list:
| list '\n'
| list expr '\n' {printf("%.10g\n",last_value=$2);
}
;

expr: term {$$ = $1; }


| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| expr '^' expr {$$ = pow($1,$3);}
;term: NUMBER { $$ = $1; }
| LAST { $$ = last_value; }
| '(' expr ')' { $$ = $2; }

;
%%

#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int lineno;
char *fname = "-stdin-";
int yyerror(const char *s)
{
fprintf(stderr,"%s(%d):%s\n",fname,lineno,s);
return 0;
}
main()
{
yyparse();
return 0;
}

////////////****************************////////////////

%{
#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
#include "math.h"
#define YYSTYPE double
#include "y.tab.h"
extern int lineno; /* current line number */
extern YYSTYPE yylval; /* value of numeric token */
%}

digit [0-9]
space [ \t]

%%

{space} { ; } /* spaces are ignored */


{digit}+\.?|{digit}*\.{digit}+ {
yylval = strtod(yytext,0);
return NUMBER; }
\n { lineno++; return '\n'; }
. { return yytext[0]; }
%%

You might also like