Sir MVIT Dept of Computer Science & Engg SIR M. VISVESVARAYA INSTITUTE OF TECHNOLOGY (Affiliated to VTU & recognized by AICTE, New Delhi) ACCREDATED BY NBA Department Of Computer Science & Engineering SYSTEM SOFTWARE AND COMPILER DESIGN LABORATORY 06CSL68 6 TH SEMESTER System Software Lab Manual 1 / 35 Prepared By, Salma Banu N K
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.
Transcript
Sir MVIT Dept of Computer Science & Engg
SIR M. VISVESVARAYA INSTITUTE OF TECHNOLOGY
(Affiliated to VTU & recognized by AICTE, New Delhi)
ACCREDATED BY NBA
Department Of Computer Science & Engineering
SYSTEM SOFTWARE AND COMPILER DESIGN LABORATORY
06CSL68
6TH SEMESTER
Prepared & Compiled By, Under Guidance of,
Mrs. Salma Banu N K Prof. Dilip K Sen
Lecturer, Dept. Of CS&E HOD, Dept. Of CSE
System Software Lab Manual 1 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
SIR M. VISVESVARAYA INSTITUTE OF TECHNOLOGYBANGALORE
DEPT. OF COMPUTER SCIENCE AND ENGG.
SYSTEM SOFTWARE AND COMPILER DESIGN LABORATORY
Subject Code: 06CSL68 Sem : 6th CSE Max Marks : 50
Part ALEX AND YACC PROGRAMS:
Execution of the following programs using LEX:1) a. Program to count the number of characters, words, spaces and lines in a given input file. b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operators present. Print them separately. b. Program to recognize whether a given sentence is simple or compound.3) Program to recognize and count the number of identifiers in a given input file.
Execution of the following programs using YACC:4) a. Program to recognize a valid arithmetic expression that uses operators +, -, * and /. b. Program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits.5) a. Program to evaluate an arithmetic expression involving operators +,-, * and /. b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (anbn, n>= 0).6) Program to recognize the grammar (anb, n>= 10).
PART BUNIX PROGRAMMING:
1) a. Non-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).
b. C 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.
2) a. Shell script that accepts two file 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.
b. C program to create a file with 16 bytes of arbitrary data from the beginning and
System Software Lab Manual 2 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
another 16 bytes of arbitrary data from an offset of 48. Display the file contents to demonstrate how the hole in file is handled.
3) a. Shell function that takes a valid directory names as an argument and Recursively descends all the subdirectories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.
b. C 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.)
4) a. Shell 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(This is same as the “bundle” script described by Brain W. Kernighan and Rob Pike in “ The Unix Programming Environment”, Prentice – Hall India).
b. C program to do the following: Using fork( ) create a child process. The child processprints 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.
COMPILER DESIGN:
5) Write a C program to implement the syntax-directed definition of “if E then S1” and “if E then S1 else S2”. (Refer Fig. 8.23 in the text book prescribed for 06CS62 Compiler Design, Alfred V Aho, Ravi Sethi, Jeffrey D Ullman: Compilers- Principles, Techniques and Tools, Addison-Wesley, 2007.)
6) Write a yacc program that accepts a regular expression as input and produce its parse tree as output.
Instructions:In the examination, a combination of one LEX and one YACC problem has to be asked from Part A for a total of 25 marks and one programming exercise from Part B has to be asked for a total of 25 marks.
System Software Lab Manual 3 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
PART A LEX :
1) a. Program to count the number of characters, words, spaces and lines in a given input file.
if (argc!=2) { printf("\nusage:./a.out filename\n"); return(0); }
yyin=fopen(argv[1],"r");yylex();printf("\n no of lines are %d\n",lc);printf("\n no of words are %d\n",wc);printf("\n no of blanks are %d\n",bc);printf("\n no of character are %d\n",cc);
}
Sample Input/Output:
$ lex prga1a.l $ cc lex.yy.c $ ./a.out ben10
no of lines are no of words are no of blanks are no of character
$ cat ben10
System Software Lab Manual 4 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
Ben discovers a strange device, the Omnitrix, which locks itself on his wrist.Each alien has its own set of special abilities that Ben can use, while at the same time still maintaining his 10-year-old personality.
1) b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.
main(int argc,char *argv[]){ if(argc!=3) { printf("\nusage:<./a.out><source><destination>"); return(0); } yyin=fopen(argv[1],"r"); yyout=fopen(argv[2],"w"); yylex(); printf("\n no of comment lines is %d",c);}
Sample Input/Output :
$vi prga1b.l$ lex prga1b.l$ cc lex.yy.c -lfl$ ./a.out test.c out.c
no of comment lines is 4
$ cat out.c
#include<stdio.h>int main(){int a,b;
System Software Lab Manual 5 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
printf("this is test program "); return 0; }
$ cat test.c
/* this is for counting the no of comment lines in a given prog. */#include<stdio.h>int main(){int a,b; //data type declarationprintf("this is test program "); /* this is for printing */return 0; } //end of main program
2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operators present. Print them separately.
2) b. Program to recognize whether a given sentence is simple or compound.%{#include<stdio.h>int flag=0;%}%%[\t\n]+ ;([aA][nN][dD]) flag=1;("or") {flag=1;}("nevertheless") {flag=1;}("inspite") {flag=1;}("because") {flag=1;}. ;%%main(){
printf("\n\n enter the sentence\n");yylex();if(flag==0)
printf("\n\n sentence is simple\n");else
printf("\n\n sentence is compund\n"); }
System Software Lab Manual 7 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
Sample Input/Output :
Run 1:
$ lex prog2b.l$ cc lex.yy.c -lfl$ ./a.out
enter the sentence
Noddy and his friends have been exploring the magical world.
sentence is compound
Run 2:$ ./a.out
enter the sentence
A shooting star, in a galaxy far far away.
Sentence is simple
3) Program to recognize and count the number of identifiers in a given input file.
%{#include<stdio.h>%}%token L D P%%var:L XX:X D| {printf("\nvalid variable"); return 0;}P { ; }%%main(){
printf("\nEnter variable");yyparse();
System Software Lab Manual 11 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
}yyerror(){
printf("\nInvalid variable");}
Alternate Program
%{#include<stdio.h>#include<ctype.h>%}%token LETTER DIGIT%%var : LETTER st '\n' {printf("\nValid variable\n");return 0;};st : LETTER | DIGIT | DIGIT st | LETTER st ;%%
%{#include<stdio.h>int valid=1;%}%token A B%%str:S'\n' {return 0;}S:A S B | ;%%main(){printf("Enter the string:\n");yyparse();if(valid==1) printf("\nvalid string");}
%{#include<stdio.h>int valid=1;%}%token A B%%str:S'\n' {return 0;}S:A S B | ;%%main(){printf("Enter the string:\n");yyparse();if(valid==1) printf("\nValid string");}
System Software Lab Manual 16 / 27 Prepared By, Salma Banu N K
$ lex prog6.l$ yacc -d prog6.y$ cc -c lex.yy.c y.tab.c$ cc -o a.out lex.yy.o y.tab.o -lfl$ ./a.outEnter the string:aaaabbInvalid string Enter the string:aaaaaaaaaabvalid string
PART B Unix Programming:
Shell Programming :
The Shell is one of the major components of the Unix system. As a command interpreter, it provides an interface between the user and the operating system. The shell is also a programming language that executes shell scripts in the interpretive mode – one line at a time.
The Shell programs or shell scripts are executable text files that contain UNIX commands.
The Unix Systems offers a variety of shells like Bourne shell , C shell, Korn shell and bash(born again shell) shell for you to choose.
Shell scripts are typically written when:- there is a command or string of commands that you will use more than
once.- you want access to command line arguments- you need looping and testing.
Writing and running a Shell Script
System Software Lab Manual 17 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
1. Use your vi editor to create the shell script2. To run the script, use sh followed by the name of the shell script. For example,
$ sh filename.sh When used in this way, the script doesn’t need to have executable permission. OR To run the script, make it executable and invoke the script name.
For example,$ chmod +x filename.sh$ filename.sh
Simple Shell script
Shell script (named first) that displays the current date, the users on the system and a welcome message for the user logged in.
1) a. Non-recursive shell script that accepts any number of arguments and prints them in the Reverse order, (For example, if the script is named ranges, then executing ranges A B C should produce C B A on the standard output).
echo "Input string is:$*"for x in "$@"doy=$x" "$ydoneecho "Reversed string is:$y"
Sample input/output:
$ sh 1a.sh a b c dInput string is: a b c dReversed string is: d c b a
System Software Lab Manual 18 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
1) b. C 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>int main(){ char str[10]; int pid; pid=fork(); if(!pid) { printf("Child process..."); printf("\nEnter a command:"); scanf("%s",str); system(str); printf("Finished with child"); } else{
wait();printf("\nParent Process");
}return 0; }Sample input/output :
$ ./a.outChild process...Enter a command:dateWed Feb 7 21:52:00 IST 2007Finished with childParent Process
2) a. Write a shell script that accepts two filenames as arguments, checks if the permissions for these files are identical & if the permissions are identical, outputs the common permissions, otherwise outputs each file names followed by its permissions
f1=`ls -l $1|cut -c2-10`f2=`ls -l $2|cut -c2-10`if [ $f1 == $f2 ]thenecho "files $1 & $2 have common file permissions: $f1"elseecho "file $1 has file permissions : $f1"echo "file $2 has file permissions : $f2"
System Software Lab Manual 19 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
fi
Sample input/output :
Run 1: $ sh 2a.sh 4a.l 4a.shfile 4a.l has file permissions : rw-r--r--file 4a.sh has file permissions : rwxr-xr-x
Run 2:
$ sh 2a.sh 1a.sh 1b.cfiles 1a.sh & 1b.c have common file permissions: rwxr-xr-x
2) b. Write a C program to create a file with 16 bytes of arbitrary data fro the beginning and another 16 bytes of arbitrary data from an offset of 48.Display the file contents to demonstrate how the hole in file is handled.
System Software Lab Manual 20 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
3) a. Shell script that takes a valid directory names as an argument and recursively descents all the subdirectories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.
if [ $# -ne 1 ]thenecho "Usage : $0 directory"exit 1fils -Rl $1 | grep "^-" | sort -n -k 5,9 | tee f1echo "Maximum length of file is "tail -1 f1 | cut –d “ ” –f 5,8
Sample input/output :
$ sh 3a.sh .-rw-r--r-- 1 root root 76 Feb 7 21:43 y.tab.h-rw-r--r-- 1 root root 90 Feb 7 20:37 cprog.c-rw-r--r-- 1 root root 120 Feb 6 22:59 6.l-rw-r--r-- 1 root root 130 Feb 6 22:33 5b.l-rw-r--r-- 1 root root 152 Feb 6 23:15 4b.l-rw-r--r-- 1 root root 176 Feb 6 22:18 5a.lMaximum length of file is176 7 22:10 5a.l
3) b. Write a C Program that accepts valid file names as command line Arguments & 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 <sys/stat.h>#include<stdio.h>int main(int argc,char* argv[]){ int i; struct stat file; for(i=1;i<argc;i++) {
if(lstat(argv[i],&file)!=0) {printf("File %s does not exits",argv[i]);continue;
}printf("\nFile %s is a ",argv[i]);if(S_ISREG(file.st_mode)) printf("Regular File");else if(S_ISDIR(file.st_mode)) printf("Directory File");else if(S_ISCHR(file.st_mode)) printf("Character Device File");else if(S_ISBLK(file.st_mode)) printf("Block Device File");
System Software Lab Manual 21 / 27 Prepared By, Salma Banu N K
Run 1: $ ./a.out 3a.shFile 3a.sh is a Regular File
Run 2:
$ ./a.out 3a.sh /dev/hda4File /dev/hda4 is a Block Device File
4) a. Shell 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 (This is same as the “bundle” script described by Brain W. Kernighan and Rob Pike in “The Unix Programming Environment”, Prentice – Hall India).
for x in $*doecho "cat > $x << hereabcdefghihere"done > recreate
Sample input/output :
$ sh 7a.sh file1 file2$ vi recreatecat > file1 << hereabcdefghiherecat > file2 << hereabcdefghi
System Software Lab Manual 22 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
here$ sh recreate$ ls file1 file2 recreate$ cat file1abcdefghi$ cat file2abcdefghi
4) b. Write a C program to create 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 & prints its own process id & the id of its child process and then exits.
int main(){
char str[10];int pid;pid=fork();if(!pid){
printf("Child process...");printf("\n\nChild PID : %d",getpid());printf("\nParent PID : %d",getppid());printf("\n\nFinished with child\n");
System Software Lab Manual 23 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
COMPILER DESIGN:
5. Write a C program to implement the syntax-directed definition of “if E then S1” and “if E then S1 else S2”. (Refer Fig. 8.23 in the text book prescribed for 06CS62 Compiler Design, Alfred V Aho, Ravi Sethi, Jeffrey D Ullman: Compilers- Principles, Techniques and Tools, Addison-Wesley, 2007.)
int counter=0,stlen=0,elseflag=0;char stmt[60];char strB[54];char strs1[50];char strs2[45];printf("\nFormat of 'if' statement \nExample : \n");printf(" if( a < b ) then ( s = a );\n");
printf(" if( a < b ) then ( s = a ) else ( s = b );\n\n"); printf("Enter the statement \n"); scanf("%[^\n]s",stmt);
stlen=strlen(stmt);counter=counter+2; //increment over 'if'counter=parsecondition(stmt,counter,strB,stlen);if(stmt[counter]==')')
System Software Lab Manual 25 / 27 Prepared By, Salma Banu N K
Sir MVIT Dept of Computer Science & Engg
6. Write a yacc program that accepts a regular expression as input and produce its parse tree as output.%{#include<ctype.h>char str[20];int i=0;%}%token id%left '+''/''*''-'%%E : S {infix_postfix(str);}S : S '+' T | S '-' T | TT : T '*' F | T '/' F | FF : id | '(' S ')' ;%%#include<stdio.h>main(){printf("\nEnter an expression : ");yyparse();}yyerror(){