CODE(assign.l):
%{
#include<stdio.h>
#include "y.tab.h"
#define YYSTYPE int //to avoid global var coz multiple instances
%}
%option reentrant bison-bridge
%%
[0-9]+ {
*yylval=atoi(yytext); //to refer to address of yylval
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap(yyscan_t yyscanner) //parser object in bracket
{
return 1;
}
CODE(assign.y):
%{
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#define YYSTYPE int
int numofthreads,expsperthread;
char inputfilename[30];
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%pure-parser
%lex-param {void * scanner}
%parse-param {void * scanner}
%%
ArithmeticExpression: E{
//printf("\nResult=%d\n",$$);
return $$;
}
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
void func()
{
void * scanner;
yylex_init(&scanner); /*parser object created ie scanner initialized and destroyed*/
FILE *fp;
fp=fopen(inputfilename,"r");
int i,j;
int tid;
int res;
tid=omp_get_thread_num();
char *s=malloc(100);
size_t n=100;
for(i=0;i<(expsperthread*tid);i++) //to skip some lines
{
getline(&s,&n,fp);
}
yyset_in(fp,scanner);
for(j=0;j<expsperthread;j++)
{
res=yyparse(scanner); //invoking parser which invokes lexer
printf("%dth Expression=%d is given by Thread %d\n",(expsperthread*tid+j+1),res,tid);
}
yylex_destroy(scanner);
fclose(fp);
}
void main()
{
printf("Enter Input Text File Name\n");
scanf("%s",inputfilename);
printf("Enter Number of threads to be created\n");
scanf("%d",&numofthreads);
printf("Enter Number of Arithmetic Expressions to be evaluated by each thread\n");
scanf("%d",&expsperthread);
#pragma omp parallel num_threads(numofthreads)
{
func();
}
}
int yyerror()
{
printf("\nArithmetic expression is Invalid\n\n");
}
CODE(file.txt):
2+3
3+4
4+5
5+6
1*2
2*3
3*4
4*5
5*6
2-1
3-2
4-3
5-4
6-5
7+5
5*7
34+790
120+210
12*23
1+2
OUTPUT:
[pl3@pl3]$ lex test.l
[pl3@pl3]$ yacc -d test.y
[pl3@pl3]$ ls
a.out lex.yy.c file.txt assign.l assign.l~ assign.y assign.y~ y.tab.c y.tab.h
[pl3@pl3]$ gcc -fopenmp lex.yy.c y.tab.c
[pl3@pl3]$ ./a.out
Enter Input Text File Name
test1.txt
Enter Number of threads to be created
5
Enter Number of Arithmetic Expressions to be evaluated by each thread
3
7th Expression=12 is given by Thread 2
13th Expression=1 is given by Thread 4
14th Expression=1 is given by Thread 4
15th Expression=12 is given by Thread 4
4th Expression=11 is given by Thread 1
8th Expression=20 is given by Thread 2
1th Expression=5 is given by Thread 0
9th Expression=30 is given by Thread 2
10th Expression=1 is given by Thread 3
2th Expression=7 is given by Thread 0
3th Expression=9 is given by Thread 0
5th Expression=2 is given by Thread 1
11th Expression=1 is given by Thread 3
6th Expression=6 is given by Thread 1
12th Expression=1 is given by Thread 3
[cnlab@localhost 3]$ ./a.out
Enter Input Text File Name
test1.txt
Enter Number of threads to be created
3
Enter Number of Arithmetic Expressions to be evaluated by each thread
3
7th Expression=12 is given by Thread 2
8th Expression=20 is given by Thread 2
1th Expression=5 is given by Thread 0
9th Expression=30 is given by Thread 2
4th Expression=11 is given by Thread 1
5th Expression=2 is given by Thread 1
6th Expression=6 is given by Thread 1
2th Expression=7 is given by Thread 0
3th Expression=9 is given by Thread 0