+ All Categories
Home > Documents > seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that...

seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that...

Date post: 10-Feb-2019
Category:
Upload: vandieu
View: 223 times
Download: 0 times
Share this document with a friend
33
UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB 1.Write a C/C++ POSIX compliant program to check the following limits: (i) No. of clock ticks (ii) Max. no. of child processes (iii) Max. path length (iv) Max. no. of characters in a file name (v) Max. no. of open files/ process /*pgm1.cpp*/ #define _POSIX_SOURCE #define _POSIX_C_SOURCE 199309L #include<stdio.h> #include<iostream> #include<unistd.h> using namespace std; int main() { int res; if((res=sysconf(_SC_CLK_TCK))==-1) perror("sysconf"); else cout<<"clock ticks:"<<res<<endl; if((res=sysconf(_SC_CHILD_MAX))==-1) perror("sysconf"); else cout<<"NO OF CHILD PROCESSES:"<<res<<endl; if((res=pathconf("/",_PC_PATH_MAX))==-1) perror("pathconf"); else cout<<"max pathname:"<<(res+1)<<endl; if((res=pathconf("/",_PC_NAME_MAX))==-1) perror("pathconf"); else cout<<"max file name:"<<(res)<<endl; if((res=sysconf(_SC_OPEN_MAX))==-1) perror("sysconf"); else cout<<"NO OF FILES:"<<res<<endl; return 0; } DEPARTMENT OF CSE. GEC,RAICHUR Page 1
Transcript
Page 1: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

1.Write a C/C++ POSIX compliant program to check the following limits:(i) No. of clock ticks (ii) Max. no. of child processes (iii) Max. path length(iv) Max. no. of characters in a file name (v) Max. no. of open files/ process

/*pgm1.cpp*/

#define _POSIX_SOURCE#define _POSIX_C_SOURCE 199309L#include<stdio.h>#include<iostream>#include<unistd.h>using namespace std;int main(){int res;if((res=sysconf(_SC_CLK_TCK))==-1)perror("sysconf");else cout<<"clock ticks:"<<res<<endl;if((res=sysconf(_SC_CHILD_MAX))==-1)perror("sysconf");else cout<<"NO OF CHILD PROCESSES:"<<res<<endl;if((res=pathconf("/",_PC_PATH_MAX))==-1)perror("pathconf");else cout<<"max pathname:"<<(res+1)<<endl;if((res=pathconf("/",_PC_NAME_MAX))==-1)perror("pathconf");else cout<<"max file name:"<<(res)<<endl;if((res=sysconf(_SC_OPEN_MAX))==-1)perror("sysconf");else cout<<"NO OF FILES:"<<res<<endl;return 0;}

OUTPUT[gec@localhost ~]$ c++ pg1.cpp[gec@localhost ~]$ ./a.outclock ticks:100NO OF CHILD PROCESSES:1024max pathname:4097max file name:255NO OF FILES:1024NO. OF FILES:1024

DEPARTMENT OF CSE. GEC,RAICHUR Page 1

Page 2: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.

/*pgm2.c*/

#define _POSIX_SOURCE#define _POSIX_C_SOURCE 199309L#include<stdio.h>#include<unistd.h>int main(){#ifdef _POSIX_JOB_CONTROLprintf("the system supports job control\n");#endif#ifdef _POSIX_SAVED_IDSprintf("the system supports saved ID\n");#elseprintf("saved IDs not supported\n");#endif#ifdef _POSIX_CHOWN_RESTRICTEDprintf("system supports _POSIX_CHOWN_RESTRICTED");#elseprintf("no supports of chown restricted\n");#endif#ifdef _POSIX_NO_TRUNCprintf("system wide pathname supported\n");#elseprintf("wide pathname not supported\n");#endif#ifdef _POSIX_VDISABLEprintf("disabling character is supported\n");#elseprintf("no disabling character\n");#endifreturn 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 2

Page 3: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

[gec@localhost ~]$ cc pg2.cpp[gec@localhost ~]$ ./a.outthe system supports job controlthe system supports saved IDsystem supports _POSIX_CHOWN_RESTRICTEDsystem wide pathname supporteddisabling character is supported

DEPARTMENT OF CSE. GEC,RAICHUR Page 3

Page 4: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

3.Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. If the region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region.

/*pgm3.cpp*/

