0% found this document useful (0 votes)
79 views

Sonu Yadav: Algorithm:-1 Aim: Algorithm To Find Whether Given String Is Keyword or Not

The document describes three algorithms: 1. An algorithm to determine if a given string is a keyword or not by comparing it to an array of keywords. 2. An algorithm to determine if a given string is an identifier by checking characters and data types. 3. An algorithm to determine if a given string is a constant by checking if it contains only digits.

Uploaded by

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

Sonu Yadav: Algorithm:-1 Aim: Algorithm To Find Whether Given String Is Keyword or Not

The document describes three algorithms: 1. An algorithm to determine if a given string is a keyword or not by comparing it to an array of keywords. 2. An algorithm to determine if a given string is an identifier by checking characters and data types. 3. An algorithm to determine if a given string is a constant by checking if it contains only digits.

Uploaded by

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

SONU YADAV 16775

ALGORITHM:-1

AIM: ALGORITHM TO FIND WHETHER GIVEN STRING IS KEYWORD OR NOT.

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

AIM: PROGRAM TO FIND WHETHER GIVEN STRING IS KEYWORD OR NOT.

#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();

puts("Enter the string :: ");


gets(str);

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

Enter the string ::


printf
Keyword

Enter the string ::


vikas
String
SONU YADAV 16775
ALGORITHM:-2

AIM: ALGORITHM TO FIND WHETHER GIVEN STRING IS IDENTIFIER OR NOT.

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

AIM: PROGRAM TO FIND WHETHER GIVEN STRING IS IDENTIFIER OR NOT.

#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();

puts("Enter the string :: ");


gets(str);

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

Enter the string ::


printf
Identifier

Enter the string ::


123scanf
Not Identifier

Enter the string ::


printf_scanf
Not Identifier
SONU YADAV 16775
ALGORITHM:-3

AIM: ALGORITHM TO FIND WHETHER GIVEN STRING IS CONSTANT OR NOT.

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

AIM: PROGRAM TO FIND WHETHER GIVEN STRING IS CONSTANT OR NOT.

#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

Enter the value ::


123
Value is constant

Enter the value ::


vikas
Value is a variable
SONU YADAV 16775

PROGRAM:-4

AIM: STUDY OF LEX AND YACC TOOLS.

LEX-THE LEXICAL ANALYZER

STEP I:-

The Lex utility generates a 'C' code, which is yylex () function, which can be used as an
interface to YACC.

The General Format of a Lex File consists of three sections:


1. Definitions
2. Rules
3. User Subroutines
Definitions consists of any external 'C' definitions used in the lex actions or subroutines. e.g. all
preprocessor directives like #include, #define macros etc. These are simply copied to the
lex.yy.c file. The other type of definitions is Lex definitions, which are essentially the lex
substitution strings, lex start states and lex table size declarations.

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.

We terminate the code section with '%%' again.


SONU YADAV 16775

YACC YET ANOTHER COMPILER COMPILER

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

AIM: TO CONVERT REGULAR EXPRESSION INTO NFA.

PROGRAM:

Input: A regular expression R over alphabet .

Output: A NFA n accepting the language denoted by R

Method: We first decompose R into the primitive components. For each component we
construct a finite automaton as follows:-

1. For we construct the NFA

i f

2. For a in we construct the NFA

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.

For regular expression R1/R2 we construct the composite NFA:-

N1

i f


SONU YADAV 16775

N2

For R1, R2 we construct the composite NFA

i N1 N2 f

For R1* we construct the composite NFA

i f

where i is initial state and,

f is final state.
SONU YADAV 16775

PROGRAM:-6

AIM: AN ALGO TO CONVERT NFA TO DFA

Input: An NFA N.

Output: A DFA D accepting the same language

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.

initially, -closure(s0) is the only state in Dstates and it is unmarked. while


there is a n unmarked state T in Dstates do begin
mark T;
for each input symbol a do begin U:=
-closure(move(T,a)); if U is
not in Dstates then
add U as unmarked state in Dstates
Dtran[T,a]:=U
end
end

The Subset Construction

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.

push all states in T onto stack;


initialize -closure(T) to T; while
stack is not empty do begin
pop t, the top element, off of stack;
for each state u with an edge from t to u labeled do if u is
not in -closure(T) do begin
add u to -closure(T);
push u onto stack;
end
end

Computation of -closure

The computation of -closure(T) is a typical process of searching a graph for nodes


reachable from a given set of nodes. In this case the states of T are the given set of nodes, and
the graph consists of just the -labeled edges of the NFA. A simple algorithm to compute -
closure(T) uses a stack to hold states whose edges have not been checked for -labeled
transitions. Such a procedure is shown in
Computation of -closure
SONU YADAV 16775
PROGRAM:-7

AIM: MINIMIZATION THE NUMBER OF STATES OF A DFA

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.

for each group G of do begin


partition G into subgroups such that two states s and t
of G are in the same subgroup if and only if for all
input symbols a, states s and t have transitions on a
to states in the same group of ;
replace G in new by the set of all subgroups formed
end

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();

printf("Enter the Sentence (add '$' at the end) :: \n\n");

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);

printf("\n\nNo. of lines = %d",flag);

getch();
}
SONU YADAV 16775
Output

Enter the Sentence (add '$' at the end) ::

vikas kapoor
hello world
welcome$

Altered Sentence ::

vikas;kapoor
hello"world
welcome

No. of lines = 3
SONU YADAV 16775
ALGORITHM:-9

AIM: ALGORITHM TO GENERATE TOKENS FOR THE GIVEN GRAMMER

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

AIM : PROGRAM TO GENERATE TOKENS FOR THE GIVEN GRAMMER

#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

Input the string :: (12+23*34)


Corresponding Tokens are :: 4121315
SONU YADAV 16775
ALGORITHM=10

AIM : WAP To CHECK IF STRING IS IN GRAMMER OR NOT.

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

AIM : WAP To CHECK IF STRING IS IN GRAMMER OR NOT.

#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

Input the expression=(23+) 4625 4625 4625 4625 4625

It is not in grammer.

Input the expression=(2+(3+4)+5) 46246265265


46246265265
46246265265
46246265265
46246265265
462462652
462462652
462462652
462462652
462462652
462462
462462
462462
462462
462462
462
462
462
462
462
6
6
6
6
6

You might also like