0% found this document useful (0 votes)
92 views69 pages

Ss&Os Lab Master Manual2020-21 - Ajiet

This document provides information about a System Software Laboratory course offered at Visvesvaraya Technological University Belgaum. The document includes details such as the course code, semester, instructor details, objectives of the course, expected outcomes, list of experiments to be conducted, steps for executing programs using Lex and Yacc tools, and an introduction to compiler design and Lex. The course involves implementing programs for lexical analysis, syntax analysis, code generation and operating system concepts like scheduling and memory management algorithms.

Uploaded by

Zaid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views69 pages

Ss&Os Lab Master Manual2020-21 - Ajiet

This document provides information about a System Software Laboratory course offered at Visvesvaraya Technological University Belgaum. The document includes details such as the course code, semester, instructor details, objectives of the course, expected outcomes, list of experiments to be conducted, steps for executing programs using Lex and Yacc tools, and an introduction to compiler design and Lex. The course involves implementing programs for lexical analysis, syntax analysis, code generation and operating system concepts like scheduling and memory management algorithms.

Uploaded by

Zaid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

VISVESVARAYA TECHNOLOGICAL

UNIVERSITY BELGAUM

SYSTEM SOFTWARE LABORATORY

(Subject Code: 18CSL66)


MASTER MANUAL
VI-SEMESTER

Mrs Snitha Shetty


Assistant Professor

AJIET
A J INSTITUTE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


(A unit of Laxmi Memorial Education Trust. (R))

NH - 66, Kottara Chowki, Kodical Cross,Mangaluru - 575 006


SS Lab 18CSL66

University syllabus
SYSTEM SOFTWARE LABORATORY
(Subject Code: 18CSL66)

[As per Choice Based Credit System (CBCS) scheme, Effective from the academic year 2018-2019]

SEMESTER – VI

Laboratory Code 18CSL66 IA Marks 40

Number of 01Hr Tutorial (Instructions) Exam 60


Lecture Marks
Hours/Week + 02 Hours Laboratory

Total Number of 40 Exam 3


Lecture Hours Hours

CREDITS – 02

Course objectives:

This laboratory course enables students to get practical experience to

 To make students familiar with Lexical Analysis and Syntax Analysis phases of Compiler
Design and implement programs on these phases using LEX & YACC
Tools and/or C/C++/Java
 To enable students to learn different types of CPU scheduling algorithms used in Operating
system.
 To make students able to implement memory management - page replacement and
Deadlock handling algorithms

Descriptions

Dept. of CSE,AJIET,Mangaluru Page 2


SS Lab 18CSL66

Implement all the experiments in C/C++/Java language under LINUX environment.

Laboratory Experiments:

1. a) Write a LEX program to recognize valid arithmetic expression. Identifiers in the


expression could be only integers and operators could be + and *. Count the identifiers

& operators present and print them separately.

b) Write YACC program to evaluate arithmetic expression involving operators: +, -,

*, and /.

2. Develop, Implement and Execute a program using YACC tool to recognize all
strings ending with b preceded by n a’s using the grammar an b (note: input n value).

3. Design, develop and implement YACC/C program to construct Predictive / LL(1)


se the

sentence: abba$

4. Design, develop and implement YACC/C program to demonstrate Shift Reduce

parse the sentence: id + id * id.

5. Design, develop and implement a C/Java program to generate the machine code
using Triples for the statement A = -B * (C +D) whose intermediate code in three-address form:

T1 = -B

T2 = C + D

T3 = T1 + T2

A = T3

6. a) Write a LEX program to eliminate comment lines in a C program and copy the
resulting program into a separate file.

b) Write YACC program to recognize valid identifier, operators and keywords in thegiven text (C
program) file.

7. Design, develop and implement a C/C++/Java program to simulate the working of

Dept. of CSE,AJIET,Mangaluru Page 3


SS Lab 18CSL66

Shortest remaining time and Round Robin (RR) scheduling algorithms. Experiment with different
quantum sizes for RR algorithm.

8. Design, develop and implement a C/C++/Java program to implement Banker’s


algorithm. Assume suitable input required to demonstrate the results.

9. Design, develop and implement a C/C++/Java program to implement page


replacement algorithms LRU and FIFO. Assume suitable input required to demonstrate the results.

Course outcomes:

On the completion of this laboratory course, the students will be able to:

 Utilize the skill of Regular expression for implementing and demonstrating a Lexical analysis
and Syntax analysis phases of compiler

 Utilize the skill of CFG for implementing and demonstrating a machine code generation for the
given intermediate code.
 Apply the knowledge on operating system to implement and demonstrate different process
scheduling algorithms
 Apply the skill of process management to implement and demonstrate resource allocation
algorithms
 Apply the skill of memory management to implement and demonstrate memory allocation (Page
replacement) algorithms

Graduate Attributes (as per NBA)

1. Engineering Knowledge
2. Problem Analysis
3. Design/Development of Solutions
4. Modern Tool Usage

Conduction of Practical Examination:

1. All laboratory experiments (TEN no’s) are to be included for practical examination.
2. Students are allowed to pick one experiment from the lot.
Dept. of CSE,AJIET,Mangaluru Page 4
SS Lab 18CSL66

3. Strictly follow the instructions as printed on the cover page of answer script
4. Marks distribution: Procedure + Conduction + Viva Questions:20 + 50 +10 (80)
5. Change of experiment is allowed only once and marks allotted to the procedure part to be
made zero

STEPS FOR EXECUTION OF PROGRAMS

Lex program
1. Open Terminal (ctrl+alt+T)
2. gedit filename.l
3. lex filename.l
4. cc lex.yy.c –ll
5. ./a.out

Yacc program
1. Open Terminal (ctrl+alt+T)
2. gedit filename.y
3. lex filename.l
4. yacc –d filename.y
5 cc lex.yy.c y.tab.c –ll
6. ./a.out

Dept. of CSE,AJIET,Mangaluru Page 5


SS Lab 18CSL66

INTRODUCTION TO LEX AND YACC


1.1 Introduction to Compiler
The use of higher level languages for programming has become widespread. In order to execute a high
level language program written by a programmer, it is necessary to convert the program into the
language understood by the machine. A translator is a program, which performs translation from a
program written in one language to program in another language of a computer.

The important tasks of a translator are:

 Translating the high level language program input into an equivalent machine
language program.

 Providing diagnostic messages wherever the programmer violates specifications of


the high level language.

Language translators are generally classified into two groups.

a) Assemblers. b) Compilers.
Assemblers are translators for the lower level assembly language of Computers. It translates program
written in assembly language to machine language.

A Compiler is a translator program that translates a program written in high level language into an
equivalent program in low level language.

Dept. of CSE,AJIET,Mangaluru Page 6


SS Lab 18CSL66

1.1.1 Structure of Compiler


A compiler takes as input a source program and produces as output an equivalent sequence of
machine instructions. The different phases of the compiler are as given in the following diagram.

The first phase of the compiler is called Lexical Analyzer or Scanner. It reads the input source and
divides the input into meaningful units. These units are called tokens. A token is a sub-string of the
source program that is to be treated as a single unit.

Example:

if ( x < 5.0 ) THEN x=x+2 ELSE x=x-3;

TOKENS:

Keywords: IF, THEN, ELSE

Identifier(s): x
Dept. of CSE,AJIET,Mangaluru Page 7
SS Lab 18CSL66

Constants: 2, 3, 5.0

Operators: < , +, -

The Syntax Analyzer groups’ tokens together into syntactic structure called as expression. The
syntactic structure can be regarded as a tree called as parse trees.

