C code
% {
# Define YYSTYPE double
# Include <math. h>
Int yylex (void );
Void yyerror (char const *);
%}
% Token NUM
%
Input:
| Input line
;
Line: '\ N'
| Exp '\ n' {printf ("\ t %. 10g \ n", $1 );}
;
Exp: NUM {$ = $1 ;}
| Exp '+' {$ = $1 + $2 ;}
| Exp '-' {$ = $1-$2 ;}
| Exp '*' {$ = $1*$2 ;}
| Exp '/' {$ = $1/$2 ;}
/* Unary minus */
| Exp 'n' {$ =-$1 ;}
;
%
# Include <stdio. h>
Int yylex (void)
{
Int c;
/* Skip white space .*/
While (c = getchar () = ''| c = '\ t ')
;
/* Process numbers .*/
If (c = '.' | isdigit (c ))
{
Ungetc (c, stdin );
Scanf ("% lf", & yylval );
Return NUM;
}
/* Return end-of-input .*/
If (c = EOF)
Return 0;
/* Return a single char .*/
Return c;
}
Void yyerror (char const * error)
{
}
Int main ()
{
Return yyparse ();
}
Install Bison2.5 in Ubuntu linux and run:
Bison first. y
Gcc-o first. tab. c
Run
./Fisrt
1 4 +
5
3 10 *
30
OK.