#include<iostream>#include<stdio.h>#include<fcntl.h>#include<unistd.h>using namespace std;int main(int argc,char *argv[]){struct flock fvar;int fdesc,i;unsigned char buf[50];while(--argc>0){cout<<"number of arguments entered="<<argc<<endl;if((fdesc=open(*++argv,O_RDWR))==-1){perror("open");continue;}lseek(fdesc,-100,SEEK_END);cout<<"file opened is:"<<*argv<<endl;fvar.l_type=F_WRLCK;fvar.l_whence=SEEK_CUR;fvar.l_start=-100;fvar.l_len=100;if(fcntl(fdesc,F_SETLK,&fvar)==-1){cout<<"trying to set the lock\n";if(fcntl(fdesc,F_GETLK,&fvar)!=-1 && fvar.l_type!=F_UNLCK){cout<<"lock is acquired by the following process\n";cout<<*argv<<"\t locked by"<<fvar.l_pid<<"\t from"<<fvar.l_start<<"\t for"<<fvar.l_len<<"\t byte for\t"<<(fvar.l_type==F_WRLCK? "write":"read")<<endl;

DEPARTMENT OF CSE. GEC,RAICHUR Page 4

Page 5: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

return 0;}}

fvar.l_start+=fvar.l_len;fvar.l_len=0;lseek(fdesc,-50,SEEK_END);read(fdesc,&buf,50);buf[50]='\0';getchar();cout<<"LAST 50 BYTES OF THE FILE ARE....\n";cout<<buf;fvar.l_type=F_UNLCK;fvar.l_whence=SEEK_END;fvar.l_start=-100;fvar.l_len=100;if(fcntl(fdesc,F_SETLKW,&fvar)==-1)perror("fcntl");}return 0;}

OUTPUT

1st terminalgec@localhost ~]$ cat>lmnhwwwwwwfr jrrrrrhkh jrghhhhhhhhjk jdddddddddddddddddddd shhfj urrrrrrrrrrrrrrrt jssssssssssssssehhh jeeeeeeeeeeeeeeeeee jsddfhjfh jddddddddddddddd jdfffffffffffff jffffffffffffffffh jdfffffffffffffffffffffffs hbffffffffffffffffhbbb[gec@localhost ~]$ ./a.out lmnnumber of arguments entered=1file opened is:lmn

Press <ENTER>

LAST 50 BYTES OF THE FILE ARE defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz

DEPARTMENT OF CSE. GEC,RAICHUR Page 5

Page 6: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

2nd Terminal

[gec@localhost ~]$ c++ pg3.cpp[gec@localhost ~]$ ./a.out lmnnumber of arguments entered=1file opened is:lmntrying to set the locklock is acquired by the following processlmn locked by4485 from32 for100 byte for write

DEPARTMENT OF CSE. GEC,RAICHUR Page 6

Page 7: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

4.Write a C/C++ program which demonstrates interprocess communication between a reader process and a writer process. Use mkfifo, open, read, write and close APIs in your program.

/*pgm4read.c*/

#include<fcntl.h> //reader #include<sys/stat.h>#include<sys/types.h>#include<unistd.h>#include<stdio.h>#define MAX_BUF 1024int main(){int fd;char *myfifo="/tmp/myfifo";char buf[MAX_BUF];fd=open(myfifo,O_RDONLY);read(fd,buf,MAX_BUF);printf("received:%s\n",buf);close(fd);return 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 7

Page 8: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

/*pgm4write.c*/

#include<fcntl.h>#include<sys/stat.h>#include<sys/types.h>#include<unistd.h>#include<stdio.h>int main(){int fd;char *myfifo="/tmp/myfifo";mkfifo(myfifo,0666);printf("run reader to read the FIFO file\n");fd=open(myfifo,O_WRONLY);write(fd,"hi”,sizeof("hi"));close(fd);unlink(myfifo);return 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 8

Page 9: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

1 st terminal

/*Write Terminal*/

[gec@localhost ~]$ cc writer1.c[gec@localhost ~]$ ./a.outrun reader to read the FIFO file

<Ctrl> + c

2 nd Terminal

/*Read Terminal*/

output: terminal 2[gec@localhost ~]$ cc reader1.c[gec@localhost ~]$ ./a.outreceived:hi

DEPARTMENT OF CSE. GEC,RAICHUR Page 9

Page 10: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

5.a) Write a C/C++ program that outputs the contents of its Environment list.

/*pgm5a.c*/

#include<stdio.h>#include<unistd.h>