For example, for the expression total = count + rate * 10 the parse tree can be generated as follows:

total +

count *

rate 10

The Semantic Analyzer determines the meaning of the source string. For example, meaning of the

Source string means matching of the parenthesis in the expression or checking the scope of operation
as shown below.

Dept. of CSE,AJIET,Mangaluru Page 8


SS Lab 18CSL66

total +

count *

rate
int to float

10

1.2 Introduction to LEX


Lex and Yacc helps you to write programs that transforms structured input. Lex generates C code for
lexical analyzer where as Yacc generates Code for Syntax analyzer.

Lexical analyzer is build using a tool called LEX. Input is given to LEX and lexical analyzer is
generated.

Lex is an UNIX utility. It is a program generator designed for lexical processing of character input
streams. Lex generates C code for lexical analyzer. It uses the patterns that match strings in the
input and converts the strings to tokens. Lex helps you by taking a set of descriptions of possible
tokens and producing a C routine, which we call a lexical analyzer.The token description that lex uses
are known as regular expressions.

1.2.1 Uses of LEX

 Simple text search program creation


 C compiler creation

1.2.2 Structure of LEX source program


Definition section

Dept. of CSE,AJIET,Mangaluru Page 9


SS Lab 18CSL66

%%
Rules section
%%
C code section

Copied to the beginning of C


Definition
code
Section
Lex specification is translated into a file Lex.yy.c code
Rule Section containing a C routine called yylex()

Code Section
Copied to the end of C code

%% is a delimiter to mark the beginning of the Rule section. The second %% is optional, but the first is required to mark the beginning of the
rules. The definitions and the code /subroutines are often omitted.

1.2.2.1 Definition Section


There are three things that can go in the definitions section:

C code

It introduces any initial C program code we want copies into the final program. We surround the C
code with the special delimiters “%{“ and “%}”. Lex copies the material between %{ and %} directly
to the beginning of generated C file, so you may write any valid C code here. This is typically used for
defining file variables, and for prototypes of routines that are defined in the code segment.

Substitution: A Substitution is very much like #define cpp directive.

For example

letter [a-zA-Z]

digit [0-9]

punct [,.:;!?]
Dept. of CSE,AJIET,Mangaluru Page 10
SS Lab 18CSL66

nonblank [ˆ \t]

Substitution line does not end with semicolon. This Substitution can be used in the rules section: one
could start a rule {letter} + {...

State definitions If a rule depends on context, it’s possible to introduce states and incorporate those in
the rules. A state definition looks like %s STATE, and by default a state INITIAL is already given. If
the application of a rule depends on context, there are a couple of ways of dealing with this. We
distinguish between ‘left state’ and ‘right state’, basically letting a rule depend on what comes before
or after the matched token.

1.2.2.2 Rule Section


%% marks the beginning of the Rule section. Each rule is made up of two parts. A pattern and an
action separated by white space. The lexer i.e Lexical Analyzer will execute the action when it
recognizes the pattern. A number of combinations of pattern and action: if the action is more than a
single command it needs to be in braces. References to the substitution in the rules section are
surrounded by braces to distinguish them from literals. For example: {letter}

1.2.2.3 Code Section


This section consists of any legal C code and declarations. Lex copies it to the C file after the end of
the lex generated code. This can be very elaborate, but the main ingredient is the call to yylex, the
lexical analyser. If the code segment is left out, a default main is used which only calls yylex. If the
lex program is to be used on its own, this section will contain a main program. If you leave this
section empty you will get the default main.

Note that this section has to include the yywrap() function. Lex has a set of functions and variables
that are available to the user. One of them is yywrap. Typically, yywrap() is defined as shown in the
example below.

int main()
{
Dept. of CSE,AJIET,Mangaluru Page 11
SS Lab 18CSL66

yylex();
return 0;
}
int yywrap()
{
return 1;
}
where yylex is the parser that is built from the rules.

1.2.3 Advanced Lex


Lex has several functions and variables that provide different information and can be used to build
programs that can perform complex functions. Some of these variables and functions, along with their
uses, are listed in the following tables.

Lex variables

Yyin Of the type FILE*. This points to the current file being parsed by the lexer.

Yyout Of the type FILE*. This points to the location where the output of the lexer will
be written. By default, both yyin and yyout point to standard input and output.

Yytext The text of the matched pattern is stored in this variable (char*).

Yyleng Gives the length of the matched pattern.

Yylineno Provides current line number information. (May or may not be supported by the
lexer.)

Lex functions

yylex() The function that starts the analysis. It is automatically generated by Lex.

Dept. of CSE,AJIET,Mangaluru Page 12


SS Lab 18CSL66

yywrap() This function is called when end of file (or input) is encountered. If this
function returns 1, the parsing stops. So, this can be used to parse multiple
files. Code can be written in the third section, which will allow multiple
files to be parsed. The strategy is to make yyin file pointer (see the
preceding table) point to a different file until all the files are parsed. At the
end, yywrap() can return 1 to indicate end of parsing.

yyless(int n) This function can be used to push back all but first ‘n’ characters of the read
token.

yymore() This function tells the lexer to append the next token to the current token.

1.2.4 Regular Expressions


It is used to describe the pattern. It is widely used to in lex. It uses meta language. The character used
in this meta language are part of the standard ASCII character set. An expression is made up of
symbols. Normal symbols are characters and numbers, but there are other symbols that have special
meaning in Lex. The following two tables define some of the symbols used in Lex and give a few
typical examples.
Character Meaning

A-Z, 0-9, a-z Characters and numbers that form part of the pattern.

. Matches any character except \n.

- Used to denote range. Example: A-Z implies all characters from A to Z.

[] A character class. Matches any character in the brackets. If the first


character is ^ then it indicates a negation pattern. Example: [abC]
matches either of a, b, and C.

* Match zero or more occurrences of the preceding pattern.

+ Matches one or more occurrences of the preceding pattern.(no empty


string)

Ex: [0-9]+ matches “1”,”111” or “123456” but not an empty string.

? Matches zero or one occurrences of the preceding pattern.

Dept. of CSE,AJIET,Mangaluru Page 13


SS Lab 18CSL66

Character Meaning

Ex: -?[0-9]+ matches a signed number including an optional leading


minus.

$ Matches end of line as the last character of the pattern.

{} 1) Indicates how many times a pattern can be present. Example: A{1,3}


implies one to three occurrences of A may be present.

2) If they contain name, they refer to a substitution by that name.

Ex: {digit}

\ Used to escape meta characters. Also used to remove the special meaning
of characters as defined in this table.

Ex: \n is a newline character, while “\*” is a literal asterisk.

^ Negation.

| Matches either the preceding regular expression or the following regular


expression. Ex: cow|sheep|pig matches any of the three words.

"< symbols>" Literal meanings of characters. Meta characters hold.

/ Look ahead. Matches the preceding pattern only if followed by the


succeeding expression. Example: A0/1 matches A0 only if A01 is the
input.

() Groups a series of regular expressions together into a new regular


expression. Ex: (01) represents the character sequence 01. Parentheses
are useful when building up complex patterns with *,+ and |

Examples of regular expressions

Regular Meaning
expression

joke[rs] Matches either jokes or joker.

A{1,2}shis+ Matches AAshis, Ashis, AAshi, Ashi.

(A[b-e])+ Matches zero or one occurrences of A followed by any

Dept. of CSE,AJIET,Mangaluru Page 14


SS Lab 18CSL66

Regular Meaning
expression

character from b to e.

[0-9] 0 or 1 or 2 or………9

[0-9]+ 1 or 111 or 12345 or …At least one occurrence of preceding


exp

