Nba - CD Lab Manual
Nba - CD Lab Manual
LABORATORY MANUAL
VISION
To impart high quality professional training with an emphasis on basic principles of computer
science and engineering
To strengthen links with industry through partnerships and collaborative development works
To attain self-sustainability and overall development through research, consultancy and
development activities
To make the students as for as possible industry ready to enhance their employability in the
industries
To improve department industry collaboration through internship program and interaction with
professional society through seminar/workshops
MISSION
To enhance knowledge and skills of students in science, technology and humanbehavior that will
serve the nation.
To create an ambience for new idea, research, innovation and entrepreneurial attitude, with a
high level of ethics, communication and leadership qualities.
To develop ability and passion to work wisely, creatively, and effectively in each member of
college for the betterment of the mankind and all living beings
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an
engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for the public health
and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineeringandITtoolsincludingpredictionandmodelingtocomplexengineeringactivitieswith an understanding
of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent
and life-long learning in the broadest context of technological change.
PE01: Graduates will have a thorough grounding in the key principles and practices of computing, and will have
applied their software development skills and knowledge of foundational principles to the design and
implementation of practical systems consisting of software and/or hardware components to meet customer
requirements.
PEO2: Graduates will be successfully employed in the computing profession, and will be actively engaged in
learning, understanding, and applying new ideas and technologies as the field evolves; or will have successfully
completed, or be actively pursuing, graduate studies in computing.
PEO3: To impart science-based engineering education to develop professional skills that will prepare the
students for immediate employment in the relevant branch of engineering in industry, as against the model that
just prepares them for post-graduate education.
PEO4: To develop the design capability among students so that they have the ability to participate in creative,
synthetic and integrative activities of the relevant branch of engineering
PEO5: To develop among students breadth of knowledge so that they have familiarity with many
important technical areas
Students:
PSO 1: To develop among students ability to apply in depth knowledge of one or more
specializations within the relevant branch of engineering.
PSO 2: To develop communication skills so that the students are able to express ideas clearly and
persuasively, in written and oral forms.
PSO 3: To develop among students the awareness of, and the competence to be savvy users of
information Technology
PSO 4: To develop ability to design and perform experiments in the relevant discipline, and to
acquire the ability to interpret and evaluate experimental results.
RULESANDREGULATIONS
1. When entering a laboratory for an experiment, be aware of the zone in which the experiment will be
conducted and use the PPEs accordingly. Students are required to come with appropriate attire and
shoes for the experiment. No dangling objects (e.g., necklace), loose-fit garments, and slippers are
allowed.
2. Each laboratory is equipped with a first-aid box and fire extinguishers to be used in an emergency.
The safety guides with emergency contact telephone numbers will be made available to all students.
A safety telephone directory will also be available near the laboratory telephones for quick access..
3. Be cognizant about the emergency exits, fire exits, and assembly areas when you are working in a
laboratory.
4. In an emergency, seek professional help. Consult the laboratory staff who have been trained to
provide professional help. Do not attend to any safety operation if you are not trained.
5. If you come across any defective equipment with potential risks, inform the laboratory staff
immediately.
6. In case of any type of accident and/or if someone is hurt, seek help immediately. Behave as a
responsible citizen in case of serious accidents and report to the concerned authorities. Breaching of
safety rules during an experiment will not be tolerated.
7. After finishing each experiment, they must get the staff member in charge to sign the laboratory
record
SAFETYPRECAUTIONS
1. All staff, students and others who work in laboratory must understand and agree to the information
in this document. All procedures which need to be conducted alone or after-hours need to be
reviewed for hazards and must be
2. Approved by the in charge laboratory. Know location of all exits, evacuation route, first aid kit, eye
wash, fire extinguisher.
3. Shoes must completely cover the foot. No sandals or any other shoes are allowed.
6. All work space must be kept clear of clutter. All exits, fire extinguishers, electrical disconnects,
eye.
7. Washes and safety showers must remain accessible at all times. All equipment guards must remain
in place. You may not modify equipment without approval of the in charge laboratory.
CONTENTS
SL NAMEOFTHE EXPERIMENTS PAGE NO
NO
1 Using JFLAP, create a DFA from a given regular expression. All types 11
of error must be checked during the conversion.
2 Read a regular expression in standard form and check its validity by 15
converting it to postfix form. Scan a string and check whether the
string matches against the given regular expression or not.
3 (Tokenizing). A programs that reads a source code in C/C++ from an 19
unformatted file and extract various types of tokens from it (e.g.
keywords/variable names, operators, constant values).
4 Read a regular expression in its standard form and find out an Ɛ-NFA 23
from it. Need to use adjacency list data structure of graph to store
NFA. Thompson’s construction needs to be used too.
5 Evaluate an arithmetic expression with parentheses, unary and binary 26
operators using Flex and Yacc.
6 (Tokenizing) Use Lex and yacc to extract tokens from a given source 29
code.
7 Write a suitable data structure to store a Context Free Grammar. 33
Prerequisite is to eliminate left recursion from the grammar before
storing. Write functions to find FIRST and FOLLOW of all the
variables.
8 Using JFLAP create LL(1) parse table for a given CFG and hence 36
Simulate LL(1) parsing.
9 Using JFLAP create SLR(1) parse table for a given grammar. Simulate 38
parsing and output the parse tree proper format.
INDEXSHEET/VALUATIONSHEET
1.
Using JFLAP, create a DFA from a given
regular expression. All types of error must be
checked during the conversion.
9.
Using JFLAP create SLR(1) parse table for a
given grammar. Simulate parsing and output
the parse tree proper format.
Pass mark will be 50% which means students have to score 50 out of 100
Evaluation Scheme
Attendance & Daily performance 20
Lab manual 20
Lab quiz 10
Final experiment & viva 50
Total 100
All lab examination is to be completed one week before the end semester examination and
marks are to be displayed on the collage notice board.
Apparatus Required
A desktop computer
Marker pen /Paper / notebook
JFLAP program
Procedure:
What is JFLAP: -
JFLAP program makes it possible to create and simulate automata. Learning about automata with pen
and paper can be difficult, time consuming and error-prone. With JFLAP we can create automata of
different types and it is easy to change them as we want. JFLAP supports creation of DFA and NFA,
Regular Expressions, PDA, Turing Machines, Grammars and more.
Setup: -
JFLAP is available from the homepage: (www.JFLAP.org). From there press “Get FLAP” and follow the
instructions. You will notice that JFLAP have a .JAR extension. This means that you need Java to run
JFLAP. With Java correctly installed you can simply select the program to run it. You can also use a
command console run it from the files current directory with, Java –jar JFLAP.jar.
Using JFLAP: -
When you first start JFLAP you will see a small menu with a selection of eleven different automata and rule
sets. Choosing one of them will open the editor where you create chosen type of automata. Usually you
can create automata containing states and transitions but there is also creation of Grammar and Regular
Expression which is made with a text editor.
Now you should have an empty window in front of you. You will have a couple of tools and features at your
disposal
The toolbar contains six tools, which are used to edit automata.
Attribute Editor Tool, changes properties and position of existing states and transitions.
State Creator Tool, creates new states.
Transition Creator Tool, creates transitions.
Deletion Tool, deletes states and transitions.
Undo/Redo, changes the selected object prior to their history.
Choose Regular Expression in the main menu, then just type the expression in the textbox. Definitions
for Regular Expressions in JFLAP:
Correctly written expressions can then be converted to an NFA. To convert your expression select Convert
→ Convert to NFA. The conversion will begin with two states and a transition with your Regular
Expression. With the (D)e-expressionify Transition tool you can break down the Regular Expression into
smaller parts. Each transition will contain a sub expression. The next step is to link every rule with lambda
transitions. Add new transition between states that should be connected with the Transition Tool. If you
are unsure what to do you can select Do Step to automatically make the next step. If you want the NFA
immediately Do All creates the whole NFA for you.
You can notice how the conversion differs depending on how the Regular Expression looks. For
example the expression a+b results in a fork, were either ‘a’ or ‘b’ can be chosen.
Input : Enter a String: aaaabbbbb Output: aaaabbbbb is accepted under rule 'a*b+
A desktop computer
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
Procedure:
1. /*
2. * C Program to Implements Regular Expression Matching
3. */
4. #include <stdio.h>
5. #include <string.h>
6. #define MATCH printf("\nThe Text Matches The Regular Expression");
7. #define NOTMATCH printf("\nThe Text Doesn't match the Regular
Expression");
8.
9. char reg[20], text[20];
10.
11. int main()
12. {
13. int i, rlen, tlen, f = 0;
14. char ans;
15.
16. do {
17. printf("\nEnter the Regular Expression\n");
18. scanf(" %[^\n]s", reg);
19. for (rlen = 0; reg[rlen] != '\0';rlen++);
20. printf("\nEnter the text\n");
21. scanf(" %[^\n]s", text);
22. for (tlen = 0;text[tlen] != '\0' ; tlen++);
23. if (reg[0] == '*')
24. {
25. printf("\nInvalid regular expression");
26. }
27. /*
28. *If the regular expression starts with Alphabet
29. */
30. if ((reg[0] >= 65 && reg[0] <= 90) || (reg[0] >= 97
&& reg[0] <=122))
31. {
32. if (reg[0] == text [0])
33. {
34. switch (reg[1])
35. {
36. case '.' :
37. switch (reg[2])
38. {
39. case '*':
40. if (tlen != 1)
41. {
42. if (reg[3] == text[tlen-1])
43. {
44. MATCH;
45. }
46. else
47. {
48. NOTMATCH;
A desktop computer
Marker pen /Paper / notebook
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
JFLAP program
Procedure:
Lexical analysis is the process of converting a sequence of characters (such as in a computer program of
web page) into a sequence of tokens (strings with an identified “meaning”). A program that perform
lexical analysis may be called a lexer, tokenize or scanner.
Algorithm:-
Program:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("\n Enter the File Name:");
scanf("%s",&file);
fi=fopen(file,"r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi=fopen("inter.c","r");
printf("\n Lexical Analysis");
fscanf(fi,"%s",a);
printf("\n Line: %d\n",i++);
while(!feof(fi))
{
if(strcmp(a,"$")==0)
{
printf("\n Line: %d \n",i++);
fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
} fscanf(fop,"%s",ch);
}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%k",ch);
printf("\t\t%s\t:\tKeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tConstant\n",a);
else
printf("\t\t%s\t:\tIdentifier\n",a);
}
flag=0;
fscanf(fi,"%s",a); }
getch();
}
Input Files:
Oper.c
( open para
) closepara
{ openbrace }
closebrace <
lesser
> greater
" doublequote ' singlequote :
colon
; semicolon
# preprocessor =
equal
== asign
% percentage ^
bitwise
& reference *
star
+ add -
sub
\ backslash /
slash
Key.C
int
void
main
char
if
for
while
else
printf
scanf
FILE
Include
stdio.h
conio.h
iostream.h
Input.c
#include "stdio.h"
#include "conio.h"
void main()
{
int a=10,b,c;
a=b*c;
getch();
}
Input & Output / Conclusion :
Read a regular expression in its standard form and find out an Ɛ-NFA from it. Need to use adjacency list data structure
of graph to store NFA. Thompson’s construction needs to be used too.
Apparatus Required
A desktop computer
Marker pen /Paper / notebook
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
JFLAP program
Procedure:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
f(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}
Input :
Output:
Evaluate an arithmetic expression with parentheses, unary and binary operators using Flex and Yacc.[Need to write
yylex() function and to be used with Lex and yacc.]
Apparatus Required
A desktop computer
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
Procedure:
Algorithm:
1) Get the input from the user and Parse it token by token.
2) First identify the valid inputs that can be given for a program.
3) The Inputs include numbers, functions like LOG, COS, SIN, TAN, etc. and operators.
4) Define the precedence and the associatively of various operators like +,-,/,* etc.
5) Write codes for saving the answer into memory and displaying the result on the screen.
7) Display the possible Error message that can be associated with this calculation.
8) Display the output on the screen else display the error message on the screen.
Program:
CALC.L
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *);
#include "y.tab.h"
int yylval;
%}
%%
[a-z] {yylval=*yytext='&'; return VARIABLE;}
[0-9]+ {yylval=atoi(yytext); return INTEGER;}
CALC.Y
[\t] ;
%%
int yywrap(void)
{
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
return 1;
}
%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'
%{
int yylex(void);
void yyerror(char *);
int sym[26];
%}
%%
PROG:
PROG STMT '\n'
;
STMT: EXPR {printf("\n %d",$1);}
| VARIABLE '=' EXPR {sym[$1] = $3;}
;
EXPR: INTEGER
| VARIABLE {$$ = sym[$1];}
| EXPR '+' EXPR {$$ = $1 + $3;}
| '(' EXPR ')' {$$ = $2;}
%%
void yyerror(char *s)
{
printf("\n %s",s);
return;
}
int main(void)
{
printf("\n Enter the Expression:");
yyparse();
return 0;
}
$ lex calc.l
$ yacc -d calc.y
$ cc y.tab.c lex.yy.c -ll -ly -lm
$ . / a . out
Enter the Expression: ( 5 + 4 ) * 3
Answer: 27
(Tokenizing) Use Lex and yacc to extract tokens from a given source code.
Apparatus Required
A desktop computer
Marker pen /Paper / notebook
JFLAP program
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
Procedure:
Algorithm:
1. First, a specification of a lexical analyzer is prepared by creating a program lexp.l in the
LEX language.
2. The Lexp.l program is run through the LEX compiler to produce an equivalent code in C
language named Lex.yy.c
3. The program lex.yy.c consists of a table constructed from the Regular Expressions of
Lexp.l, together with standard routines that uses the table to recognize lexemes.
Finally, lex.yy.c program is run through the C Compiler to produce an object program a.out, which is the
lexical analyzer that transforms an input stream into a sequence of tokens.
Program:
lexp.l
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf ("\n %s is a Preprocessor Directive",yytext);}
int |
float |
main |
if |
else |
printf |
scanf |
for |
char |
getch |
while {printf("\n %s is a Keyword",yytext);}
"/*" {COMMENT=1;}
"*/" {COMMENT=0;}
{identifier}\( {if(!COMMENT) printf("\n Function:\t %s",yytext);} \
{ {if(!COMMENT) printf("\n Block Begins"); \} {if(!COMMENT)
printf("\n Block Ends");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s is an
Identifier",yytext);} \".*\" {if(!COMMENT) printf("\n %s is a String",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is a Number",yytext);} \)
(\;)? {if(!COMMENT) printf("\t");ECHO;printf("\n");} \( ECHO;
test.c
#include<stdio.h>
main()
{
int fact=1,n;
for(int i=1;i<=n;i++)
{ fact=fact*i; }
printf("Factorial Value of N is", fact);
getch();
}
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
$ lex lexp.l
$ cc lex.yy.c
$ ./a.out test.c
#include<stdio.h> is a Preprocessor Directive
Function: main( )
Block Begins
int is a Keyword
fact is an Identifier
= is an Assignment Operator
1 is a Number
n is an Identifier
Function: for(
int is a Keyword
i is an Identifier
= is an Assignment Operator
1 is a Number i is
an Identifier
<= is a Relational Operator n
is an Identifier
i is an Identifier );
Block Begins
fact is an Identifier
= is an Assignment Operator
fact is an Identifier
i is an Identifier
Block Ends
Function: printf(
"Factorial Value of N is" is a String
fact is an Identifier ); Function:
getch( );
Block Ends
AIM OF THE EXPERIMENT - 7
Write a suitable data structure to store a Context Free Grammar. Prerequisite is to eliminate left recursion from the
grammar before storing. Write functions to find FIRST and FOLLOW of all the variables.[May use unformatted file /
array to store the result].
Apparatus Required
A desktop computer
Marker pen /Paper / notebook
JFLAP program
Procedure:
Algorithm:
First ()-
Follow ()-
Program:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main(){
int i,z;
char c,ch;
clrscr();
printf("Enter the no of prooductions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')
first(a[i][j+1]);
if(a[i][j+1]=='\0' && c!=a[i][0])
follow(a[i][0]);
}
}
}
Input & Output / Conclusion
A desktop computer
Marker pen /Paper / notebook
JFLAP program
Procedure:
Implementation: -
Step 1. Choose the grammar from JFLAP and insert grammar you want to create LL(1) parsing table.
Step 2: - Select the Input from Menu and select Build LL(1) parsing table from it.
Step 3: - Now select parse to use that table to create parse tree from it.
We create LL(1) parse table for a given CFG and hence Simulate LL(1) parsing
A desktop computer
Marker pen /Paper / notebook
JFLAP program
Procedure:
COMPILER DESIGN LAB
RAAJDHANI ENGINEERING COLLEGE (B-Tech), BHUBANESWAR
RCS6C202 COMPILER DESIGN LAB
Implementation:
Step 1: - Choose the grammar from JFLAP and insert grammar you want to create SLR(1) parsing table.
Step 2: - Select the Input from Menu and select Build SLR (1) parsing table from it.
Step 3: - Now select parse to use that table to create parse tree from it.
We created SLR(1) parse table for a given grammar and Simulated parsing and output the parse
tree proper format