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

Objective Resource: Program Logic

The document describes 4 programs: 1) A program to detect identifiers by checking the first character and subsequent characters. 2) A program to recognize constants by checking if the input is an integer. 3) A program to recognize keywords and identifiers by comparing input words to a list of keywords. 4) A program to ignore comments in a C program by checking for //, /*, and */ and writing comments to a separate file.

Uploaded by

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

Objective Resource: Program Logic

The document describes 4 programs: 1) A program to detect identifiers by checking the first character and subsequent characters. 2) A program to recognize constants by checking if the input is an integer. 3) A program to recognize keywords and identifiers by comparing input words to a list of keywords. 4) A program to ignore comments in a C program by checking for //, /*, and */ and writing comments to a separate file.

Uploaded by

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

1)

 OBJECTIVE
Write a program to detect identifiers.
 RESOURCE
Codeblocks
 PROGRAM LOGIC:
-Read input string
-Check the first character if it is between a and z or A and Z or underscore including a
and z
-Then checking other character if it is alphanumeric or underscore then if it fulfills
the two condition, it is valid.
Unless it is invalid
 PROGRAM

#include<string.h>
#include<ctype.h>
#include<stdio.h>
void Identifier(char str[10])
{
if(strcmp(str,"for")!=0 && strcmp(str,"printf")!=0 && strcmp(str,"void")!=0 &&
strcmp(str,"main")!=0 && strcmp(str,"iostream")!=0 && strcmp(str,"include")!=0 &&
strcmp(str,"while")!=0 && strcmp(str,"do")!=0 && strcmp(str,"int")!=0 &&
strcmp(str,"float")!=0 &&strcmp(str,"char")!=0 && strcmp(str,"double")!=0
&&strcmp(str,"static")!=0 &&strcmp(str,"switch")!=0 && strcmp(str,"case")!=0 )
printf(" \t%s",str);
}
main()
{
FILE *f1,*f2;
char c,str[10],st1[10];
int tokenvalue=0,i=0,j=0,k=0;
printf("\nEnter the c program");
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
while((c=getc(f1))!=EOF)
{
if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else if(c==' '||c=='\t')
printf(" ");
}
fclose(f2);
fclose(f1);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("The identifiers are:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
Identifier(str);
k=0;
}
}
fclose(f2);
}

 OUTPUT
Enter string:_name
_name is valid identifier

2).
 OBJECTIVE
Write a program to recognize constants.

 RESOURCE
Codeblock
 PROGRAM LOGIC:
-Read input string
Then check the input if it is integer or not
If it is integer say constant unless not constant

 PROGRAM
#include<string.h>
#include<ctype.h>
#include<stdio.h>
// sample input for constants
/*
main(){
int a=3, b=5;
String c="abrakedabra";

}
*/

main()
{

FILE *f1,*f2,*f3, *f4, *f5;


char c,str[10],st1[10], com[1000];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0,z;
printf("\nEnter the c program");/*gets(st1);*/

// input stands for the directory the file is in


// for example >>C:\Users\abrham\Documents\cpp\first.cpp
f1=fopen("input1.txt","w");

while((c=getchar())!='$')

putc(c,f1);

fclose(f1);

f1=fopen("input1.txt","r");

f5=fopen("constants","w");

while((c=getc(f1))!=EOF)
{
if(c=='"')
{
c=getc(f1);
putc('\n',f5);
while(c!='"')
{
putc(c,f5);
c=getc(f1);
}
}
if(isdigit(c))
{

tokenvalue=c-'0';
c=getc(f1);

while(isdigit(c))
{
z=c-'0';
tokenvalue=10*tokenvalue+z;
c=getc(f1);
}

num[i++]=tokenvalue;

ungetc(c,f1);

}
}
fclose(f5);
f3=fopen("constants","r");

printf("\nConstants are");

while((c=getc(f3))!=EOF)

printf("%c",c);

printf("\n");

fclose(f3);
for(j=0; j<i; j++)
{

printf("%d",num[j]);
printf("\n");
}
}
 OUTPUT
Enter any thing:89
It is constant

3)
 OBJECTIVE
Write a program to recognize keywords and identifiers-
 RESOURCE
Codeblocks
 PROGRAM LOGIC:
-Take input from user and split words using space then compare each word with keyword
they match print as keyword unless check as identifier and print as identifier
 PROGRAM
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void Recognize_keyword_identifier(char str[10])
{

if(strcmp("iostream",str)==0||strcmp("include",str)==0||strcmp("for",str)==0||strcmp("whi
le",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcmp("float",str)==0||strcmp("cha
r",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||strc
mp("case",str)==0)
printf("\n\t%s is a keyword",str);
else
printf("\n\t%s is an identifier",str);
}
main()
{
FILE *f1,*f2;
char c,str[10];
int k=0;
printf("\nEnter the c program");/*gets(st1);*/
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
while((c=getc(f1))!=EOF)
{
if(isalpha(c))
{
putc(c,f2);
c=getc(f1);

while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}

putc(' ',f2);
ungetc(c,f1);
}
else if(c==' '||c=='\t')
printf(" ");
}
fclose(f2);
fclose(f1);
f2=fopen("identifier","r");
k=0;
printf("The Identifiers and keywords are listed below:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
Recognize_keyword_identifier(str);
k=0;
}
}
fclose(f2);
}

 OUTPUT
Enter the program : while
while is key word
4).
 OBJECTIVE
Write a program to ignore the comments in the given input source program and print out the
remaining program without comments.

 RESOURCE
Codeblocks
 PROGRAM LOGIC:
-Read input string
Then check if the first and the second character is /
then continue or check if the first and the second characters are / and * respectively then
find the last two characters and check if they are * and / respectively then call continue
function
Unless output it is not comment

 PROGRAM

#include<string.h>
#include<ctype.h>
#include<stdio.h>
// sample input for ignoring comments
/*
#include<iostream.h>
void play(){
printf("playing COD");
}
// testp
int main(){
int a=01203.5, b=2;
a++;
}

/*
this is to test of
comment reading
in multiple lines
*/

main()
{
FILE *f1,*f2,*f3, *f4;
char c,str[10],st1[10], com[1000];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0,z;
printf("\nEnter the c program");/*gets(st1);*/

// input stands for the directory the file is in


// for example >>C:\Users\abrham\Documents\cpp\first.cpp
f1=fopen("input","w");

f2=fopen("code","w");
while((c=getchar())!='$')
putc(c,f1);
fclose(f1);
f1=fopen("input","r");

f4=fopen("Comment","w");
while((c=getc(f1))!=EOF)
{

if(c=='/')
{
c=getc(f1);
if(c=='/')
{
c=getc(f1);
putc('\n',f4);
while(c!='\n')
{
putc(c,f4);
c=getc(f1);
}
}
else if(c=='*')
{
putc('\n',f4);
while((c=getc(f1))!=EOF)
{
if(c=='*')
{
c=getc(f1);
if(c=='/')
{
break;
}
ungetc(c,f1);
}
putc(c,f4);
}
}
else
{
ungetc(c,f1);
putc(c,f2);
}
}
else
{
putc(c,f2);
}
}
fclose(f4);
fclose(f2);
fclose(f1);

/////////////////////////////////////
f4=fopen("code","r");
while((c=getc(f4))!=EOF)
{

printf("%c",c);
}
printf("\n");

fclose(f4);

}
 OUTPUT
Enter comment: If (x==5)
break;
It is not a comment

You might also like