Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 42620103d8ee8a2d972d3103bad0ab73 > files > 16

waf-1.5.19-1.fc14.noarch.rpm

%{
#define YYSTYPE double
#include <stdio.h>
// yylex() is generated by flex 
int yylex(void);
// we have to define yyerror()
int yyerror (char const *);

%}
%token NUMBER

%left '+' '-'
%left '*' '/'
%right '('

%%
stmtlist: statement '\n' stmtlist {
	printf("done with statement equal to [%g]\n", $1);
	} | // EMPTY RULE i.e. stmtlist -> nil
	{ printf("DONE\n") ;};

statement:	expression { printf("VALUE=%g\n",$1); };

expression:	expression '+' expression { $$ = $1 + $3; } |
		expression '-' expression { $$ = $1 - $3; } |
		expression '*' expression { $$ = $1 * $3; } |
		expression '/' expression { 
			if($3 !=0) { $$ = $1 / $3; } else 
			{ printf("div by zero\n"); $$=0;} } |
		'(' expression ')' { $$ = $2; } |
		NUMBER { $$ = $1; } ;

%%