SlideShare a Scribd company logo
1 of 19
Index
S. No. Name of Program Page
No.
Remarks/
Signature
1. Write a C program for simulation of a finite
state automata to recognize the tokens of
various control statements.
1
2. Write a C program to copy the contentof
one file to another and count the number of
comment lines in a C file.
4
3. Write a C program to count the number of
lines, number of words, number of blank
spaces andnumber of characterin a C file.
6
4. Write a C program to find the no of
identifiers in a C file.
8
5. Write a C program to evaluate an
arithmetic expressioninvolving operators +,
-, *, /.
11
6. Write a C program to recognize strings
‘aaab’, ‘abbb’, ‘ab’, ‘a’, aabb using the
grammer (an bn, n>=0).
14
7. Write a C program to find FIRST of NON
TERMINALS of the given grammar.
16
- 1 -
Program 1
Write a C program for simulation of a finite state automata to recognize
the tokens ofvarious control statements.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[8];
int s,i,flag=0,loc1=0,loc2=0,j;
printf("Enter any stringn");
gets(a);
for(i=0;i<8;i++)
{
int ch=a[i];
if(ch!=32)
{
loc2=i;
flag++;
}
if(ch==32)
{
s=flag;
flag=0;
switch(s)
{
case 2:
if(a[loc1]=='i' && a[loc2]=='f')
{
printf("STRING HAS if AS A KEYWORD");
}
break;
case 3:
if(a[loc1]=='f' && a[loc1+1]=='o' && a[loc2]=='r')
{
printf("STRING HAS for AS A KEYWORD");
}
break;
default:
- 2 -
printf("not our case");
}
}
}
getch();
}
- 3 -
Output:-
- 4 -
Program 2
Write a C program to copy the contentof one file to another and count the
number of comment lines in a C file.
%{
#include<stdio.h>
#include<conio.h>
int com=0;
%}
%%
/*[^*]**(*|([^*/][^*]**))*/ com++;//[^n]*[n] com++;
% fprintf(yyout,"%%");
.|[n] fprintf(yyout, yytext);
%%
main(int argc, char **argv)
{
clrscr();
if(argc!=3)
{
printf("n Arguments passed in wrong mannern");
return 1;
}
yyin=fopen(*(argv+1),"r");
yyout=fopen(*(argv+2),"w");
if(!(yyin&&yyout))
{
printf("n Specified file cannot be opened!");
return 1;
}
yylex();
printf("n Total number of comment lines=%d n", com);
}
int yywrap()
{
return 1;
- 5 -
Output:-
- 6 -
Program 3
Write a C program to count the number of lines, number of words, number
of blank spacesand number of characterin a C file.
%{
#include<stdio.h>
#include<conio.h>
int cc=0,bc=0,wc=0,lc=0;
%}
%%
[^ tn]+ { wc++;
cc=cc+yyleng;}
n lc++;
" " bc++;
t bc=bc+5;
%%
main(int argc, char *Count[])
{
clrscr();
if (argc!=2)
{
printf("n usage:./a.out filenamen");
return(0);
}
yyin=fopen(Counr[1],"r");
yylex();
printf("n no of lines are %dn", lc);
printf("n no of words are %dn", wc);
printf("n no of blanks are %dn", bc);
printf("n no of character are %dn", cc);
}
int yywrap()
{
return 1;
}
- 7 -
Output:-
- 8 -
Program 4
Write a C program to find the no of identifiers in a C file.
%{
#include<stdio.h>
#include<conio.h>
int count=0;
%}
%%
"int" |
"float" |
"double" |
"char" {
char ch;
ch=input();
while(1) {
if(ch==',')
count++;
if(ch==';'){
count++;
break;
}
if(ch=='n')
break;
ch=input();
}
}
. | 'n' ;
%%
int main(int argc, char *argv[])
{
if(argc!=2)
{
printf("nn INVALID INPUTnn");
}
else
{
yyin=fopen(argv[1],"r");
if(yyin)
{
- 9 -
yylex();
printf("n No of identifiers = %dn", count);
}
else
printf("nError in opening the filen");
}
printf("n");
}
int yywrap()
{
return 1;
}
- 10 -
Output:-
- 11 -
Program 5
Write a C program to evaluate an arithmetic expressioninvolving
operators +, -, *, /.
Lex Part:
%{
#include<stdio.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+{
yylval=atoi(yytext);
return NUM;
}
[t];
return 0;
.return yytext[0];
%%
Yacc Part:
%{
#include<stdio.h>
#include<conio.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%left '(' ')'
%%
expr: e{
printf("result:%dn",$$);
return 0;
}
e:e'+'e {$$=$1+$3;}
|e'-'e {$$=$1-$3;}
|e'*'e {$$=$1*$3;}
- 12 -
|e'/'e {$$=$1/$3;}
|'('e')' {$$=$2;}
| NUM {$$=$1;}
;
%%
main()
{
clrscr();
printf("n enter the arithematic expression:n");
yyparse();
printf("nvalid expressionn");
}
yyerror()
{
printf("n invalid expressionn");
exit(0);
}
- 13 -
Output:-
- 14 -
Program 6
Write a C programto recognize strings ‘aaab’, ‘abbb’, ‘ab’, ‘a’, aabb using the
grammer (an bn, n>=0).
Lex Part:
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
.|n return yytext[0];
%%
Yacc Part:
%{
#include<stdio.h>
int valid=1;
%}
%token A B
%%
str:S'n' {return 0;}
S:A S B
. |;
%%
main()
{
printf("Enter the string:n");
yyparse();
if(valid==1)
printf("nvalid string");
}
- 15 -
Output:-
- 16 -
Program 7
Write a C program to find FIRST of NON TERMINALS of the
given grammar.
#include<stdio.h>
#include<conio.h>
char array[10][20],temp[10];
int c,n;
void fun(int,int[]);
int fun2(int i,int j,int p[],int );
void main()
{
clrscr();
int p[2],i,j;
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0;i<n;i++)
{
c=-1, p[0]=-1, p[1]=-1;
fun(i,p);
printf("First(%c) : [ ",array[i][0]);
for(j=0;j<=c;j++)
printf("%c,", temp[j]);
printf("b ].n");
getch();
}
}
int fun2(int i, int j, int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
- 17 -
else
{
for(k=0;k<=c;k++)
{
if(array[i][j]==temp[k])
break;
}
if(k>c)return 1;
else return 0;
}
}
void fun(int i,int p[])
{
int j ,k, key;
for(j=2;array[i][j] != NULL; j++)
{
if(array[i][j-1]=='/')
{
if(array[i][j]>= 'A' && array[i][j]<='Z')
{
key=0;
fun2(i, j, p, key);
}
else
{
key = 1;
if(fun2(i, j, p, key))
temp[++c] = array[i][j];
if(array[i][j]== '@'&& p[0]!=-1) //taking '@' as null symbol
{
if(array[p[0]][p[1]]>='A' && array[p[0]][p[1]] <='Z')
{
key=0;
fun2(p[0], p[1], p, key);
}
else
if(array[p[0]][p[1]] != '/'&& array[p[0]][p[1]]!=NULL)
{
if(fun2(p[0], p[1], p, key))
temp[++c]=array[p[0]][p[1]];
}
}
}
}
}
}
- 18 -
Output:-

More Related Content

What's hot

Pressman ch-11-component-level-design
Pressman ch-11-component-level-designPressman ch-11-component-level-design
Pressman ch-11-component-level-designOliver Cheng
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSSVigneshkumar Ponnusamy
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Decomposition technique In Software Engineering
Decomposition technique In Software Engineering Decomposition technique In Software Engineering
Decomposition technique In Software Engineering Bilal Hassan
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
Unified process model
Unified process modelUnified process model
Unified process modelRyndaMaala
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical AnalyzerArchana Gopinath
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Muhammad Hammad Waseem
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Alamgir Hossain
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and LexemesBen Scholzen
 
Software Engineering Fundamentals
Software Engineering FundamentalsSoftware Engineering Fundamentals
Software Engineering FundamentalsRahul Sudame
 

What's hot (20)

Pressman ch-11-component-level-design
Pressman ch-11-component-level-designPressman ch-11-component-level-design
Pressman ch-11-component-level-design
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Decomposition technique In Software Engineering
Decomposition technique In Software Engineering Decomposition technique In Software Engineering
Decomposition technique In Software Engineering
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
Php array
Php arrayPhp array
Php array
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
Uncertainty in AI
Uncertainty in AIUncertainty in AI
Uncertainty in AI
 
Unified process model
Unified process modelUnified process model
Unified process model
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
System Modelling
System ModellingSystem Modelling
System Modelling
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Software Engineering Fundamentals
Software Engineering FundamentalsSoftware Engineering Fundamentals
Software Engineering Fundamentals
 

Viewers also liked

7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab MashaelQ
 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design FileArchita Misra
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)guest251d9a
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyappasami
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front pageKandarp Tiwari
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoringShraddha Patel
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture NotesFellowBuddy.com
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1MashaelQ
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local namesVarsha Kumar
 
