0% found this document useful (0 votes)
18 views9 pages

CD New File

The document contains multiple code snippets related to lexical analysis programs in C/C++ using lex: 1) It contains code to count the number of keywords, operators, and characters in a given input file. 2) Lex programs are presented to validate a mobile number, identifier, URL, date, and time. 3) Additional lex programs count white spaces, vowels/consonants, and identify positive and negative numbers. 4) The last section contains code for a symbol table implementation with functions for insertion, deletion, and display.

Uploaded by

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

CD New File

The document contains multiple code snippets related to lexical analysis programs in C/C++ using lex: 1) It contains code to count the number of keywords, operators, and characters in a given input file. 2) Lex programs are presented to validate a mobile number, identifier, URL, date, and time. 3) Additional lex programs count white spaces, vowels/consonants, and identify positive and negative numbers. 4) The last section contains code for a symbol table implementation with functions for insertion, deletion, and display.

Uploaded by

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

WAP TO CHECK WHETHER A STRING CONTAINS KEYWORD OR NOT

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char keyword[32][10]={"auto","double","int","struct","break","else","long",

"switch","case","enum","register","typedef","char",

"extern","return","union","const","float","short",

"unsigned","continue","for","signed","void","default",

"goto","sizeof","voltile","do","if","static","while"} ;

char string[10];

int flag=0,i;

printf("enter any string:");

gets(string);

for(i=0;i<32;i++)

if(strcmp(string,keyword[i])==0)

flag=1;

if(flag==1)

printf("%s is a keyword",string);

else

printf("%s is not a keyword",string);

getch();
}

-----------------------------------------------------------------------------------
-
WAP TO COUNT TOTAL NUMBER OF KEYWORD IN A FILE

void main()
{
//2d array used to store the keywords but few of them are used.

char key[32][12]={"int","char","while","for","if","else"};

//cnt is used to count the occurrence of the keyword in the file.

int cnt=0,i;

//used to store the string that is read line by line.

char ch[100];

FILE *fp=fopen("key.c","r");

//to check whether file exists or not

if(fp=='\0')
{
printf("file not found..\n");
exit(0);
}
//to extract the word till it don't reach the end of file

while((fscanf(fp,"%s",ch))!=EOF)
{
//compare the keyword with the word present in the file.

for(i=0;i<32;i++)
{
// compare the keyword with the string in ch.

if(strcmp(key[i],ch)==0) {
//just to check which keyword is printed.
printf("\nkeyword is : %s",ch);
cnt++;
}
}
}
printf("\n Total no. of keywords are : %d", cnt);
fclose(fp);
}
----------------------------------------------------------------
WAP TO COUNT TOTAL NUMBER OF OPERATORS IN A GIVEN INPUT FILE

#include<stdio.h>
#define MAX_FILENAME 200
int main()
{
int T=2;
while(T--)
{
FILE* fptr;
int count= 0;
int n ;
// printf("Enter sizxe of array");
//scanf("%d",&n);
char filename[n];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cant open %s",
filename);
return 0;
}
for (c = getc(fptr); c != EOF; c = getc(fptr))
{
if(c>=33&&c<=47||c>=58&&c<=63)
count++;
}
fclose(fptr);
printf("The file %s has %d opreatorrs \n ", filename, count);
}
return 0;
}

------------------------------------------------------------------
WAP TO COUNT TOTAL NO. OF CHARACTERS IN A FILE
// C Program to count
// the Number of Characters in a Text File

#include <stdio.h>
#define MAX_FILENAME 200

int main()
{
int t=2;
while(t--)
{
FILE* fp;
int count = 0;
int n ;
char filename[n];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Cant open %s",
filename);
return 0;
}for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",
filename, count);
}
return 0;
}
----------------------------------------------------------
WAP IN C TO INSERT,DELETE AND DISPLAY THE ENTRIES IN A SYMBOL TABLE
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
int size=0;
void Insert();
void Display();
void Delete();