[0-9]* Empty string (no digits at all) or one or more occurrence.

-?[0-9]+ -1 or +1 or +2 …..

[0.9]*\.[0.9]+ 0.0,4.5 or .31415 But won’t match 0 or 2

Examples of token declarations

Token Associated expression Meaning

Number ([0-9])+ 1 or more occurrences of a


digit

Chars [A-Za-z] Any character

Blank "" A blank space

Word (chars)+ 1 or more occurrences of


chars

Variable (chars)+(number)*(chars)*(
number)*

Dept. of CSE,AJIET,Mangaluru Page 15


SS Lab 18CSL66

2. 1 INTRODUCTION TO YACC
YACC provides a general tool for imposing structure on the input to a computer program. The
input specification is a collection of grammar rules. Each rule describes an allowable structure and
gives it a name. YACC prepares a specification of the input process.YACC generates a function to
control the input process. This function is called a parser.
The name is an acronym for “Yet Another Compiler Compiler”. YACC generates the code for
the parser in the C programming language. YACC was developed at AT& T for the Unix operating
system. YACC has also been rewritten for other languages, including Java, Ada.
The function parser calls the lexical analyzer to pick up the tokens from the input stream.
These tokens are organized according to the input structure rules .The input structure rule is called as
grammar. When one of the rule is recognized, then user code supplied for this rule ( user code is
action) is invoked. Actions have the ability to return values and makes use of the values of other
actions.

When we run YACC, it generates a parser in file y.tab.c and also creates an include file y.tab.h. To
obtain tokens, YACC calls yylex. Function yylex has a return type of int, and returns the token.
Values associated with the token are returned by lex in variable yylval.

Dept. of CSE,AJIET,Mangaluru Page 16


SS Lab 18CSL66

2.2 Structure of YACC source program:


Basic Specification:

Every YACC specification file consists of three sections. The declarations, Rules (of
grammars), programs. The sections are separated by double percent “%%” marks. The % is
generally used in YACC specification as an escape character.

The general format for the YACC file is very similar to that of the Lex file.
{definitions}
%%
{rules}
%%
{user subroutines}
%% is a delimiter to the mark the beginning of the Rule section

Definition Section

%union It defines the Stack type for the Parser. It is a union of various datas/structures/ Objects

%token These are the terminals returned by the yylex function to the YACC. A token can also have
type associated with it for good type checking and syntax directed translation. A type of a
token can be specified as %token <stack member>tokenName.
Ex: %token NAME NUMBER

%type The type of a non-terminal symbol in the Grammar rule can be specified with this. The
format is %type <stack member>non-terminal.
%no Specifies that there is no associatively of a terminal symbol.
assoc
%left Specifies the left associatively of a Terminal Symbol

%right Specifies the right associatively of a Terminal Symbol.

%start Specifies the L.H.S non-terminal symbol of a production rule which should be taken as the
starting point of the grammar rules.

Dept. of CSE,AJIET,Mangaluru Page 17


SS Lab 18CSL66

%prec Changes the precedence level associated with a particular rule to that of the following token
name or literal

Rules Section
The rules section simply consists of a list of grammar rules. A grammar rule has the form:
A: BODY
A represents a nonterminal name, the colon and the semicolon are YACC punctuation and BODY
represents names and literals. The names used in the body of a grammar rule may represent tokens or
nonterminal symbols. The literal consists of a character enclosed in single quotes.
Names representing tokens must be declared as follows in the declaration sections:
%token name1 name2…

Every name not defined in the declarations section is assumed to represent a nonterminal
symbol. Every non-terminal symbol must appear on the left side of at least one rule. Of all the no
terminal symbols, one, called the start symbol has a particular importance. The parser is designed to
recognize the start symbol. By default the start symbol is taken to be the left hand side of the first
grammar rule in the rules section.
With each grammar rule, the user may associate actions to be. These actions may return values,
and may obtain the values returned by the previous actions. Lexical analyzer can return values for tokens,
if desired. An action is an arbitrary C statement. Actions are enclosed in curly braces.

Dept. of CSE,AJIET,Mangaluru Page 18


SS Lab 18CSL66

PROGRAM-1a

TITLE Write a LEX program to recognize valid arithmetic expression. Identifiers in the expression
could be only integers and operators could be + and *. Count the identifiers & operators present and
print them separately.

AIM To check the valid arithmetic expression and Count the identifiers & operators present.

DESCRIPTION
In this program we need to check whether inputted expression is valid or not, then we are counting the
number of identifiers and operator. Ex: (2+3) it is a valid expression with two identifiers and one
operator.

INPUT
Valid arithmetic expression

EXPECTED OUTPUT
a) Print given expression valid or invalid
b) Print the count of Identifier and Operator in the given expression.

Lex PROGRAM

%{
#include<stdio.h>
int identifiers=0,operators=0,braces=0,alpha=0,spaces=0;
%}
%%
[a-zA-Z] {alpha++;printf("\nAlphabet:");ECHO;}
[0-9]* {identifiers++;printf("\nIdentifier:");ECHO;}
[+\*] {operators++;printf("\nOperator:");ECHO;}
"(" {braces++;}
")" {braces--;}
[ ] {spaces++;}
. {;}
%%
void main()
{
printf("Enter the expression:");
Dept. of CSE,AJIET,Mangaluru Page 19
SS Lab 18CSL66

yylex();
if((braces==0) && (operators==identifiers-1) && alpha==0 && spaces==0)
{
printf("\nValid expression");
printf("\nNumber of operators:%d",operators);
printf("\nNumber of identifiers:%d\n",identifiers);
}
else
printf("\nInvalid expression");
printf("\n");
}

int yywrap()
{
return 1;
}

RESULT

Viva Questions
Define system software.
System software is computer software designed to operate the computer hardware and to provide a
platform for running application software. Eg: operating system, assembler, and loader.
Dept. of CSE,AJIET,Mangaluru Page 20
SS Lab 18CSL66

What is an Assembler?
Assembler for an assembly language, a computer program to translate between lower-level
representations of computer programs.

Explain lex and yacc tools

Lex: - scanner that can identify those tokens


Yacc: - parser.yacc takes a concise description of a grammar and produces a C routine that can
parse that grammar.

PROGRAM -1b
TITLE
Write YACC program to evaluate arithmetic expression involving operators: +, -,
*, and /
AIM
Evaluate arithmetic expression
DESCRIPTION
YACC cannot represent numbers as [0-9]+ nor easily obtain the corresponding value, nor can it easily
be used to ignore white space and comments. Therefore, we need to use both LEX and
YACC together; LEX for the simple parts (e.g. numbers, white space, comments) and YACC for more
complex parts (e.g. expressions).

y.tab.h gives LEX the names and type declarations etc. from YACC

yylval name used for values set in LEX e.g. yylval.a_number = atoi (yytext);

%token declare each grammar rule used by YACC that is recognised by LEX and give type of

Dept. of CSE,AJIET,Mangaluru Page 21


SS Lab 18CSL66

value

$$ resulting value for any part of the grammar

$1, $2, etc. values from sub-parts of the grammar

yyparse routine created by YACC from (expected input, action) lists.


(It actually returns a value indicating if it failed to recognise the input.)

yylex routine called by yyparse for all its input.


We are using getchar, which just reads characters from the input.

yyerror routine called by yyparse whenever it detects an error in its input.

INPUT
Arithmetic expression is given as input

EXPECTED OUTPUT
Evaluate result is printed

PROGRAM

Lex program