int main(int argc, char *argv[], char *envp[]){int i;for(i=0;envp[i];i++)printf("%s\n",envp[i]);}

OUTPUT

[gec@localhost CSE]$vi pgm5a.c[gec@localhost CSE]$ cc pgm5a.c[gec@localhost CSE]$ ./a.outNCTUNSHOME=/USR/LOCAL/NCTUNSORBIT_SOCKETDIR=/tmp/orbit-NITHOSTNAME=localhost.localdomainSHELL=/bin/bashTERM=xtermHISTSIZE=1000XDG_SESSION_COOKIE=588ea643320f0612413442364ec39d1a-1359072729.324813-396874535GTK_RC_FILES=/etc/gtk/gtkrc:/home/NIT/.gtkrc-1.2-gnome2WINDOWID=60817471QTDIR=/usr/lib/qt-3.3NCTUNS_TOOLS=/USR/LOCAL/NCTUNS/TOOLSQTINC=/usr/lib/qt-3.3/includeNCTUNS_BIN=/USR/LOCAL/NCTUNS/BINUSER=NIT...........

DEPARTMENT OF CSE. GEC,RAICHUR Page 10

Page 11: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

5.b) Write a C / C++ program to emulate the unix ln command.

/*pgm5b.c*/

#include<iostream>#include<unistd.h>int main(int argc, char*argv[]){using namespace std;if(argc!=3){cout<<"Usage ./a.out sourcefile destinationfile\n";return 0;}if(link(argv[1],argv[2])==-1){cout<<"Can't Link\n";return 1;}else{cout<<"Files have been linked\n";}return 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 11

Page 12: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

[gec@localhost ~]$ c++ program5b.cpp[gec@localhost ~]$ ./a.outUsage ./a.out sourcefile destinationfile

[gec@localhost ~]$ ./a.out program5b.cpp abcdeFiles have been linked

[gec@localhost ~]$ cat>xyzhi hello srinivas

[gec@localhost ~]$ ls -il program5b.cpp abcde183646 -rw-rw-r-- 8 gec gec 293 2008-01-01 01:33 abcde183646 -rw-rw-r-- 8 gec gec 293 2008-01-01 01:33 program5b.cpp

[gec@localhost ~]$ ln -s program5b.cpp pqr

[gec@localhost ~]$ ls -il program5b.cpp pqr184006 lrwxrwxrwx 1 sln sln 13 2008-01-01 00:29 pqr -> program5b.cpp183646 -rw-rw-r-- 8 sln sln 293 2008-01-01 01:33 program5b.cpp[gec@localhost ~]$ ./a.out program5b.cpp xyzCan't Link

DEPARTMENT OF CSE. GEC,RAICHUR Page 12

Page 13: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

6.Write a C/C++ program to illustrate the race condition.

/*pgm6.c*/

#include "apue.h"#include "error.c"static void charatatime(char *);int main(void){pid_t pid;if((pid=fork())<0){err_sys("fork error");}else if(pid==0){charatatime("output from child\n");}else{charatatime("output from parent\n");}exit(0);}static void charatatime(char *str){char *ptr;int c;setbuf(stdout,NULL);for(ptr=str;(c=*ptr++)!=0;)putc(c,stdout);}

DEPARTMENT OF CSE. GEC,RAICHUR Page 13

Page 14: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

[gec@localhost ~]$ vi pg6.c

[gec@localhost ~]$ cc pg6.c

[gec@localhost ~]$ ./a.out

output from child

output from parent

[gec@localhost ~]$ vi pg6.c

[gec@localhost ~]$ cc pg6.c

[gec@localhost ~]$ ./a.out

output from parent

[gec@localhost ~]$ output from child

DEPARTMENT OF CSE. GEC,RAICHUR Page 14

Page 15: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

7.Write a C/C++ program that creates a zombie and then calls system to execute the ps command to verify that the process is zombie.

/*pgm7.c*/

#include "apue.h"#ifdef SOLARIS#define PSCMD"ps -a -o pid,ppid,s,tty,comm"#else#define PSCMD"ps -o pid,ppid,state,tty,command"#endif

int main(void){

pid_t pid;if ((pid = fork()) < 0)

perror("fork error");else if (pid == 0) /* child */

exit(0);/* parent */sleep(4);system(PSCMD);exit(0);

}

DEPARTMENT OF CSE. GEC,RAICHUR Page 15

Page 16: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

Run 1

