Professional Documents
Culture Documents
M. Ahmed Raza 20021519-050 Hafiz M. Nouman 20021519-111 Talha Fayaz 20021519-112
M. Ahmed Raza 20021519-050 Hafiz M. Nouman 20021519-111 Talha Fayaz 20021519-112
• Contains extra C code and typically includes the program's entry point.
Additional Code Section: • Placed at the end of the Yacc file.
%{#include <stdio.h>
#include <math.h>%}
%union {
double dvalue;}
%token <dvalue> NUMBER
%token PLUS TIMES LPAREN RPAREN
%left PLUS MINUS
%start calc
%%calc: expression { printf("Result: %f\n", $1); } ;
expression: expression PLUS expression { $$ = $1 + $3; }
| expression MINUS expression { $$ = $1 - $3; }
| LPAREN expression RPAREN { $$ = $2; }
| NUMBER { $$ = $1; } ;%%
int yylex();
int yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
return 1;}
int main() { yyparse();
return 0;}