%{
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[+\-\*\/] {return yytext[0];}
"(" {return yytext[0];}
")" {return yytext[0];}
"\n" {return ENTER;}
"." {return yytext[0];}
%%
int yywrap()
{
Dept. of CSE,AJIET,Mangaluru Page 22
SS Lab 18CSL66

return 1;
}

Yacc program

%{
#include<stdio.h>
#include<stdlib.h>
int yylex();
int yyerror();
%}
%token NUM ENTER
%left '+' '-'
%left '*' '/'
%%
input: expr ENTER {printf("Result is %d\n",$$);exit(0);}
expr: expr '+' expr {$$=$1+$3;}
|expr '-' expr {$$=$1-$3;}
|expr '*' expr {$$=$1*$3;}
|expr '/' expr {if($3==0) {printf("Divide by zero error");exit(0);}else $$=$1/$3;}
|'(' expr ')' {$$=$2;}
|NUM {$$=$1;}
;
%%
void main()
{
printf("Enter the expression\n");
yyparse();
}
int yyerror()
{
printf("Invalid expression\n");
return 1;
}

Dept. of CSE,AJIET,Mangaluru Page 23


SS Lab 18CSL66

RESULT

Viva Questions
1. What is the Syntax of a Language?
The Rules which tells whether a string is a valid Program or not are called the Syntax. 

 What is the Semantics of a Language?


The Rules which gives meaning to programs are called the Semantics of a Language. 

 What are tokens?


When a string representing a program is broken into sequence of substrings, such that each
substring represents a constant, identifier, operator, keyword etc of the language, these substrings are
called the tokens of the Language.

Dept. of CSE,AJIET,Mangaluru Page 24


SS Lab 18CSL66

PROGRAM-2
TITLE
Develop, Implement and Execute a program using YACC tool to recognize all strings ending with b
preceded by n a’s using the grammar an b (note: input n value).

AIM

To recognize all strings ending with b preceded by n a’s


DESCRIPTION
A grammar G can be formally written as a 4-tuple (N, T, S, P)
Where
 N or VN is a set of variables or non-terminal symbols
 T or  is a set of Terminal symbols
 S is a special variable called the Start symbol, S ∈ N
 P is Production rules for Terminals and Non-terminals. A production rule has the form → , where  and
 are strings on ∪ Σ and least one symbol of  belongs to VN
Example Grammar G1:
({S, A, B}, {a, b}, S, {S →AB, A →a, B →b})
Here, S, A, and B are Non-terminal symbols;
a and b are Terminal
symbols S is the Start symbol, S ∈ N
Productions, P : S →AB, A →a, B →b

INPUT
Input n value {n=0,1 ,2 ,3 etc b, ab, aab, aaab etc}

EXPECTED OUTPUT
Print the recognized string

PROGRAM
Lex Program

%{
#include"y.tab.h"
%}
%%
a {return A;}
b {return B;}
"\n" {return ENTER;}
. {printf("Invalid input");exit(0);}
%%
Dept. of CSE,AJIET,Mangaluru Page 25
SS Lab 18CSL66

int yywrap()
{
return 1;
}
Yacc program
%{
#include<stdio.h>
#include<stdlib.h>
int yylex();
int yyerror();
%}
%token A B ENTER
%%
input:S ENTER {printf("Correct grammar");exit(0);}
S:C B |B
C:A C|A
;
%%
void main()
{
printf("enter the string");
yyparse();
}
int yyerror()
{
printf("INVALID grammar");
return 1;
}

Dept. of CSE,AJIET,Mangaluru Page 26


SS Lab 18CSL66

RESULT

Viva Questions
1.What is the Lexical Analysis?

The Function of a lexical Analyzer is to read the input stream representing the Source
program, one character at a time and to translate it into valid tokens.

 How can we represent a token in a language?


The Tokens in a Language are represented by a set of Regular Expressions. A regular expression
specifies a set of strings to be matched. It contains text characters and operator characters. The
Advantage of using regular expression is that a recognizer can be automatically generated.

 How are the tokens recognized?


The tokens which are represented by an Regular Expressions are recognized in an input string by
means of a state transition Diagram and Finite Automata.

Dept. of CSE,AJIET,Mangaluru Page 27


SS Lab 18CSL66

PROGRAM-3
TITLE
Design, develop and implement YACC/C program to construct Predictive / LL(1) Parsing Table for
the grammar rules: A aBa , B bB | . Use this table to parse the
sentence: abba$.

AIM
To construct parsing Table and parse the given string
DESCRIPTION
The types of top-down parsing are depicted below:

Predictive Parser
Predictive parser is a recursive descent parser, which has the capability to predict which production is
to be used to replace the input string. The predictive parser does not suffer from backtracking.

To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to the next
input symbols. To make the parser back-tracking free, the predictive parser puts some constraints on
the grammar and accepts only a class of grammar known as LL(k) grammar

Predictive parsing uses a stack and a parsing table to parse the input and generate a parse tree. Both the
stack and the input contains an end symbol $ to denote that the stack is empty and the input is consumed.
The parser refers to the parsing table to take any decision on the input and stack element combination.

Dept. of CSE,AJIET,Mangaluru Page 28


SS Lab 18CSL66

LL Parser
An LL Parser accepts LL grammar. LL grammar is a subset of context-free grammar but with some
restrictions to get the simplified version, in order to achieve easy implementation. LL grammar can be
implemented by means of both algorithms namely, recursive-descent or table-driven.

LL parser is denoted as LL(k). The first L in LL(k) is parsing the input from left to right, the second L
in LL(k) stands for left-most derivation and k itself represents the number of look aheads. Generally k
= 1, so LL(k) may also be written as LL(1)

INPUT
Enter the string

EXPECTED OUTPUT
Display the predictive table and Parsing of given string.

PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char prod[3][15]={"A->aBa","B->bB","B->@"};

Dept. of CSE,AJIET,Mangaluru Page 29


SS Lab 18CSL66

char table[2][3][3]={{"aBa"," "," "},{"@","bB"," "}};


int size[2][3]={3,0,0,1,2,0},n;
char s[20],stack[20];
void display(int i,int j)
{
int k;
for(k=0;k<=i;k++)
printf("%c",stack[k]);
printf(" ");
for(k=j;k<=n;k++)
printf("%c",s[k]);
printf("\n");
}
void main()
{
int i,j,k,row,col,flag=0;
printf("\nThe grammar is:\n");
for(i=0;i<3;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is:\n");
printf("\ta \tb \t$ \n");
printf("_________________________________\n");
for(i=0;i<2;i++)
{
if(i==0)
printf("A");
else
printf("B");
for(j=0;j<3;j++)
{
printf("\t%s",table[i][j]);
}
printf("\n");
}//end for
printf("\nEnter the string:");
scanf("%s",s);
strcat(s,"$");
n=strlen(s);
stack[0]='$';
stack[1]='A';
Dept. of CSE,AJIET,Mangaluru Page 30
SS Lab 18CSL66

i=1;
j=0;
printf("\nStack input");
printf("\n____________________________________________\n");
while(1)
{
if(stack[i]==s[j])
{//first if
i--;
j++;
if(stack[i]=='$' && s[j]=='$')
{//second if
printf("$$\nSuccess\n");
break;
}// second end if
else
if(stack[i]=='$' && s[j]!='$')
{//third if
printf("Error\n");
break;
}//end third if
display(i,j);
}//end first if
switch(stack[i])
{
case 'A':row=0;break;
case 'B':row=1;break;
}//end switch
switch(s[j])
{
case 'a':col=0;break;
case 'b':col=1;break;
case '$':col=2;break;
}// end switch
if(table[row][col][0]=='\0')
{
printf("\nError\n");
break;
}// end if
else if(table[row][col][0]=='@')
Dept. of CSE,AJIET,Mangaluru Page 31
SS Lab 18CSL66

{
i--;
display(i,j);
}//end elseif
else
{
for(k=size[row][col]-1;k>=0;k--)
{
stack[i]=table[row][col][k];
i++;
}
i--;
display(i,j);
}// end else
} //end while
}//end main

RESULT

Dept. of CSE,AJIET,Mangaluru Page 32


SS Lab 18CSL66

Viva Questions
 Are Lexical Analysis and Parsing two different Passes?
These two can form two different passes of a Parser. The Lexical analysis can store all the
recognized tokens in an intermediate file and give it to the Parser as an input. However it is more
convenient to have the lexical Analyzer as a co routine or a subroutine which the Parser calls
whenever it requires a token.

2. What are the Parse Trees?


Parse trees are the Graphical representation of the grammar which filters out the choice for
replacement order of the Production rules.

3. What are Ambiguous Grammars?


A Grammar that produces more than one Parse Tree for the same sentences or the Production
rules in a grammar is said to be ambiguous.

Dept. of CSE,AJIET,Mangaluru Page 33


SS Lab 18CSL66

PROGRAM-4
TITLE
Design, develop and implement YACC/C program to demonstrate Shift Reduce Parsing technique for
the grammar rules: E E+T | T, T T*F | F, F (E) | id and parse the sentence: id + id * id.
AIM
To demonstrate Shift Reduce Parsing technique
DESCRIPTION

Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it reaches
the root node. Here, we start from a sentence and then apply production rules in reverse manner in
order to reach the start symbol. The image given below depicts the bottom-up parsers available.

Shift-Reduce Parsing
Shift-reduce parsing uses two unique steps for bottom-up parsing. These steps are known as shift-
step and reduce-step.

Shift step: The shift step refers to the advancement of the input pointer to the next input symbol,
which is called the shifted symbol. This symbol is pushed onto the stack. The shifted symbol is
treated as a single node of the parse tree.

Dept. of CSE,AJIET,Mangaluru Page 34


SS Lab 18CSL66

Reduce step : When the parser finds a complete grammar rule (RHS) and replaces it to (LHS), it is
known as reduce-step. This occurs when the top of the stack contains a handle. To reduce, a POP
function is performed on the stack which pops off the handle and replaces it with LHS non-terminal
symbol.
INPUT
Enter the string to be parsed {id+id*id}

EXPECTED OUTPUT

Display Stack, input and action and Parsing of inputted string.


PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
void main()
{
printf("GRAMMAR is E->E+T|T \n T->T*F|F \n F->(E) \n F->ID");
printf("Enter input string");
//gets(a);
scanf("%s",a);
c=strlen(a);
strcpy(act,"SHIFT->");
printf("stack \t input \t action");
for(k=0,i=0;j<c;k++,i++,j++)
{
if(a[j]=='i' && a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
check();
}
Dept. of CSE,AJIET,Mangaluru Page 35
SS Lab 18CSL66

else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}// end else
}// end for
}// end main
//--------------------------------------------------------------------------------------------
void check()
{
strcpy(ac,"REDUCE");
//first for
for(z=0;z<c;z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{
stk[z]='F';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
//2nd for
for(z=0;z<c;z++)
if(stk[z]=='i' && stk[z+1]=='d')
{
stk[z]='F';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
//3rd for
for(z=0;z<c;z++)
{
if(stk[z]=='T' && stk[z+1]=='*' && stk[z+2]=='F')
{
stk[z]='T';
stk[z+1]='\0';
Dept. of CSE,AJIET,Mangaluru Page 36
SS Lab 18CSL66

stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
else if(stk[z]=='F')
{
stk[z]='T';
printf("\n$%s\t%s$\t%s",stk,a,ac);
}
}
//4th for
for(z=0;z<c;z++)
{
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='T' && stk[z+3]=='*')break;
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='T')
{
if(a[j+1]=='*')break;

else
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}//end else
}// end if
else if(stk[z]=='T')
{
stk[z]='E';
printf("\n$%s\t%s$\t%s",stk,a,ac);
}
}//for ends
}//check()

RESULT

Dept. of CSE,AJIET,Mangaluru Page 37


SS Lab 18CSL66

Viva Questions
1.What is shift reduce parsing in compiler?
A shift-reduce parser is a class of efficient, table-driven bottom-up parsing methods for computer
languages and other notations formally defined by a grammar.

 What is shift reduce conflict?


A message from yacc indicating a “conflict” means that at some point in the course of generating a
parser for its input grammar, it faced a choice between multiple possible actions for the parser to take.
Shift-reduce conflict.

 What is bottom up parsing approach?


Bottom up parsing can be defined as an attempt to reduce the input string w to the start symbol of a
grammar by tracing out the rightmost derivations of w in reverse.
4.What is LR 0 Parser?
Both LR(0) and SLR(1) are shift/reduce parsers, meaning that they process the tokens of the input
stream by placing them on a stack, and at each point either shifting a token by pushing it onto the
stack or reducing some sequence of terminals and nonterminals atop the stack back to some
nonterminal symbol.
Dept. of CSE,AJIET,Mangaluru Page 38
SS Lab 18CSL66

PROGRAM-5
TITLE
Design, develop and implement a C/Java program to generate the machine code using Triples for the
statement A = -B * (C +D) whose intermediate code in three-address form:
T1 = -B
T2 = C + D
T3 = T1 + T2
A = T3

AIM
To generate the machine code using Triples
DESCRIPTION
Three-Address Code
Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the form
of an annotated syntax tree. That syntax tree then can be converted into a linear representation, e.g.,
postfix notation. Intermediate code tends to be machine independent code. Therefore, code generator
assumes to have unlimited number of memory storage (register) to generate code.
For example:
a=b+c*d; {Regular Expression}
The intermediate code generator will try to divide this expression into sub-expressions and then
generate the corresponding code.
t1=c*d;
t2=b+t1;
a=t2;
A three-address code has at most three address locations to calculate the expression. A three-address
code can be represented in two forms quadruples and triples.

Triples
Each instruction in triples presentation has three fields : op, arg1, and arg2.The results of respective
sub-expressions are denoted by the position of expression. Triples represent similarity with DAG and
syntax tree. They are equivalent to DAG while representing expressions

Dept. of CSE,AJIET,Mangaluru Page 39


SS Lab 18CSL66

INPUT
A text file is inputted which contains three address code
EXPECTED OUTPUT
Print the machine code

PROGRAM
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
char a[20];
int i,j=0;
FILE *f;
f=fopen("z.txt","r");
while(fscanf(f,"%s",a)!=EOF)
{
if(a[3]=='-')//first if
{
printf("MOV R%d,%c\n",j,a[4]);
printf("NEG R%d\n",j);
j++;
}
if(isalpha(a[3]))//second if
{
if(isdigit(a[4]))
{
if(isdigit(a[5]=='+'))
printf("ADD R%d,R%d,R%d\n",j,j-4,j-1);
Dept. of CSE,AJIET,Mangaluru Page 40
SS Lab 18CSL66

else if(a[5]=='-')
printf("SUB R%d,R%d,R%d\n",j,j-4,j-1);
else if(a[5]=='*')
printf("MUL R%d,R%d,R%d\n",j,j-4,j-1);
else if(a[5]=='/')
printf("DIV R%d,R%d,R%d\n",j,j-4,j-1);
}
else
{
if(a[4]=='+')
{
printf("MOV R%d,%c\n",j++,a[3]);
printf("MOV R%d,%c\n",j++,a[5]);
printf("ADD R%d,R%d,R%d\n",j,j-2,j-1);
}
if(a[4]=='-')
{
printf("MOV R%d,%c\n",j++,a[3]);
printf("MOV R%d,%c\n",j++,a[5]);
printf("SUB R%d,R%d,R%d\n",j,j-2,j-1);
}
else if(a[4]=='*')
{
printf("MOV R%d,%c\n",j++,a[3]);
printf("MOV R%d,%c\n",j++,a[5]);
printf("MUL R%d,R%d,R%d\n",j,j-2,j-1);
}
else if(a[4]=='/')
{
printf("MOV R%d,%c\n",j++,a[3]);
printf("MOV R%d,%c\n",j++,a[5]);
printf("DIV R%d,R%d,R%d\n",j,j-2,j-1);
}
j++;
}//end else
}//end isalpha
if(isdigit(a[3]))//third if
{
printf("MOV %c,R%d\n",a[0],j);
}//end while
Dept. of CSE,AJIET,Mangaluru Page 41
SS Lab 18CSL66

}//end main

RESULT

Viva Questions
1.What is token in compiler?
A lexeme is a sequence of characters in the source program that matches the pattern for a token and is
identified by the lexical analyzer as an instance of that token.A token is a pair consisting of
a token name and an optional attribute value.

 What is intermediate code generation?


In computing, code generation is the process by which a compiler's code generator converts
some intermediate representation of source code into a form (e.g., machine code) that can be
readily executed by a machine.

3.What is code optimization?


Code optimization is any method of code modification to improve code quality and efficiency.

4.What is three address code in compiler?


Three-address code (often abbreviated to TAC or 3AC) is an intermediate code used by optimizing
compilers to aid in the implementation of code-improving transformations. Each TAC instruction has
at most three operands and is typically a combination of assignment and a binary operator.

Dept. of CSE,AJIET,Mangaluru Page 42


SS Lab 18CSL66

PROGRAM-6a
TITLE
Write a LEX program to eliminate comment lines in a C program and copy the resulting program into
a separate file.
AIM
to eliminate comment lines in a C program

DESCRIPTION

The given lex program eliminates the comments and display the comment count.

INPUT
Input will be simple c program with single and multi-line comments

EXPECTED OUTPUT
Display the c program without comments and print the comment count.

Lex PROGRAM 6a
%{
#include<stdio.h>
int comment1=0,comment2=0,i=0;
%}
%%
[/][/].* {if(i==0)comment1++;}
[/][*] {if(i==0)i++;}
[*][/] {if(i==1)i--;comment2++;}
. {if(i==0)ECHO;}
%%
void main()
{
yyin=fopen("program.c","r");
yyout=fopen("output.txt","w");
yylex();
printf("Single line comments:%d\n",comment1);
printf("Multi line comments:%d\n",comment2);
printf("Total number of comments:%d\n",comment1+comment2);
}
int yywrap()
{
return 1;

Dept. of CSE,AJIET,Mangaluru Page 43


SS Lab 18CSL66

}
RESULT

Input File consisting coments Output after eliminating the comments

Output comment count

Viva Questions
1. Give the structure of the lex program.
definition section- any intitial ‘c’ program code
%%
Rules section- pattern and action separated by white space
%%
User subroutines section-concsit of any legal code.

2. Explain yyleng?
yyleng-contains the length of the string our lexer recognizes.

3. What is exit status command?


Exit 0- return success, command executed successfully. Exit 1 – return failure.

Dept. of CSE,AJIET,Mangaluru Page 44


SS Lab 18CSL66

PROGRAM-6b
TITLE
Write YACC program to recognize valid identifier, operators and keywords in the given text (C
program) file.
AIM
To recognize valid identifier, operators and keywords

DESCRIPTION

y.tab.h gives LEX the names and type declarations etc. from YACC

yylval name used for values set in LEX e.g. yylval.a_number = atoi (yytext);

%token declare each grammar rule used by YACC that is recognised by LEX and give type of
value

atoi Convert string to integer

yyparse routine created by YACC from (expected input, action) lists.


(It actually returns a value indicating if it failed to recognise the input.)

yylex routine called by yyparse for all its input.


We are using getchar, which just reads characters from the input.

yyerror routine called by yyparse whenever it detects an error in its input.

INPUT
Input will be c program file

EXPECTED OUTPUT
1. Print the identifier, operator and keywords present in the c file inputted
2. Print the count of identifier, operator and keywords present in the c file inputted

PROGRAM 6b
Lex program

%{
#include<stdio.h>

Dept. of CSE,AJIET,Mangaluru Page 45


SS Lab 18CSL66

#include "y.tab.h"
extern int yylval;
%}
%%
[+\-\*\/] {printf("Operator:%s\n",yytext);return OP;}
[0-9]+ {yylval = atoi(yytext); printf("Digit is %d\n",yylval); return DIGIT;}
"void"|"int"|"main"|"printf" {printf("Keyword:%s\n",yytext);return KEY;}
[a-zA-Z]+ {printf("Identifier:%s\n",yytext);return ID;}
.;
%%

int yywrap()
{
return 1;
}

Yacc Program

%{
#include<stdio.h>
#include<stdlib.h>
int dig=0,id=0,op=0,key=0;
extern FILE *yyin;
int yylex();
int yyerror();
%}
%token DIGIT ID KEY OP
%%
input:DIGIT input {dig++;}
|ID input {id++;}
|OP input {op++;}
|KEY input {key++;}
|DIGIT {dig++;}
|ID {id++;}
|OP {op++;}
|KEY {key++;}
;
%%
//#include<stdio.h>

Dept. of CSE,AJIET,Mangaluru Page 46


SS Lab 18CSL66

void main()
{
yyin=fopen("sample.c","r");
yyparse();
printf("Key count=%d\n",key);
printf("Operator count=%d\n",op);
printf("Identifier count=%d\n",id);
printf("Digit count=%d\n",dig);
}
int yyerror()
{
printf("INVALID");
return 1;
}

RESULT

Sample.c

Dept. of CSE,AJIET,Mangaluru Page 47


SS Lab 18CSL66

Viva Questions
1. What is identifier?
An Identifier can only have alphanumeric characters ( a-z , A-Z , 0-9 ) and underscore( _ ).
The first character of an identifier can only contain alphabet ( a-z , A-Z ) or underscore ( _ ).

2. What are keywords?


A keyword is a word that is reserved by a program because the word has a special meaning.

3. What are the phases of compiler?


Lexical Analysis. The first phase of scanner works as a text scanner.
Syntax Analysis. The next phase is called the syntax analysis or parsing.
Semantic Analysis.
Intermediate Code Generation.
Code Optimization.
Code Generation.
Symbol Table.

Dept. of CSE,AJIET,Mangaluru Page 48


SS Lab 18CSL66

PROGRAM-7
TITLE
Design, develop and implement a C/C++/Java program to simulate the working of Shortest remaining
time and Round Robin (RR) scheduling algorithms. Experiment with different quantum sizes for RR
algorithm.

AIM
To simulate the working of shortest remaining time and Round Robin (RR) scheduling algorithms

DESCRIPTION
Shortest remaining time scheduling algorithm:
Shortest remaining time, also known as shortest remaining time first (SRTF), is a scheduling method
that is a preemptive version of shortest job next scheduling. In this scheduling algorithm, the process
with the smallest amount of time remaining until completion is selected to execute. Since the currently
executing process is the one with the shortest amount of time remaining by definition, and since that
time should only reduce as execution progresses, processes will always run until they complete or a
new process is added that requires a smaller amount of time.
Shortest remaining time is advantageous because short processes are handled very quickly. The
system also requires very little overhead since it only makes a decision when a process completes or a
new process is added, and when a new process is added the algorithm only needs to compare the
currently executing process with the new process, ignoring all other processes currently waiting to
execute.
Like shortest job first, it has the potential for process starvation; long processes may be held off
indefinitely if short processes are continually added.
Round Robin (RR) scheduling algorithm:
Round-robin (RR) is one of the algorithms employed by process and network schedulers in
computing. As the term is generally used, time slices (also known as time quanta) are assigned to each
process in equal portions and in circular order, handling all processes without priority (also known as
cyclic executive). Round-robin scheduling is simple, easy to implement, and starvation-free. Round-
robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in
computer networks. It is an operating system concept.

Dept. of CSE,AJIET,Mangaluru Page 49


SS Lab 18CSL66

INPUT
1. Enter the choice
2. Enter the quantum time
3. Enter the Burst time

EXPECTED OUTPUT
Display the Average waiting and Turn-around Time

PROGRAM
#include<stdio.h>
struct proc
{
int id;
int arrival;
int burst;
int rem;
int wait;
int finish;
int turnaround;
float ratio;
}process[10]; //structure to hold the process information
struct proc temp;
int no;
int chkprocess(int);
int nextprocess();
void roundrobin(int, int, int[], int[]);
void srtf(int);

int main()

{
int n,tq,choice;
int bt[10],st[10],i,j,k;
for(; ;)
{
printf("Enter the choice \n");
printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n");
scanf("%d",&choice);
switch(choice)
Dept. of CSE,AJIET,Mangaluru Page 50
SS Lab 18CSL66

{
case 1:
printf("Round Robin scheduling algorithm\n");
printf("Enter number of processes:\n");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i]; //service time
}
printf("Enter time quantum:");
scanf("%d",&tq);
roundrobin(n,tq,st,bt);
break;
case 2:
printf("\n \n ---SHORTEST REMAINING TIME NEXT---\n \n ");
printf("\n \n Enter the number of processes: ");
scanf("%d", &n);
srtf(n);
break;
case 3: break;
}// end of switch
}// end of for
}//end of main()

