Monday, October 17

Recursive Descent Parser using C program

Updated on 09/02/15 Hello reader, Here is the updated post considering your valuable suggestions. check it out. The grammar on which we are going to do recursive descent parsing is: E -> E+T | T T -> T*F | F F -> (E) | id   RD parser will verify whether the syntax of the input stream is correct by checking each character  from left to right. A basic operation necessary is reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input.  The given grammar can accept all arithmetic equations involving +, * and (). eg:  a+(a*a)  a+a*a...

Monday, October 10

SEMAPHORES TO IMPLEMENT PRODUCER CONSUMER PROBLEM

#include<sys/sem.h> #include<fcntl.h> #include<unistd.h> #include<sys/types.h> #include<stdio.h> #include<sys/ipc.h> void up(int); void down(int); union value { int val; struct semid_ds *buf; unsigned short *array; struct seminto *_buf; } main() { int mutex,full,empty,pid,i,key,j,k; union value arg; key=ftok("",'a'); mutex=semget(key,1,0660|IPC_CREAT); arg.val=0; semctl(full,0,SETVAL,arg); key=ftok("",'c'); empty=semget(key,1,0660|IPC_CREAT); arg.val=4; semctl(empty,0,SETVAL,arg); pid=fork(); switch(pid) { case -1: printf("Error"); case 0: sleep(13); printf("\n"); printf("Consumer consuming items\n"); for(i=0;i<5;i++) { down(full); down(mutex); for(j=4-i;j>=1;j--) printf("*"); for(k=0;k<=i;k++) printf("_"); printf("\n"); fflush(stdout); up(mutex); up(empty); sleep(1); } printf("Buffer...

Saturday, October 8

Calculator using YACC program

Here is the Yacc program implementing a simple calculator: %{ #include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*''/' %right '^' %% s:e'\n'{printf("%d",$1);} e:    e '+' e{$$=$1+$3;}  |e '-' e{$$=$1-$3;}  |e '*' e{$$=$1*$3;}  |e '/' e{$$=$1/$3;}  |e '^' e {   int i,j=$1;   for(i=1;i<$3;i++)   {   j=j*$1;   $$=j;   }   }  |'('e')'{$$=$2;}  |num1; num1:num1 num{$$ = $1*10 + $2;}  |num  ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0'; return num; } return c; } int...

YACC program to convert infix

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %% s:e'\n'{} e:e'+'e{printf("+");} |e'-'e{printf("-");} |e'/'e{printf("/");} |e'*'e{printf("*");} |num1{printf("%d",$1);} num1:num1 num {$$=$1*10+$2;} |num ; %% yylex() {  int c;  c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1;...

Yacc program to evaluate POSTFIX expression

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %right '^' %% s:e'\n'{printf("\n%d",$1);} e:e e'+'{$$=$1+$2;} |e e'-'{$$=$1-$2;} |e e'*'{$$=$1*$2;} |e e'/'{$$=$1/$2;} |num ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1; } Illustraion: Evaluation of postfix expression can be done very easily. 34+ 3--> store value in $1 4--> store value in $2 +--> add $1 and $2 and store result in $$ For more Yacc programs click here.....

C Program to generate Intermediate code

#include<stdio.h> #include<ctype.h> #include<string.h> main() { char a[20]; int i=2,n; printf("Exp  :"); scanf("%s", a); if(isdigit(a[0])) printf("MVI A,%c\n",a[0]); else printf("MOV A,%c\n",a[0]); n=strlen(a); while(i<n) { switch(a[i]) { case '+':printf("ADD B\n");i+=3;break; case '-':printf("SUB B\n");i+=3;break; default:if(isdigit(a[i])) printf("MVI B,%c\n",a[i]); else printf("MOV B,%c\n",a[i]); i--; } }...

LEX program to check the syntax of SCANF statement

%{ #include<stdio.h> #include<ctype.h> int i=0,j=0; %}a "%d"|"%f"|"%c"|"%s" id [a-zA-Z][a-zA-Z0-9]* %% scanf\((\"({a}*|.*)*\"(,\&{id})*\))\; {for(i=0;i<yyleng;i++) {if(yytext[i]=='%') j++; if(yytext[i]==',') j--; } if(j==0) printf("Correct"); else printf("Incorrect"); } %% main() { yyin=fopen("sample.c","r"); yylex(); } int yywrap() { return 1; }        ...

LEX program to check the syntax of PRINTF statement

%{ #include<stdio.h> #include<ctype.h> int i=0,j=0; %}a "%d"|"%f"|"%c"|"%s" id [a-zA-Z][a-zA-Z0-9]* %% printf\((\"({a}*|.*)*\"(,{id})*\))\; {for(i=0;i<yyleng;i++) { if(yytext[i]=='%') j++; if(yytext[i]==',') j--; } if(j==0) printf("Correct..!!"); else printf("Incorrect..!!"); } %% main() { yyin=fopen("sample.c","r"); yylex(); } int yywrap() { return 1; }       Explanation: Well, checking syntax of printf statement is very easy. The syntax should be "printf" followed by "(" and  " . It can include a usual string or %d,%f type expression delimited by another ". The entities specified...

udp communication

Hi Viewer, Here is a simple java program showing client-server interacion using UDP. In tgis example, client will send a message and the sever will reply back with same message after converting it to Uppercase..!! server.java import java.io.*; import java.net.*; class UDPServer { public static void main(String args[])throws Exception { DatagramSocket serverSocket=new DatagramSocket(1249); byte[] receiveData=new byte[1024]; byte[] sendData=new byte[1024]; while(true) { DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length); serverSocket.receive(receivePacket); String sentence=new String(receivePacket.getData()); InetAddress...

Lex program to check the syntax of FOR loop

%{ #include<stdio.h> #include<ctype.h> int c=1; %} op "++"|"--" rop "<"|">"|"<="|">="|"=="|"!=" id [a-zA-Z][a-zA-Z0-9]* no [0-9]* pp [\n] %% for\(({id}=({no}|{id}))?\;{id}{rop}({id}|{no})\;{id}{op}\){pp}+\{(.*\n)*.*\} {printf("correct");c=0;} %% main() { yyin=fopen("file11.c","r"); yylex(); if(c==1) printf("incorrect"); } int yywrap() { return 1;...

FIRST of a Given Grammar using C program

Hello Friend, In this post we will discuss how to find FIRST of a grammar using C. The rules for finding FIRST of a given grammar is: If X is terminal, FIRST(X) = {X}. If X → ε is a production, then add ε to FIRST(X). If X is a non-terminal, and X → Y1 Y2 … Yk is a production, and ε is in all of FIRST(Y1), …, FIRST(Yk), then add ε to FIRST(X). If X is a non-terminal, and X → Y1 Y2 … Yk is a production, then add a to FIRST(X) if for some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1), …, FIRST(Yi-1). Assumptions: Each Non terminal character is represented by one Uppercase letter. Each Terminal character is represented...
Page 1 of 3012345Next