Friday, November 13, 2015

Write a concurrent Lex-Yacc Program to solve multiple expressions from an input file.

Program

c1.l

%{
#include "y.tab.h"
#include<stdlib.h>
%}
%option reentrant bison-bridge
%%
[ \t] { ; }
[0-9]+ { *yylval=atoi(&yytext[0]); return NUMBER; }

[-+*/\n] { return *yytext; }
%%

int yywrap(void *scanner){

    return 1;
}

c1.y

%{
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
    FILE *yy[4];
%}
%pure-parser
%parse-param {void *scanner}
%lex-param {void *scanner}

%token NUMBER
%left '+' '-'
%left '*' '/'
%%
list:   
    | list '\n'
    | list expr '\n'  {printf("I am thread %d and result is:%d\n",omp_get_thread_num(),$2);cnt++;}
    ;
expr:   NUMBER        { $$ = $1; }
    |   expr '+' expr {$$ = $1+$3;}
    |   expr '-' expr {$$ = $1-$3;}
    |   expr '*' expr {$$ = $1*$3;}
    |   expr '/' expr {$$ = $1/$3;}
    ;
%%

int main(void)
{
   
int i,j;
char *ch=malloc(sizeof(char)*4);
int num;
    for(i=1;i<=3;i++)
    {
yy[i]=fopen("/home/student/3236/my/yacc_sir/f1.txt","r");
for(j=0;j<i*5;j++)
{

getline(&ch,&num,yy[i]);


}

   }
    
    void *scanner;
   #pragma omp parallel private(scanner)
   {
int myid=omp_get_thread_num();
int pos,j;

//printf("myid is %d",myid);
 
  yylex_init(&scanner);
  yyset_in(yy[myid],scanner);
yyparse(scanner);
printf("Output of thread %d",myid);
         yylex_destroy(scanner);
    fclose(yy[myid]);
}

    return 0;
}
int yyerror()
{
printf("Something wrong");
}

f1.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

I am thread 3 and result is:35
I am thread 3 and result is:824
I am thread 3 and result is:330
I am thread 3 and result is:276
I am thread 3 and result is:3
I am thread 0 and result is:5
Output of thread 3I am thread 2 and result is:1
I am thread 2 and result is:1
I am thread 1 and result is:6
I am thread 2 and result is:1
I am thread 0 and result is:7
I am thread 1 and result is:12
I am thread 2 and result is:1
I am thread 1 and result is:20
I am thread 1 and result is:30
I am thread 1 and result is:1
I am thread 2 and result is:12
I am thread 0 and result is:9
Output of thread 2Output of thread 1I am thread 0 and result is:11
I am thread 0 and result is:2
Output of thread 0

Lex-Yacc Tutorial

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...