Sonu Yadav: Algorithm:-1 Aim: Algorithm To Find Whether Given String Is Keyword or Not
Sonu Yadav: Algorithm:-1 Aim: Algorithm To Find Whether Given String Is Keyword or Not
ALGORITHM:-1
1. Start
2. Declare i, flag as int and str[10] as char
3. Declare array a[5][10]={printf,scanf,if,else,break}
4. Allow user to enter the string
5. Initialize the loop i=0 to i<strlen(str)
a. Compare str and a[i]
b. If same, assign flag=1 else flag=0
6. If flag=1, print Keyword else print String
7. Stop
SONU YADAV 16775
PROGRAM NO:-1
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[5][10]={"printf","scanf","if","else","break"}; char
str[10];
int i,flag;
clrscr();
for(i=0;i<strlen(str);i++)
{
if(strcmp(str,a[i])==0)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==1)
puts("Keyword");
else
puts("String");
getch();
}
SONU YADAV 16775
Output
1. Start
2. Declare i,j,flag=1,len as int and str[10] as char
3. Allow the user to enter the string
4. Initialize the loop i=0,j=1 to i<len
b. if str[j] is digit, assign flag=0
c. else if str[i] is alphabet, increment flag
1) if str[i] is alphanum, increment flag
2) else if str[i] is not digit, assign flag=0
3) else increment flag
d. else if str[i] is not alphanum, assign flag=0
5. if flag==0, print Identifier else print Not Identifier
6. Stop
SONU YADAV 16775
PROGRAM NO:-2
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
int i,j,flag=1,len;
char str[10];
clrscr();
len=strlen(str);
for(i=0,j=1;i<len;i++)
{
if(isdigit(str[j]))
{
flag=0;
break;
}
else if(isalpha(str[i]))
{
{
flag++;
continue;
}
SONU YADAV 16775
if(isalnum(str[i]))
{
flag++;
continue;
}
else if(!isdigit(str[i]))
{
flag=0;
break;
}
else
flag++;
}
else if(!isalnum(str[i]))
{
flag=0;
break;
}
if(flag==0)
puts("Not Identifier");
else
puts("Identifier");
getch();
}
SONU YADAV 16775
Output
1. Start
2. Declare i, flag as int and a[5] as char
3. Allow user to enter the value
4. Initialize the loop from i=0 to i<strlen(a)
a. if a[i] is digit, assign flag=1
b. else assign flag=0
5. if flag==1, print Value is Constant else print Value is Variable
6. Stop
SONU YADAV 16775
PROGRAM:-3
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i,flag;
char a[5];
clrscr();
puts("Enter the value :: ");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(isdigit(a[i]))
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
puts("Value is constant");
)() else
puts("Value is a variable");
getch();
}
SONU YADAV 16775
Output
PROGRAM:-4
STEP I:-
The Lex utility generates a 'C' code, which is yylex () function, which can be used as an
interface to YACC.
The Rules is the basic part, which specifies the regular expressions and their corresponding
actions. The User Subroutines are the function definitions of the functions that are used in the
Lex actions.
Step II:-
The program Lex generates `Lexer'. This is a function that takes a stream of characters as its
input, and whenever it sees a group of characters that match a key, takes a certain action.e.g:
%{
#include <stdio.h>
%}
%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
The first section, in between the %{ and %} pair is included directly in the output program. This
is required, because we use printf, which is defined in stdio.h. Sections are separated using '%%',
so the first line of the second section starts with the 'stop' key. Whenever the 'stop' key is
encountered in the input, the rest of the line (a printf() call) is executed. Besides 'stop', we've
also defined 'start', which otherwise does mostly the same.
Step I:-
Yacc is the Utility, which generates the function 'yyparse' which is indeed the Parser. Yacc
describes a context free, LALR(1) grammar and supports both bottom-up and top-down parsing.
The general format for the YACC file is very similar to that of the Lex file.
1. Declarations
2. Grammar Rules
3. Subroutines
Step II:-
YACC can parse input streams consisting of tokens with certain values. This describes the
relation YACC has with Lex, YACC has no idea what 'input streams' are, it needs preprocessed
tokens. While you can write your own Tokenizer, we will leave that entirely up to Lex.
YACC tool was used to parse input files for compilers: programs. Programs written in a
programming language for computers are typically *not* ambiguous - they have just one
meaning. As such, YACC does not cope with ambiguity and will complain about shift/reduce or
reduce/reduce conflicts.
SONU YADAV 16775
PROGRAM:-5
PROGRAM:
Method: We first decompose R into the primitive components. For each component we
construct a finite automaton as follows:-
i f
i f
3. Having constructed components for the basic regular expressions, we proceed to combine
them in ways that correspond to the way compounded regular expressions are formed from
small regular expressions.
N1
i f
SONU YADAV 16775
N2
i N1 N2 f
i f
f is final state.
SONU YADAV 16775
PROGRAM:-6
Input: An NFA N.
Method: Our algorithm constructs a transition table Dtran for D. Each DFA
state is a set of NFA states and we construct Dtran so that D will simulate in parallel all
possible moves N can make on a given input string
Before it sees the first input symbol, N can be in any of the states in the set -closure(s0),
where s0 is the start state of N. Suppose that exactly the states in set T are reachable from s0 on
a given sequence of input symbols, and let a be the next input symbol. On seeing a, N can move
to any of the states in the set move(T,a). When we allow for -transitions, N can be in any of the
states in -closure(move(T,a)), after seeing the a.
We construct Dstates, the set of states of D, and Dtran, the transition table for D, in the
following manner. Each state of D corresponds to a set of NFA states that N could be in after
reading some sequence of input symbols including all possible -transitions before or after
symbols are read. The start state of D is -closure(s0). States and transitions are added to D
using algorithm of Subset Constructions. A
SONU YADAV 16775
state of D is an accepting state if it is a set of NFA states containing at least one accepting
state of N.
Computation of -closure
Input: A DFA M with set of states S, set of inputs , transitions defined for all states
and inputs, start state s0, and set of accepting states F.
Output: A DFA M accepting the same language as M and having as few states as
possible.
Method:
1. Construct as initial partition of set of states with two groups: the accepting
states F and the non accepting states S-F
2. Apply the procedure to to construct a new partition new.
3. If new = , let final = and continue with step (4). Otherwise, repeat
step (2) with = new.
4. Choose one state in each group of the partition final as the representative for
that group. The representatives will be the states of the reduced DFA M.
Let s be a representative state, and suppose on input a there is a transition of M
from s to t. let r be the representative of ts group. Then M has a transition from
s to r on a. Let the start state of M be the representative of the group containing
the start state s0 of M, and let the accepting states f M be the representatives
that are in F. Note that each group of final either consists only of states in F or
has no states in F.
5. If M has a dead state, that is, a state d that is not accepting and that has
transitions to itself on all input symbols, then remove d from M. Also
remove any states not reachable from the start state. Any transitions to d
from other states become undefined.
Construction of new
SONU YADAV 16775
ALGORITHM:-8
AIM :PROGRAM TO COUNT BLANK SPACE AND COUNT THE NO. OF LINES.
1. Start
2. Declare flag=1 as int and I,j=0,temp[100] as char.
3. Allow the user to enter the sentence
4. Read the sentence until it is not empty
b. if i== , replace i with ;
c. else if i==\t, replace i with ,
d. else if i==\n, increment the value of flag
5. Assign temp[j++]=i and temp[j]=NULL
6. Print the value of temp for removing blanks
7. Print the value of flag for counting the no. of lines
8. Stop
SONU YADAV 16775
PROGRAM:-8
AIM: PROGRAM TO COUNT BLANK SPACE AND COUNT THE NO. OF LINES.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int flag=1;
char i,j=0,temp[100];
clrscr();
while((i=getchar())!='$')
{
if(i==' ')
i=';';
else if(i=='\t')
i='"';
else if(i=='\n')
flag++;
temp[j++]=i;
}
temp[j]=NULL;
printf("\n\n\nAltered Sentence :: \n\n");
puts(temp);
getch();
}
SONU YADAV 16775
Output
vikas kapoor
hello world
welcome$
Altered Sentence ::
vikas;kapoor
hello"world
welcome
No. of lines = 3
SONU YADAV 16775
ALGORITHM:-9
1. Start
2. Declare i as int, str[20] as char
3. Allow user to enter the string
4. Initialize the loop until str[i] is not equal to NULL
a. if str[i]==( || str[i]=={, print 4
b. if str[i]==) || str[i]==}, print 5
c. if str[i] is digit, increment i and print 1
d. if str[i]==+, print 2
e. if str[i]==*, print 3
5. Stop
SONU YADAV 16775
PROGRAM:-9
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
int i=0; char
str[20];
clrscr();
printf(" \n Input the string ::");
gets(str);
printf("Corresponding Tokens are :: ");
while(str[i]!='\0')
{
if((str[i]=='(')||(str[i]=='{'))
{
printf("4");
}
if((str[i]==')')||(str[i]=='}'))
{
printf("5");
}
if(isdigit(str[i]))
{
while(isdigit(str[i]))
{
i++;
}
i--;
printf("1");
SONU YADAV 16775
}
if(str[i]=='+')
{
printf("2");
}
if(str[i]=='*')
{
printf("3");
}
i++;
}
getch();
}
SONU YADAV 16775
Output
1. Start.
2. Declare the character array str[], token, and initialize integer variables a=0, b=0,c,d.
3. Input the string from user.
4. Repeat step 5 to 12 till str[a]='10'.
5. If str[a]=='(' orstr[a]=='{' then token[b]='4',b++.
6. If str[a]==')'or str[a]=='}' then token[b]='5',b++
7. Check if isdigit(str[a]) then repeat step 8 till isdigit(str[a])
8. a++.
9. a--, token[b]='1', b++.
10. If (str[a]=='+') then token[b]='2',b++.
11. If (str[a]=='*') then token[b]='3',b++.
12. a==.
13. token[b]='\0'.
14. Then print the token genarated for string.
15. b=0.
16. Repeat step 17 to 18 till token[b='\0'.
17. If token[b]=='1' then token[b]=='6'.
18. b++
19. Print the token.
20. b=0
21. Repeat step 22 to 31 till token[b]='\0'.
22. c=0.
23. Repeat step 24 to 36 til token[b]=='6'.
24. token[c]='6'.
25. c++
26. Repeat step 27 to 28 till token[c]='0'.
27. token[c]==token[c+2].
28. c++
29. Token[c-2]='\0'.
30. put token.
31. b++
32. Compare token with 6 and result store in d.
33. If d=0, then put the string in the grammer.
34. Else print string is not in the string .
35. Stop.
SONU YADAV 16775
PROGRAM=10
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
int a=0,b=0,c;
char str[20],tok[11];
clrscr();
printf(input the expression=);
gets(str);
while(str[a]!=\0)
{
if((str[a]==))||str[a]=={))
{
tok[b]=4;
b++;
}
if((str[a]==))|str[a]==}))
{
tok[b]=5;
b++;
}
if(isdigit(str[a]))
{
while(isdigit(str[a]))
{
a++;
}
a--;
tok[b]=0;
b++;
}
if(str[a]==+)
{
SONU YADAV 16775
tok[b]=2;
b++;
}
if(str[a]==*)
{
tok[b]=3;
b++;
}
a++;
}
tok[b]=\0;
puts()tok);
b=0;
while(tok[b]!=\0)
{
if(((tok[b]==6)&&(tok[b+1]==2)&&(tok[b+2]==6))||((tok[b]==6)&&(tok[b
+1]==3)&&(tok[b+2]==6))||
((tok[b]==4)&&(tok[b+1]==6)&&(tok[b+2]==5)))
{
tok[b]=6;
c=b+1;
while(tok[c]!=\0)
{
tok[c]=tok[c+2];
c++;
}
tok[c]=\0;
puts(tok);
b=0;
}
else
{
b++;
puts(tok);
}
}
int d;
d=strcmp(tok,6);
if(d==0)
SONU YADAV 16775
{
printf(it is in grammer):
}
else
{
printf(it is not in grammer);
}
getch();
}
SONU YADAV 16775
OUTPUT
It is not in grammer.