Practical List COMPILER DESIGN
Practical List COMPILER DESIGNPractical List COMPILER DESIGN
Practical List COMPILER DESIGNShraddha Patel
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics labPriya Goyal
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab FileKandarp Tiwari
 
Critical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet BankingCritical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet BankingThomas Donofrio
 

Viewers also liked (20)

Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Cd lab manual
Cd lab manualCd lab manual
Cd lab manual
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design File
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer key
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front page
 
Program to remove Left factoring
Program to remove Left factoringProgram to remove Left factoring
Program to remove Left factoring
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Compiler Engineering Lab#1
Compiler Engineering Lab#1Compiler Engineering Lab#1
Compiler Engineering Lab#1
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
 
Practical List COMPILER DESIGN
Practical List COMPILER DESIGNPractical List COMPILER DESIGN
Practical List COMPILER DESIGN
 
Books anna university
Books anna universityBooks anna university
Books anna university
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
 
Critical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet BankingCritical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet Banking
 
MATERI KRIPTOGRAFI
MATERI KRIPTOGRAFIMATERI KRIPTOGRAFI
MATERI KRIPTOGRAFI
 

Similar to Compiler Design Lab File

'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
C basics
C basicsC basics
C basicsMSc CST
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdfmhande899
 

Similar to Compiler Design Lab File (20)

