You are on page 1of 2

LAB6

%{
/*** Auxiliary declarations section ***/

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

/* Custom function to print an operator*/


void print_operator(char op);

/* Variable to keep track of the position of the number in the input */


int pos=0;

%}

/*** YACC Declarations section ***/


%token DIGIT
%left '+'
%left '*'
%%

/*** Rules Section ***/


S : E '\n' {printf("Accepted\n");}
;

E: E '+' T {printf("E->E+T\n");}
|T {printf("E->T\n");}
;
T: T '*' F {printf("T->T*F\n");}
|F {printf("T->F\n");}
;
F: '(' E ')' {printf("F->(F)\n");}
| DIGIT {printf("F->num\n");}
;

%%

/*** Auxiliary functions section ***/

void print_operator(char c){


switch(c){
case '+' : printf("PLUS ");
break;
case '*' : printf("MUL ");
break;
}
return;
}
LAB6

yyerror(char const *s)


{
printf("yyerror %s",s);
}

yylex(){
char c;
c = getchar();
if(isdigit(c)){
pos++;
return
DIGIT;
}
else if(c == ' '){
yylex(); /*This is to ignore whitespaces in the input*/
}
else {
return c;
}
}

main()
{
yyparse();
return 1;
}

You might also like