void roundrobin(int n,int tq,int st[],int bt[])

int time=0;
int tat[10],wt[10],i,count=0,swt=0,stat=0,temp1,sq=0,j,k;
float awt=0.0,atat=0.0;
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp1=tq;
if(st[i]==0) // when service time of a process equals zero then
//count value is incremented
Dept. of CSE,AJIET,Mangaluru Page 51
SS Lab 18CSL66

{
count++;
continue;
}
if(st[i]>tq) // when service time of a process greater than time
//quantum then time
st[i]=st[i]-tq; //quantum value subtracted from service time
else
if(st[i]>=0)
{
temp1=st[i]; // temp1 stores the service time of a process
st[i]=0; // making service time equals 0
}
sq=sq+temp1; // utilizing temp1 value to calculate turnaround time
tat[i]=sq; // turn around time
} //end of for
if(n==count) // it indicates all processes have completed their task because the count value
break; // incremented when service time equals 0
} //end of while
for(i=0;i<n;i++) // to calculate the wait time and turnaround time of each process
{
wt[i]=tat[i]-bt[i]; // waiting time calculated from the turnaround time - burst time
swt=swt+wt[i]; // summation of wait time
stat=stat+tat[i]; // summation of turnaround time
}
awt=(float)swt/n; // average wait time
atat=(float)stat/n; // average turnaround time
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f\n Avg turn around time is %f\n",awt,atat);
}// end of Round Robin