[gec@localhost CSE]$vi pgm7.c[gec@localhost CSE]$ cc pgm7.c[gec@localhost CSE]$ ./a.outPID PPID S TT COMMAND2913 2908 S pts/0 bash3823 2913 S pts/0 ./a.out3824 3823 Z pts/0 [a.out] <defunct>3826 3823 R pts/0 ps -o pid,ppid,state,tty,command

Run 2

[gec@localhost CSE]$ ./a.outPID PPID S TT COMMAND2913 2908 S pts/0 bash3835 2913 S pts/0 ./a.out3836 3835 Z pts/0 [a.out] <defunct>

3838 35 R pts/0 ps -o pid,ppid,state,tty,command

DEPARTMENT OF CSE. GEC,RAICHUR Page 16

Page 17: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

8.Write a C/C++ program to avoid zombie process by forking twice.

/*pgm8.c*/

#include "apue.h"#include "error.c"#include<sys/wait.h>

int main(void){pid_t pid;if((pid=fork())<0){err_sys("fork error");}else if(pid==0){if((pid=fork())<0)exit(0);sleep(2);printf("second child, parent pid=%d\n",getppid());exit(0);}if(waitpid(pid,NULL,0)!=pid)err_sys("waitpid error");exit(0);}

DEPARTMENT OF CSE. GEC,RAICHUR Page 17

Page 18: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

Run 1

[gec@localhost CSE]$vi pgm8.c

[gec@localhost CSE]$ cc pgm8.c

[gec@localhost CSE]$ ./a.out

second child, parent pid=3876

second child, parent pid=3875

Run 2

[gec@localhost CSE]$ ./a.out

second child, parent pid=3888

second child, parent pid=3887

Run 3

[gec@localhost CSE]$ ./a.out

second child, parent pid=3280

second child, parent pid=1

DEPARTMENT OF CSE. GEC,RAICHUR Page 18

Page 19: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

9.Write a C/C++ program to implement the system function.

/*pgm9.c*/

#include<stdio.h>#include<sys/wait.h>#include<errno.h>#include<unistd.h>

main(){char cmd[10];printf("enter the command:\n");scanf("%s",cmd);system(cmd);}intsystem(const char *cmdstring) /* version without signal handling */{

pid_t pid;int status;if (cmdstring == NULL)

return(1); /* always a command processor with UNIX */if ((pid = fork()) < 0) {

status = -1; /* probably out of processes */} else if (pid == 0) { /* child */

execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);_exit(127); /* execl error */

} else { /* parent */while (waitpid(pid, &status, 0) < 0) {

if (errno != EINTR) {status = -1; /* error other than EINTR from waitpid() */break;

}}

}

return(status);}

DEPARTMENT OF CSE. GEC,RAICHUR Page 19

Page 20: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

Run 1

[gec@localhost CSE]$vi pgm9.c

[gec@localhost CSE]$ cc pgm9.c

[gec@localhost CSE]$ ./a.out

enter the command: date

Fri april 20 06:23:34 IST 2017

Run 2

[gec@localhost CSE]$ ./a.out

enter the command: cal

TUE APRIL 2017

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 0 31

DEPARTMENT OF CSE. GEC,RAICHUR Page 20

Page 21: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

10.Write a C/C++ program to set up a real-time clock interval timer using the alarm API.

/*pgm10.c*/

#include<stdio.h>#include<signal.h>#include<unistd.h>#include<errno.h>#define INTERVAL 10void wakeup(){alarm(INTERVAL);printf("hello\n");printf("------------\n");}int main(){int i;struct sigaction action;action.sa_handler=wakeup;action.sa_flags=SA_RESTART;sigemptyset(&action.sa_mask);if(sigaction(SIGALRM,&action,0)==-1){perror("sigaction");}alarm(10);for(i=0;i<5;i++){printf("wainting for alarm\n");sleep(10);}return 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 21

Page 22: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

OUTPUT

[gec@localhost ~]$ cc p10.c

[gec@localhost ~]$ ./a.out

wainting for alarm

hello

------------

wainting for alarm

hello

------------

wainting for alarm

hello

------------

wainting for alarm

hello

------------

wainting for alarm

hello

------------

DEPARTMENT OF CSE. GEC,RAICHUR Page 22

Page 23: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

11. Write a C program to implement the syntax-directed definition of “if E then S1”and “if E then S1 else S2”.