struct SymbTab
{
char label[10],symbol[10];
int addr;
struct SymbTab *next;};
struct SymbTab *first,*last;
void main()
{
int op,y;
char la[10];
clrscr();
do
{
printf("\n\tSYMBOL TABLE IMPLEMENTATION\n");
printf("\n\t1.INSERT\n\t2.DISPLAY\n\t3.DELETE\n\t4.SEARCH\n");
printf("\n\tEnter your option : ");
scanf("%d",&op);
switch(op)
{
case 1:
Insert();
break;
case 2:
Display();
break;
case 3:
Delete();
break;
case 4:
exit(0);
}
}while(op<3);
getch();
}
void Insert()
{
int n;
char l[10];
printf("\n\tEnter the label : ");
scanf("%s",l);
n=Search(l);
if(n==1)
printf("\n\tThe label exists already in the symbol table\n\tDuplicate can't be
inserted");
else
{
struct SymbTab *p;
p=malloc(sizeof(struct SymbTab));
strcpy(p->label,l);
printf("\n\tEnter the symbol : ");
scanf("%s",p->symbol);
printf("\n\tEnter the address : ");
scanf("%d",&p->addr);
p->next=NULL;
if(size==0)
{
first=p;
last=p;
}
else
{
last->next=p;
last=p;
}
size++;
}
printf("\n\tLabel inserted\n");
}
void Display()
{
int i;
struct SymbTab *p;
p=first;
printf("\n\tLABEL\t\tSYMBOL\t\tADDRESS\n");
for(i=0;i<size;i++)
{
printf("\t%s\t\t%s\t\t%d\n",p->label,p->symbol,p->addr);
p=p->next;
}
}

void Delete()
{
int a;
char l[10];
struct SymbTab *p,*q;
p=first;
printf("\n\tEnter the label to be deleted : ");
scanf("%s",l);
a=Search(l);
if(a==0)
printf("\n\tLabel not found\n");
else
{
if(strcmp(first->label,l)==0)
first=first->next;
else if(strcmp(last->label,l)==0)
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=NULL;
last=p;
}
else
{
q=p->next;
while(strcmp(q->label,l)!=0)
{
p=p->next;
q=q->next;
}
p->next=q->next;
}
size--;
printf("\n\tAfter Deletion:\n");
Display();
}
}
---------------------------------------------------------------
WAP A LEX PROGRAM TO COUNT NO. OF CHARACTERS IN A STRINGS

---------------------------------------------------------------
write a lex prog to validate a mobile number
/* Lex Program to check valid Mobile Number */

%{
/* Definition section */
%}

/* Rule Section */
%%

[1-9][0-9]{9} {printf("\nMobile Number Valid\n");}

.+ {printf("\nMobile Number Invalid\n");}

%%

// driver code
int main()
{
printf("\nEnter Mobile Number : ");
yylex();
printf("\n");
return 0;
}
----------------------------------------------------------------
write a lex prog to validate a idenfier
/*lex code to determine whether input is an identifier or not*/
% {
#include <stdio.h>
%
}

/ rule section % %
// regex for valid identifiers
^[a - z A - Z _][a - z A - Z 0 - 9 _] * printf("Valid Identifier");

// regex for invalid identifiers


^[^a - z A - Z _] printf("Invalid Identifier");
.;
% %

main()
{
yylex();
}
----------------------------------------------------------------
write a lex program to validate a url
%%
((http)|(ftp))s?:\/\/[a-zA-Z0-9]{2, }(\.[a-z]{2, })
+(\/[a-zA-Z0-9+=?]*)* {printf("\nURL Valid\n");}

.+ {printf("\nURL Invalid\n");}

%%

// driver program
void main()
{
printf("\nEnter URL : ");
yylex();
printf("\n");
}
--------------------------------------------------------------------------
write a lex program to validate a date
%{
#include<stdio.h>
int valid=0;
%}
%%
([0-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([1-2][0-9][0-9][0-9]) {valid=1;}
%%
main()
{
yyin=fopen("input.txt","r");
yylex();
if(valid==1) printf("It is a valid date\n");
else printf("It is not a valid date\n");
}
int yywrap()
{
return 1;
}
------------------------------------------------------------------------------
lex prog to validate time
lex prog to validate time
%{
%}
%%
([0-2] [0-9]:[0-6][0-9]\:[0-6][0-9])
{
printf("it is valid time\n");
}
.+{printf("invalid time");}
%%
int main(){
printf("enter time");
yylex();
printf("\n");
return 0;
}
int yywrap(){}
----------------------------------------------------------------------------
count white spaces
%{
#include<stdio.h>
int space=0,words=0,lines=0;
%}
%%
[\n]{lines++;}
[\t]{space++;}
[(a-z A-Z 0-9)]* {words++;}
%%
int main(int argc,char* argv[]){
printf("enter the input:\n");
yylex();
printf("no. of lines=%d\n",lines);
printf("No. of spaces =%d",space);
printf("No. of words=%d",words);

int yywrap();
{
return 1;
}
-----------------------------------------------------
9.vowel and consonanat
%{#include<stdio.h>
int vowels_cont=0;
int consonants_count=0;
%}
%%
[aeiou AEIOU]{vowel_count++;}
[a-z A-Z]{consonant_count++;}
%%
int yywrap(){}
int main(){
printf("enter a string:");
yylex();
printf("no of vowels=%d",vowel_count);
printf("No. of consonants=%d",consonant_count);
return 0;
}
-------------------------------------------------------------
10.positive and negative
/* Lex program to Identify and Count
Positive and Negative Numbers */
%{
int positive_no = 0, negative_no = 0;
%}

/* Rules for identifying and counting


positive and negative numbers*/
%%
^[-][0-9]+ {negative_no++;
printf("negative number = %s\n",
yytext);} // negative number

[0-9]+ {positive_no++;
printf("positive number = %s\n",
yytext);} // positive number
%%

/*** use code section ***/

int yywrap(){}
int main()
{

yylex();
printf ("number of positive numbers = %d,"
"number of negative numbers = %d\n",
positive_no, negative_no);

return 0;
}
---------------------------------------------------------

You might also like