int chkprocess(int s) // function to check process remaining time is zero or not

{
int i;
for(i = 1; i <= s; i++)
{
if(process[i].rem != 0)
Dept. of CSE,AJIET,Mangaluru Page 52
SS Lab 18CSL66

return 1;
}
return 0;
} // end of chkprocess

int nextprocess() // function to identify the next process to be executed

{
int min, l, i;
min = 32000; //any limit assumed
for(i = 1; i <= no; i++)
{
if( process[i].rem!=0 && process[i].rem < min)
{
min = process[i].rem;
l = i;
}
}
return l;
} // end of nextprocess

void srtf(int n)

{
int i,j,k,time=0;
float tavg,wavg;
for(i = 1; i <= n; i++)
{
process[i].id = i;
printf("\n\nEnter the arrival time for process %d: ", i);
scanf("%d", &(process[i].arrival));
printf("Enter the burst time for process %d: ", i);
scanf("%d", &(process[i].burst));
process[i].rem = process[i].burst;
}
for(i = 1; i <= n; i++)
{
for(j = i + 1; j <= n; j++)
{
Dept. of CSE,AJIET,Mangaluru Page 53
SS Lab 18CSL66

if(process[i].arrival > process[j].arrival) // sort arrival time of a process


{
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
no = 0;
j = 1;
while(chkprocess(n) == 1)
{
if(process[no + 1].arrival == time)
{
no++;
if(process[j].rem==0)
process[j].finish=time;
j = nextprocess();
}
if(process[j].rem != 0) // to calculate the waiting time of a process
{
process[j].rem--;
for(i = 1; i <= no; i++)
{
if(i != j && process[i].rem != 0)
process[i].wait++;
}
}
else
{

process[j].finish = time;
j=nextprocess();
time--;
k=j;
}

time++;
}//end of while
Dept. of CSE,AJIET,Mangaluru Page 54
SS Lab 18CSL66

process[k].finish = time;
printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---");
printf("\n\n Process Arrival Burst Waiting Finishing turnaround Tr/Tb \n");
printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time", "time", "time");
for(i = 1; i <= n; i++)
{
process[i].turnaround = process[i].wait + process[i].burst; // calc of turnaround
process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;
printf("%5d %8d %7d %8d %10d %9d %10.1f ", process[i].id, process[i].arrival,
process[i].burst, process[i].wait, process[i].finish, process[i].turnaround,
process[i].ratio);
tavg=tavg+ process[i].turnaround; //summation of turnaround time
wavg=wavg+process[i].wait; // summation of waiting time
printf("\n\n");
}
tavg=tavg/n; // average turnaround time
wavg=wavg/n; // average wait time
printf("tavg=%f\t shortest remaining time scheduling=%f\n",tavg,wavg);

}// end of srtf

RESULT

Dept. of CSE,AJIET,Mangaluru Page 55


SS Lab 18CSL66

Viva Questions
1. Explain the main purpose of an operating system?
Operating systems exist for two main purposes. One is that it is designed to make sure a computer
system performs well by managing its computational activities. Another is that it provides an
environment for the development and execution of programs.

2. What is kernel?
A kernel is the core of every operating system. It connects applications to the actual processing of
data..

3. What is RR scheduling algorithm?


RR (round-robin) scheduling algorithm is primarily aimed for time-sharing systems. A circular
queue is a setup in such a way that the CPU scheduler goes around that queue, allocating CPU to
each process for a time interval of up to around 10 to 100 milliseconds.

4. What are necessary conditions which can lead to a deadlock situation in a system?
Deadlock situations occur when four conditions occur simultaneously in a system: Mutual
exclusion; Hold and Wait; No preemption; and Circular wait.

Dept. of CSE,AJIET,Mangaluru Page 56


SS Lab 18CSL66

PROGRAM-8
TITLE
Design, develop and implement a C/C++/Java program to implement Banker’s algorithm. Assume
suitable input required to demonstrate the results..

AIM
To implement Banker’s algorithm

DESCRIPTION
Banker’s algorithm
The Banker's algorithm, sometimes referred to as the detection algorithm, is a resource allocation
and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the
allocation of predetermined maximum possible amounts of all resources, and then makes an "s-state"
check to test for possible deadlock conditions for all other pending activities, before deciding whether
allocation should be allowed to continue.
The algorithm was developed in the design process for the operating system and originally described
(in Dutch) in EWD108. When a new process enters a system, it must declare the maximum number of
instances of each resource type that it may ever claim; clearly, that number may not exceed the total
number of resources in the system. Also, when a process gets all its requested resources it must return
them in a finite amount of time.

INPUT

1. Enter the no process

2. Enter the no of resources

3. Enter Allocation, Max, Available Matrix

EXPECTED OUTPUT

Display the safe allocation of the process

PROGRAM
#include<stdio.h>

Dept. of CSE,AJIET,Mangaluru Page 57


SS Lab 18CSL66

void main()
{
intk=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz,
count=0;
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{ avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");

for(i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for(i=0;i<pno;i++)
{ P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}

printf("\nEnter the MAX matrix \n ");


for(i=0;i<rz;i++)
{ printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
Dept. of CSE,AJIET,Mangaluru Page 58
SS Lab 18CSL66

printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{ count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
P[++d]=P[i];
}//end for

if(d!=-1)
{ pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}// end main

RESULT

Dept. of CSE,AJIET,Mangaluru Page 59


SS Lab 18CSL66

Viva Questions
1. Why bankers algorithm are used?
Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is
used in banking systems to determine whether a loan can be granted or not.

2. What are deadlock avoidance algorithm?


Deadlock avoidance algorithms try not to allocate resources to a process if it will make the system in
an unsafe state. ... An edge from a process to resource is a request edge and an edge from a resource to
process is an allocation edge.

3. What is deadlock problem on OS?


Deadlock refers to a specific condition when two or more processes are each waiting for another to
release a resource, or more than two processes are waiting for resources in a circular chain .

Dept. of CSE,AJIET,Mangaluru Page 60


SS Lab 18CSL66

4. How does Bankers algorithm prevents deadlock?


The banker's algorithm is a resource allocation and deadlock avoidancealgorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all resources,
then makes an “s-state” check to test for possible activities, before deciding whether allocation should
be allowed to continue .

5.What is safe state in deadlock avoidance?


A state is safe if the system can allocate all resources requested by all processes ( up to their stated
maximums ) without entering a deadlock state. If a safe sequence does not exist, then the system is in
an unsafe state, which MAY lead to deadlock.

Dept. of CSE,AJIET,Mangaluru Page 61


SS Lab 18CSL66

PROGRAM-9
TITLE
Design, develop and implement a C/C++/Java program to implement page replacement algorithms
LRU and FIFO. Assume suitable input required to demonstrate the results.

AIM
To implement page replacement algorithms LRU and FIFO

DESCRIPTION
Page replacement algorithms LRU and FIFO:
In a computer operating system that uses paging for virtual memory management, page replacement
algorithms decide which memory pages to page out, sometimes called swap out, or write to disk,
when a page of memory needs to be allocated. Page replacement happens when a requested page is
not in memory (page fault) and a free page cannot be used to satisfy the allocation, either because
there are none, or because the number of free pages is lower than some threshold.
When the page that was selected for replacement and paged out is referenced again it has to be paged
in (read in from disk), and this involves waiting for I/O completion. This determines the quality of the
page replacement algorithm: the less time waiting for page-ins, the better the algorithm. A page
replacement algorithm looks at the limited information about accesses to the pages provided by
hardware, and tries to guess which pages should be replaced to minimize the total number of page
misses, while balancing this with the costs (primary storage and processor time) of the algorithm
itself. The page replacing problem is a typical online problem from the competitive analysis
perspective in the sense that the optimal deterministic algorithm is known.

INPUT
1. Enter the frame size.
2. Enter the No of Pages
3. Enter the Page no

EXPECTED OUTPUT
Display the page faults and page hit count

PROGRAM
Dept. of CSE,AJIET,Mangaluru Page 62
SS Lab 18CSL66

#include<stdio.h>
#include<stdlib.h>
void FIFO(char [ ],char [ ],int,int);
void lru(char [ ],char [ ],int,int);
int cnt;
int main()
{
int ch,YN=1,i,l,f;
char F[10],s[25];
printf("\n\n\tEnter the no of empty frames: ");
scanf("%d",&f);
printf("\n\n\tEnter the length of the string: ");
scanf("%d",&l);
printf("\n\n\tEnter the string: ");
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
printf("\n\n\t*********** MENU ***********");
printf("\n\n\t1:FIFO\n\n\t2:LRU\n\n\t3:EXIT");
printf("\n\n\tEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
F[i]=-1;
}
FIFO(s,F,l,f);
break;
case 2:
for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;
case 3:
Dept. of CSE,AJIET,Mangaluru Page 63
SS Lab 18CSL66

exit(0);
}
printf("\nNumber of page fault is %d",cnt);
printf("\nNumber of page hit is %d",(l-cnt));
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 :");
scanf("%d",&YN);
}
while(YN==1);
return(0);
}
//FIFO
void FIFO(char s[],char F[],int l,int f)
{
int i,j=0,k,flag=0;
cnt=0;
printf("\n\tPAGE\tFRAMES\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
printf("\tPage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
Dept. of CSE,AJIET,Mangaluru Page 64
SS Lab 18CSL66

{
printf(" %c",F[k]);
}
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}
//LRU
void lru(char s[],char F[],int l,int f)
{
int i,j=0,k,m,flag=0,top=0;
cnt=0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
{
flag=1;
break;
}
}
printf("\n\t%c\t",s[i]);
if(j!=f && flag!=1)
{
F[top]=s[i];
j++;
if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
Dept. of CSE,AJIET,Mangaluru Page 65
SS Lab 18CSL66

}
F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}
F[top]=s[i];
}
}
for(k=0;k<f;k++)
{
printf(" %c",F[k]);
}
if(flag==0)
{
printf("\tPage-fault%d",cnt);
cnt++;
}
else
printf("\tNo page fault");
flag=0;
}
}

RESULT

Dept. of CSE,AJIET,Mangaluru Page 66


SS Lab 18CSL66

Dept. of CSE,AJIET,Mangaluru Page 67


SS Lab 18CSL66

Viva Questions
1.What is FIFO page replacement algorithm?
First-In-First-Out (FIFO) Replacement. On a page fault, the frame that has been in memory the
longest is replaced. FIFO is not a stack algorithm. In certain cases, the number of page faults can
actually increase when more frames are allocated to the process.

2.What is least recently used algorithm?


The Least Recently Used (LRU) Page Replacement Algorithm. A good approximation to the
optimal algorithm is based on the observation that pages that have been heavily used in the last few
Dept. of CSE,AJIET,Mangaluru Page 68
SS Lab 18CSL66

instructions will probably be heavily usedagain in the next few. ... This strategy is called LRU (Least
Recently Used) paging.

3.What is a page in OS?


In computer operating systems, paging is a memory management scheme by which a computer stores
and retrieves data from secondary storage for use in main memory. In this scheme, the operating
system retrieves data from secondary storage in same-size blocks called pages.

4.What is page fault in OS?


An interrupt that occurs when a program requests data that is not currently in real memory. The
interrupt triggers the operating system to fetch the data from a virtual memory and load it into RAM.
An invalid page fault or page fault error occurs when the operating system cannot find the data in
virtual memory.

5.What is LRU page fault?


Any time a page is requested that isn't in memory, a page fault occurs. The LRUalgorithm
determines which page to throw out when memory is full.

Dept. of CSE,AJIET,Mangaluru Page 69

You might also like