REVA ITM Dept of Computer Science & Engg REVA INSTITUTE OF TECHNOLOGY AND MANAGEMENT DEPT. OF COMPUTER SCIENCE AND ENGG. SYSTEMS SOFTWARE & COMPILER DESIGN LABORATORY Subject Code: 06CSL68 Sem : 6 th CSE Max Marks : 50 Part A 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 (a n b n , n>= 0). 6) Program to recognize the grammar (a n b, n>= 10). PART B Unix 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 SS & CD Lab Manual 1 / 44
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
REVA ITM Dept of Computer Science & Engg
REVA INSTITUTE OF TECHNOLOGY AND MANAGEMENTDEPT. OF COMPUTER SCIENCE AND ENGG.
SYSTEMS SOFTWARE & COMPILER DESIGN LABORATORY
Subject Code: 06CSL68 Sem : 6th CSE Max Marks : 50
Part AExecution 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 another 16 bytes of
arbitrary data from an offset of 48. Display the file contents to demonstrate how the hole in file is
handled.
SS & CD Lab Manual 1 / 35
REVA ITM Dept of Computer Science & Engg
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 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.
COMPILER DESIGN:
5) Write a c program to implement the syntax directed definitions of “if E then S1” and “if E else S2” (Refer fig. 8.23in the text book prescribed for 06CS62 (compiler design) by J D Ullman.
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 30 marks and one programming exercise from Part B has to be asked for a total of 20 marks.
SS & CD Lab Manual 2 / 35
REVA ITM Dept of Computer Science & Engg
LEX AND YACC
Block Diagram of Token Generator : Lexer
Input Processing Output
Block Diagram of Syntax Analyzer: Parser
Input Grammar Output
Processing
SS & CD Lab Manual 3 / 35
LexerString Tokens
ParserTokenChecks whether the Tokens are related as per the Grammar or not
REVA ITM 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.
%{#include<stdio.h>int cc=0,bc=0,lc=0,wc=0;%}
%%[a-zA-Z]+ {wc++;cc+=yyleng;}[ ] {bc++;}[\n] {lc++;}%%int main(int argc,char *argv[]){FILE *fp;if (argc>1) { fp=fopen(argv[1],"r"); yyin=fp; }yylex();printf("\n no of chars=%d \n no of words=%d \n no of lines=%d \n no of blanks=%d \n",cc,wc,lc,bc);}
$vi twob.l$lex twob.l$cc lex.yy.c –ll$./a.outenter sentencesita and geethacompound sentence
SS & CD Lab Manual 7 / 35
REVA ITM Dept of Computer Science & Engg
3) Program to recognize and count the number of identifiers in a given input file.%{#include<stdio.h>int count=0;%}%%("int")|("float")|("char")|("double") { char ch; ch=input();
%{#include<stdio.h>#include<stdlib.h>%}%token A B%%S:A S B | ;%%main(){printf("enter the string \n");yyparse();if(yyparse()==0)printf("string is valid \n");}yyerror(char *s){printf("%s not a valid string \n");}
OUTPUT:
$lex fiveb.l$yacc –d fiveb.y$cc lex.yy.c y.tab.c –ll$./a.outenter the stringaabbstring is validabbnot a valid string
SS & CD Lab Manual 14 / 35
REVA ITM Dept of Computer Science & Engg
6) Program to recognize the grammer (anb, n>=0)
Lex Part :%{#include "y.tab.h"%}%%[a] {return A;}[b] {return B;}%%
Yacc Part :%{#include<stdio.h>#include<stdlib.h>%}%token A B%%S:A A A A A A A A A A CC:A C |B ;%%main(){printf("enter the string \n");if(yyparse()==0)printf("string is valid \n");}yyerror(char *s){printf("not a valid string \n");}
enter the stringaaaaaaaaaabstring is validaaabbbbbbnot a valid string
SS & CD Lab Manual 15 / 35
REVA ITM Dept of Computer Science & Engg
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
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.
1a) Write a non-Recursive shell Script which accepts any number of arguments & prints them in reverse order (for example, if the script is named rags, then executing rags A B C should produce C B A on standard output) #1a.sh
clear rev=" "for var in $*do rev="$var $rev"doneecho -e "The original args are: $*"echo -e "\nReversed command line args are: $rev"
OUTPUT:
$ sh 1a.sh a b c d e The original args are: a b c d eReversed command line args are: e d c b a
SS & CD Lab Manual 17 / 35
REVA ITM Dept of Computer Science & Engg
1b) 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. #1b.c
char cmd[20];pid_t pid;int choice, status;pid=fork();if(pid==0){ do {
system("clear"); printf("enter command:\t");scanf("%s",cmd);system(cmd);printf("\nenter 1 to continue and 0 to exit:");scanf("%d",&choice);
} while(choice!=0);}wait(&status);
}
OUTPUT:
$ gcc 1b.c$ ./a.outenter command: cal February 2010Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728enter 1 to continue and 0 to exit:1enter command: dateTue Feb 9 13:40:27 IST 2010enter 1 to continue and 0 to exit:0
SS & CD Lab Manual 18 / 35
REVA ITM Dept of Computer Science & Engg
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. #2a.sh
if test $p1 = $p2then echo -e "\n Permissions are Equal : $p1\n"else echo -e "\n Permissions are not-Equal \n" echo -e " hence \t$1 permissions are:$p1" echo -e " \t$2 permissions are:$p2\n"fi
OUTPUT:
$ sh 2a.sh 1a.sh 2a.sh-rwxr--r-- 1 yogish yogish 508 Feb 9 11:57 1a.sh-rw-r--r-- 1 yogish yogish 578 Feb 4 09:01 2a.sh Permissions are not-Equal hence 1a.sh permissions are:rwxr--r-- 2a.sh permissions are:rw-r--r--
$ sh 2a.sh 3a.sh 2a.sh -rw-r--r-- 1 yogish yogish 578 Feb 4 09:01 2a.sh-rw-r--r-- 1 yogish yogish 305 Feb 2 12:45 3a.sh Permissions are Equal: rw-r--r--
SS & CD Lab Manual 19 / 35
REVA ITM Dept of Computer Science & Engg
2 b) C 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 contents to demonstrate how the hole in file is handled. #2b.c
$ gcc 2b.c$./a.out$vi iseDepartment of IS^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@Department of CS
SS & CD Lab Manual 20 / 35
REVA ITM Dept of Computer Science & Engg
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. #3a.shclearls -lR $1 | tr -s ' ' | sort -t" " -nk 5 |tail -1 | cut -d" " -f 5,9
OUTPUT:$sh 3a.sh directory_name
3.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.) #3b.c#include <stdio.h>#include <sys/stat.h>#include <unistd.h>
int main(int argc, char *argv[]){
int i;struct stat buf;for(i=1;i<argc;i++){
printf("The File %s is ",argv[i]);stat(argv[i],&buf);if(S_ISREG(buf.st_mode))
OUTPUT:$gcc 3b.c$ ./a.out 3b.cThe File 3b.c is Regular file$ ./a.out USPThe File USP is Directory file$ ./a.out /dev/ttyThe File /dev/tty is Character Device file
SS & CD Lab Manual 21 / 35
REVA ITM Dept of Computer Science & Engg
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). #bundle.sh
clearfor var in $*do echo "echo $var 1>&2" echo "cat >$var<<'END $var'" cat $var echo "END $var"done
OUTPUT:
# create at least two files before executing the script$ cat >f1this is fil1 f1$ cat >f2a file f2$ lsbundl.sh f1 f2
#Execute the script $ sh bundl.sh f1 f2 > rebundl.sh
#remove the files f1 and f2$ rm f1 f2$ lsbundl.sh rebundl.sh
# Now execute the newly created bundle script# it re creates the files f1 and f2
$ sh rebundl.sh f1f2$ cat f1this is fil1 f1$ cat f2a file f2
SS & CD Lab Manual 22 / 35
REVA ITM Dept of Computer Science & Engg
4.b) C 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. #4b.c
5) Write a c program to implement the syntax directed definitions of “if E then S1” and “if E else S2” (Refer fig. 8.23in the text book prescribed for 06CS62 (compiler design) by J D Ullman.
/* Input to the program is assumed to be syntactically correct. The expression of ‘if’ statement, for true
condition and statement for false condition are enclosed in parenthesis */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int parsecondition(char[],int,char*,int);
void gen(char [],char [],char[],int);
int main()
{
int counter = 0,stlen =0,elseflag=0;
char stmt[60]; // contains the input statement
char strB[54]; // holds the expression for 'if' condition
char strS1[50]; // holds the statement for true condition
char strS2[45]; // holds the statement for false condition
printf("Format of ‘if’ statement \n Example...\n");
1. What is Lexer or define Lexer2. What are different segments of Lexer program3. What are different segments of Yacc program4. What is parser? Or Define Parser5. What are the significance of the following :
a) y.tab.h b) yylval c) yyparse() d) yyerror6. What is Grammar7. What are Regular Expressions8. What are Productions9. What is Shift/Reduce Parsing 10. What is Symbol Table? What information is stored in it11. Briefly explain about Parser-Lexer Communication12. Expand Yacc13. What is Character Class14. Explain the action of the following symbol within Regular Expression
15. What is Parse Tree ? what for it is used16. What is lex.yy.c17. Explain the logic of the given Program18. Define Assembler19. Briefly state two phases of an Assembler20. What is a compiler21. Define Loader22. Explain the significance of yyerror()23. Explain the significance of yytext
SS & CD Lab Manual 32 / 35
REVA ITM Dept of Computer Science & Engg
Part-B
1. Explain the feature of Unix OS.2. What is process? Explain the parent child relationship.3. What are the main functions of shell?4. What are the steps involved in creating child process?5. What are positional parameters?6. What are environmental variables?7. What is $*,$#,$$,$_,$!8. Where do you use expr in shell scripts?9. What is shell script? How it is different form c program?10. What do you mean by exit status of a command?11. Where do you use cut command?12. Differentiate head and tail?13. What is set command?14. What is the function of lseek ( ) ?15. What are types of files in Unix?16. How can you change the file permissions?17. What is fork ( ) ?18. What is the purpose of wait ( ) ?19. What is the purpose pf getenv ( ) ?20. Who is the parent of all processes?21. Explain the mechanism of process creation?22. List the file attributes?23. What are shell variables? Give an example.24. What does sed command do?
SS & CD Lab Manual 33 / 35
REVA ITM Dept of Computer Science & Engg
Instructions for Compilation
LEX:
If Program name is p1.l$ lex p1.l$ cc lex.yy.c –lfl$ ./a.out
YACC:
If Program name is p1.y$ yacc -d p1.y$ cc y.tab.,c –ly $ ./a.out
Yacc with Lex program
If Program name is p1.l, p1.y$ lex p1.l$ yacc –d p1.y$ cc y.tab.c lex.yy.c –lfl$ ./a.out
SHELL Program :
If Program name is p1.sh
$ Shell p1.sh
SS & CD Lab Manual 34 / 35
REVA ITM Dept of Computer Science & Engg
Bibliography
1. Aho, Alfred V., Ravi Sethi and Jeffrey D. Ullman [1986]. Compilers, Principles, Techniques and Tools. Addison-Wesley, Reading, Massachusetts.