'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C file
C fileC file
C file
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
2. operator
2. operator2. operator
2. operator
 
C basics
C basicsC basics
C basics
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C-programs
C-programsC-programs
C-programs
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
2 data and c
2 data and c2 data and c
2 data and c
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
C language concept with code apna college.pdf
C language concept with code apna college.pdfC language concept with code apna college.pdf
C language concept with code apna college.pdf
 

More from Kandarp Tiwari

Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariKandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariKandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab FileKandarp Tiwari
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front PageKandarp Tiwari
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front PageKandarp Tiwari
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab FileKandarp Tiwari
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programsKandarp Tiwari
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab FileKandarp Tiwari
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 

More from Kandarp Tiwari (11)

Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front Page
 
Web technology
Web technologyWeb technology
Web technology
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front Page
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

Compiler Design Lab File

  • 1. Index S. No. Name of Program Page No. Remarks/ Signature 1. Write a C program for simulation of a finite state automata to recognize the tokens of various control statements. 1 2. Write a C program to copy the contentof one file to another and count the number of comment lines in a C file. 4 3. Write a C program to count the number of lines, number of words, number of blank spaces andnumber of characterin a C file. 6 4. Write a C program to find the no of identifiers in a C file. 8 5. Write a C program to evaluate an arithmetic expressioninvolving operators +, -, *, /. 11 6. Write a C program to recognize strings ‘aaab’, ‘abbb’, ‘ab’, ‘a’, aabb using the grammer (an bn, n>=0). 14 7. Write a C program to find FIRST of NON TERMINALS of the given grammar. 16
  • 2. - 1 - Program 1 Write a C program for simulation of a finite state automata to recognize the tokens ofvarious control statements. #include<stdio.h> #include<conio.h> #include<string.h> void main() { clrscr(); char a[8]; int s,i,flag=0,loc1=0,loc2=0,j; printf("Enter any stringn"); gets(a); for(i=0;i<8;i++) { int ch=a[i]; if(ch!=32) { loc2=i; flag++; } if(ch==32) { s=flag; flag=0; switch(s) { case 2: if(a[loc1]=='i' && a[loc2]=='f') { printf("STRING HAS if AS A KEYWORD"); } break; case 3: if(a[loc1]=='f' && a[loc1+1]=='o' && a[loc2]=='r') { printf("STRING HAS for AS A KEYWORD"); } break; default:
  • 3. - 2 - printf("not our case"); } } } getch(); }
  • 5. - 4 - Program 2 Write a C program to copy the contentof one file to another and count the number of comment lines in a C file. %{ #include<stdio.h> #include<conio.h> int com=0; %} %% /*[^*]**(*|([^*/][^*]**))*/ com++;//[^n]*[n] com++; % fprintf(yyout,"%%"); .|[n] fprintf(yyout, yytext); %% main(int argc, char **argv) { clrscr(); if(argc!=3) { printf("n Arguments passed in wrong mannern"); return 1; } yyin=fopen(*(argv+1),"r"); yyout=fopen(*(argv+2),"w"); if(!(yyin&&yyout)) { printf("n Specified file cannot be opened!"); return 1; } yylex(); printf("n Total number of comment lines=%d n", com); } int yywrap() { return 1;
  • 7. - 6 - Program 3 Write a C program to count the number of lines, number of words, number of blank spacesand number of characterin a C file. %{ #include<stdio.h> #include<conio.h> int cc=0,bc=0,wc=0,lc=0; %} %% [^ tn]+ { wc++; cc=cc+yyleng;} n lc++; " " bc++; t bc=bc+5; %% main(int argc, char *Count[]) { clrscr(); if (argc!=2) { printf("n usage:./a.out filenamen"); return(0); } yyin=fopen(Counr[1],"r"); yylex(); printf("n no of lines are %dn", lc); printf("n no of words are %dn", wc); printf("n no of blanks are %dn", bc); printf("n no of character are %dn", cc); } int yywrap() { return 1; }
  • 9. - 8 - Program 4 Write a C program to find the no of identifiers in a C file. %{ #include<stdio.h> #include<conio.h> int count=0; %} %% "int" | "float" | "double" | "char" { char ch; ch=input(); while(1) { if(ch==',') count++; if(ch==';'){ count++; break; } if(ch=='n') break; ch=input(); } } . | 'n' ; %% int main(int argc, char *argv[]) { if(argc!=2) { printf("nn INVALID INPUTnn"); } else { yyin=fopen(argv[1],"r"); if(yyin) {
  • 10. - 9 - yylex(); printf("n No of identifiers = %dn", count); } else printf("nError in opening the filen"); } printf("n"); } int yywrap() { return 1; }
  • 12. - 11 - Program 5 Write a C program to evaluate an arithmetic expressioninvolving operators +, -, *, /. Lex Part: %{ #include<stdio.h> #include"y.tab.h" extern int yylval; %} %% [0-9]+{ yylval=atoi(yytext); return NUM; } [t]; return 0; .return yytext[0]; %% Yacc Part: %{ #include<stdio.h> #include<conio.h> %} %token NUM %left '+' '-' %left '*' '/' %left '(' ')' %% expr: e{ printf("result:%dn",$$); return 0; } e:e'+'e {$$=$1+$3;} |e'-'e {$$=$1-$3;} |e'*'e {$$=$1*$3;}
  • 13. - 12 - |e'/'e {$$=$1/$3;} |'('e')' {$$=$2;} | NUM {$$=$1;} ; %% main() { clrscr(); printf("n enter the arithematic expression:n"); yyparse(); printf("nvalid expressionn"); } yyerror() { printf("n invalid expressionn"); exit(0); }
  • 15. - 14 - Program 6 Write a C programto recognize strings ‘aaab’, ‘abbb’, ‘ab’, ‘a’, aabb using the grammer (an bn, n>=0). Lex Part: %{ #include "y.tab.h" %} %% a return A; b return B; .|n return yytext[0]; %% Yacc Part: %{ #include<stdio.h> int valid=1; %} %token A B %% str:S'n' {return 0;} S:A S B . |; %% main() { printf("Enter the string:n"); yyparse(); if(valid==1) printf("nvalid string"); }
  • 17. - 16 - Program 7 Write a C program to find FIRST of NON TERMINALS of the given grammar. #include<stdio.h> #include<conio.h> char array[10][20],temp[10]; int c,n; void fun(int,int[]); int fun2(int i,int j,int p[],int ); void main() { clrscr(); int p[2],i,j; printf("Enter the no. of productions :"); scanf("%d",&n); printf("Enter the productions :n"); for(i=0;i<n;i++) scanf("%s",array[i]); for(i=0;i<n;i++) { c=-1, p[0]=-1, p[1]=-1; fun(i,p); printf("First(%c) : [ ",array[i][0]); for(j=0;j<=c;j++) printf("%c,", temp[j]); printf("b ].n"); getch(); } } int fun2(int i, int j, int p[],int key) { int k; if(!key) { for(k=0;k<n;k++) if(array[i][j]==array[k][0]) break; p[0]=i;p[1]=j+1; fun(k,p); return 0; }
  • 18. - 17 - else { for(k=0;k<=c;k++) { if(array[i][j]==temp[k]) break; } if(k>c)return 1; else return 0; } } void fun(int i,int p[]) { int j ,k, key; for(j=2;array[i][j] != NULL; j++) { if(array[i][j-1]=='/') { if(array[i][j]>= 'A' && array[i][j]<='Z') { key=0; fun2(i, j, p, key); } else { key = 1; if(fun2(i, j, p, key)) temp[++c] = array[i][j]; if(array[i][j]== '@'&& p[0]!=-1) //taking '@' as null symbol { if(array[p[0]][p[1]]>='A' && array[p[0]][p[1]] <='Z') { key=0; fun2(p[0], p[1], p, key); } else if(array[p[0]][p[1]] != '/'&& array[p[0]][p[1]]!=NULL) { if(fun2(p[0], p[1], p, key)) temp[++c]=array[p[0]][p[1]]; } } } } } }