#include<ctype.h>#include<string.h>char input[60],stmt[3][60];int len,cur,i,j;void gen(){int l1=101,l2=102,l3=103;printf("if %s goto %d\n",stmt[0],l1);printf("goto %d\n",l2);printf("%d:%s\n",l1,stmt[1]);if(cur<3)printf("%d:STOP\n",l2);else{printf("goto %d\n",l3);printf("%d %s\n",l2,stmt[2]);printf("%d:STOP\n",l3);}}int main(){printf("format of if stmt\n example\n");printf("if(a<b)then(s=a);\n");printf("if(a<b)then(s=a)else(s=b);\n");printf("enter stmt:");gets(input);len=strlen(input);int index=0;for(i=0;i<len && input[i]!=';';i++)if(input[i]=='('){index=0;for(j=i;input[j-1]!=')';j++)stmt[cur][index++]=input[j];cur++;i=j;}gen();return 0;}

DEPARTMENT OF CSE. GEC,RAICHUR Page 23

Page 24: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

output:

[gec@localhost ~]$ cc pgm11.c/tmp/ccGqFjJI.o: In function `main':pgm11.c:(.text+0x113): warning: the `gets' function is dangerous and should not be used.

[gec@localhost ~]$ ./a.outformat of if stmt exampleif(a<b)then(s=a);if(a<b)then(s=a)else(s=b);enter stmt:if(a<b)then(s=a);if (a<b) goto 101goto 102101:(s=a)102:STOP[sln@localhost ~]$ ./a.outformat of if stmt exampleif(a<b)then(s=a);if(a<b)then(s=a)else(s=b);enter stmt:if(a<b)then(s=a)else(s=b);if (a<b) goto 101goto 102101:(s=a)goto 103102 (s=b)103:STOP

DEPARTMENT OF CSE. GEC,RAICHUR Page 24

Page 25: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

12. Write a yacc program that accepts a regular expression as input and produce itsparse tree as output.

%{#include<stdio.h>#include<ctype.h>#include<stdlib.h>#include<string.h>#define MAX 100int getREindex(const char*);signed char productions[MAX][MAX];int count=0,i,j;char temp[MAX+MAX],temp2[MAX+MAX];%}%token ALPHABET%left'|'%left'.'%nonassoc'*''+'%%S:re'\n'{printf("this is the rightmost derivation--\n");for(i=count-1;i>=0;--i){if(i==count-1){printf("\n re=>");strcpy(temp,productions[i]);printf("%s",productions[i]);}else{printf("\n=>");j=getREindex(temp);temp[j]='\0';sprintf(temp2,"%s%s%s",temp,productions[i],(temp+j+2));printf("%s",temp2);strcpy(temp,temp2);}}printf("\n");exit(0);}re:ALPHABET{temp[0]=yylval;temp[1]='\0';strcpy(productions[count++],temp);}|'('re')'

DEPARTMENT OF CSE. GEC,RAICHUR Page 25

Page 26: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

{strcpy(productions[count++],"(re)");}|re'*'{strcpy(productions[count++],"re*");}|re'+'{strcpy(productions[count++],"re+");}|re'|'re{strcpy(productions[count++],"re|re");}|re'.'re{strcpy(productions[count++],"re.re");};%%int main(int argc,char **argv){yyparse();return 0;}yylex(){signed char ch=getchar();yylval=ch;if(isalpha(ch))return ALPHABET;return ch;}yyerror(){fprintf(stderr,"invalid regular expression!!\n");exit(1);}int getREindex(const char*str){int i=strlen(str)-1;for(;i>=0;--i){if(str[i]=='e'&&str[i-1]=='r')return i-1;}}

DEPARTMENT OF CSE. GEC,RAICHUR Page 26

Page 27: seenubabueducom.files.wordpress.com file · Web view2.Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using

UNIX SYSTEMS PROGRAMMING & COMPILER DESIGN LAB

output:

[gec@localhost ~]$ yacc -d pm12.y

[gec@localhost ~]$ cc y.tab.c

[gec@localhost ~]$ ./a.out

a+|b*|(b.c*)

this is the rightmost derivation--

re=>re|re

=>re|(re)

=>re|(re.re)

=>re|(re.re*)

=>re|(re.c*)

=>re|(b.c*)

=>re|re|(b.c*)

=>re|re*|(b.c*)

=>re|b*|(b.c*)

=>re+|b*|(b.c*)

=>a+|b*|(b.c*)

DEPARTMENT OF CSE. GEC,RAICHUR Page 27


Recommended