SS & CD Lab 1 Dept. of CSE KNSIT VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAUM - 590014 SS & CD LAB MANUAL VI Sem CSE Prepared by Mrs. RASAGNA REDDY Lecturer DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING K.N.S INSTITUTE OF TECHNOLOGY, BANGALORE -560045 2011-12
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
PART – ALEX AND YACC PROGRAMS:Execute the following programs using LEX:
Program 1aProgram to count the number of characters, words, spaces and lines in a given input file.%{/*This is definition sec*/
int cc=0,wc=0,sc=0,lc=0;%}/*This is rules sec*/%%[ ] {sc++;cc++;}[^ \t\n]+ {wc++;cc+=yyleng;}[\n] {lc++;cc++;}[\t] {sc+=8;cc++;}%%/*This is user defined subroutine section*/int main(){char fname[20];printf(“Enter a file name:\n”);scanf(“%s”,fname);yyin=fopen(fname,"r");yylex();fclose(yyin);printf("The no.of characters = %d\n",cc);printf("The no.of spaces = %d\n",sc);printf("The no.of lines = %d\n",lc);printf("The no.of words = %d\n",wc);}
Output:-[student@localhost ~]# vi exNote: A file will be opened enter some lines of sentences in it. Come back by typing Esc :wq[student@localhost ~]# lex 1a.l[student@localhost ~]# cc lex.yy.c -ll[student@localhost usp]# ./a.outEnter a file name:exThe no.of characters = 40The no.of spaces = 5The no.of lines = 3The no.of words = 7
SS & CD Lab 3
Dept. of CSE KNSIT
Program 1bProgram to count the numbers of comments lines in a given C program. Also eliminate them and copy the resulting program into separate file.
Output:-[student@localhost usp]# vi ex[Note: Enter some content into this file]
//This is single line commentThis is not a comment line/* This is a single line comment */Next line is a multi line comment/* This ismulti linecomment */
[student@localhost usp]# lex 1b.l[student@localhost usp]# cc lex.yy.c -ll[student@localhost usp]# ./a.out
SS & CD Lab 4
Dept. of CSE KNSIT
This is not a comment lineNext line is a multi line comment
The no.of comment lines = 5
Program 2aProgram to recognize a valid arithmetic expression and to recognize the identifiers and operators present. Print them separately.
PART – BUNIX PROGRAMMING:Program 1aNon-recursive shell script that accepts any number of arguments and prints them in the Reverse order, ( For example, if the script is named rargs, then executing rargs A B C should produce C B A on the standard output).
if [ $# -eq 0 ]thenecho "NO ARGUMENTS"elsewhile [ $# -ne 0 ]dox=$1" "$xshiftdoneecho "THE REVERSED STRING IS :$x"fi
Output:
[student@localhost ~]$ sh vijetNO ARGUMENTS[student@localhost ~]$ sh vijet d o gTHE REVERSED STRING IS :g o d
Program 1bC program that creates a child process to read commands from the standard input and execute them ( a minimal implementation of a shell –like program). You can assume that no arguments will be passed to the commands to be executed.#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<fcntl.h>
SS & CD Lab 15
Dept. of CSE KNSIT
int main(){int s;char c[20];pid_t pid;pid=fork();if(pid==0){printf("enter the valid unix command\n");scanf("%s",c);system(c);}pid=waitpid(pid,&s,0);return 0;}
Output:[student@localhost ~]$ vi mann.c[student@localhost ~]$ cc mann.c[student@localhost ~]$ ./a.outenter the valid unix commandcal May 2012 Su Mo Tu We Th Fr Sa 1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31
[student@localhost ~]$ ./a.outenter the valid unix commandpwd/home/student
SS & CD Lab 16
Dept. of CSE KNSIT
Program 2aShell script that accepts two files names as arguments, checks if the permissions for these files are identical and if the permissions are identical, outputs the common permissions, otherwise outputs each file name followed by its permissions.
if [ $# -ne 2 ]thenecho "no arguments"elif [ ! -e $1 -o ! -e $2 ]thenecho "file doesnot exist"elsep1=`ls -l $1|cut -c2-10`p2=`ls -l $2|cut -c2-10`if [ $p1 == $p2 ]thenecho "file permissions are equal and are $p1"elseecho "file permission are not equal"echo "permission of $1 is $p1"echo "permission of $2 is $p2"fifi
Output:[student@localhost ~]$ sh per.shno arguments[student@localhost ~]$ sh per.sh 3.c 4.cfile permission are not equalpermission of 3.c is rwxrwxrwxpermission of 4.c is rw-rw-r--[student@localhost ~]$ sh per.sh a.c 4.cfile permissions are equal and are rw-rw-r--
Program 2bC program to create a file with 16 bytes of arbitrary data from the beginning and another 16 bytes of arbitrary data from an offset of 48. Display the file to demonstrate how the hole in file is handled.
Output:[student@localhost ~]$ cc loka.c[student@localhost ~]$ ./a.out loka.c[student@localhost ~]$ ./a.out[student@localhost ~]$ vi la[student@localhost ~]$ vi loka.c
Program 3aShell function that takes a valid directory names as an arguments and recursively descends all the subdirectories, finds the maximum length of any file in that hierarchy and write this maximum value to the standard output.
if [ $# -ne 1 ]thenecho "NO ARGUMENTS"exitfiif [ ! -e $1 ]thenecho "given directory does not exist"exitfiecho "the file with maximum length $1 directory is"ls -lR $1 | tr -s " " | cut -d " " -f5 | sort -n | tail -n 1
SS & CD Lab 18
Dept. of CSE KNSIT
output:[student@localhost ~]$ mkdir kantharaj[student@localhost ~]$ cd kantharaj[student@localhost kantharaj]$ cat > l1kuhf gfhfg[student@localhost kantharaj]$ cat > l2sthj jhbi[student@localhost kantharaj]$ cat > l3jhg8ojhydef lkijdf[student@localhost kantharaj]$ cd ..[student@localhost ~]$ sh vNO ARGUMENTS[student@localhost ~]$ sh v kantharajthe file with maximum lengthkantharaj directory is19
Program 3bC program that accepts valid file names as command line arguments and for each of the arguments, prints the type of the file (Regular file, Directory file, Character special file, Block special file, Symbolic link etc.)
#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>int main(int argc,char **argv){struct stat s;int i;if(argc==1)printf("No arguments\n");elsefor(i=1;i<argc;i++){lstat(argv[i],&s);if(S_ISREG(s.st_mode))printf("%s is a regular file \n",argv[i]);if(S_ISDIR(s.st_mode))printf("%s is a directory file \n",argv[i]);elseif(S_ISCHR(s.st_mode))printf("%s is a character device file \n",argv[i]);elseif(S_ISBLK(s.st_mode))printf("%s is a block device file\n",argv[i]);
SS & CD Lab 19
Dept. of CSE KNSIT
elseif(S_ISLNK(s.st_mode))printf("%s is a symbolic link file \n",argv[i]);}return 0;}
output:
[student@localhost ~]$ ./a.out 3.c3.c is a regular file[student@localhost ~]$ ./a.out /dev/vcs/dev/vcs is a character device file
Program 4aShell script that accepts file names specified as arguments and creates a shell script that contains this file as well as the code to recreate these files. Thus if the script generated by your script is executed, it would recreate the original files.
echo "to unbundle sh this file"for i in $*doif [ ! -f $i ]thenecho "$i doesnot exist ">/dev/ttyfiecho "echo $i is created"echo "cat>$i<<end"cat $iecho "end"done
output:[student@localhost ~]$ sh 4a.shto unbundle sh this file[student@localhost ~]$ cat>f1sdfqw4e5 [Press Ctrl+D][student@localhost ~]$ cat>f2etr235edrf [Press Ctrl+D][student@localhost ~]$ cat>f3erre5t45y [Press Ctrl+D][student@localhost ~]$ sh 4a.sh f1 f2 f3> zip[student@localhost ~]$ mkdir rasagna[student@localhost ~]$ cd rasagna
SS & CD Lab 20
Dept. of CSE KNSIT
[student@localhost rasagna]$ sh ../zip../zip: line 1: to: command not foundf1 is createdf2 is createdf3 is created
Program 4bC program to do the following: Using fork() create a child process. The child process prints its own process-id and id of its parent and then exits. The parent process waits for its child to finish(by executing the wait()) and prints its own process-id and the id of its child process and then exits.#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>int main(){pid_t pid;pid=fork();if(pid == 0){printf("IN CHILD PROCESS\n");printf("child process id is %d\n",getpid());printf("parent process id is %d\n",getppid());_exit(0);}pid=waitpid(0);printf("IN PARENT PROCESS\n");printf("child process id is %d\n",pid);printf("parent process id is %d\n",getpid());return 0;}
Output:[student@localhost ~]$ ./a.out IN CHILD PROCESSchild process id is 2389parent process id is 2388IN PARENT PROCESSchild process id is 2389parent process id is 2388
SS & CD Lab 21
Dept. of CSE KNSIT
COMPILER DESIGN PROGRAMS:
1. Write a C program to implement the syntax directed definition of “if E then S1” and “if E then S1 else S2”.[Execute this program in turbo C (file extension as .C) instead of fedora since gets function throws a warning in fedora]#include<stdio.h>#include<stdlib.h>#include<string.h>int parsecondition(char[], int, char*, int);void gen(char [],char [],char [],int);int main(){
int ct=0,len=0,eflag=0;char stmt[60];char stB[54],stS1[50],stS2[45];clrscr();printf("Format of if statement\nExample:\n");printf("if (a<b) then (s=a);\n");printf("if (a>b) then (s=a) else (s=b);\n");printf("Enter the statement\n");gets(stmt);len=strlen(stmt);ct=ct+2;ct=parsecondition(stmt,ct,stB,len);if(stmt[ct]==')')ct++;ct=ct+3;ct=parsecondition(stmt,ct,stS1,len);if(stmt[ct+1]==';'){printf("Parsing the input statement\n");gen(stB,stS1,stS2,eflag);return 0;}if(stmt[ct]==')')ct++;ct=ct+3;ct=parsecondition(stmt,ct,stS2,len);ct=ct+2;if(ct==len){eflag=1;printf("Parsing the input statement.....\n");gen(stB,stS1,stS2,eflag);return 0;
[root@localhost ~]# vi cd.y[root@localhost ~]# yacc cd.y[root@localhost ~]# cc y.tab.c[root@localhost ~]# ./a.outenter the expressiona+|b*This is the rightmost derivation--
re =>re | re=> re | re*=> re | b*=> re+ | b*=> a+ | b*
Note:1) For lex programs
vi prognsme.l[a new window will be opened press insert key and then start typing.Esc :wq to save the program.Lex progname.lCc lex.yy.c –ll./a.out[enter the input]
2) For Yacc Programsvi pname.yyacc pname.ycc y.tab.c./a.out[enter requires input]
3) Cd first program: Type program in turbo C with .C extension.Execute it.
4) For shell programs save the file name with .sh extension or just file name without extension as shown below:vi pname.sh or vi pnamesh pname.sh or sh pname