CD Manual
CD Manual
LAB MANUAL
Year : 2021-22
INDEX
Experiment 8 Objective: - Write a lex program to count blank spaces, words, lines in 38
a given file.
JAIPUR ENGINEERING COLLEGE AND RESEARCH CENTRE
41
Experiment 9 Objective: - Write a lex program to count the no. of vowels and
consonants in a C file.
To become a renowned centre of outcome based learning, and work towards academic,
professional, cultural and social enrichment of the lives of individuals and communities.
M1: Focus on evaluation of learning outcomes and motivate students to inculcate research
aptitude by project based learning.
M2: Identify, based on informed perception of Indian, regional and global needs, areas of focus
and provide platform to gain knowledge and solutions.
M4: Develop human potential to its fullest extent so that intellectually capable and
imaginatively gifted leaders can emerge in a range of professions.
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
5|Page
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
6|Page
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
7|Page
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
PSO1: Ability to interpret and analyze network specific and cyber security issues in
real world environment.
PSO2: Ability to design and develop mobile and web based application under
realistic constraints.
8|Page
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Course Outcomes
CO1. Apply the concept of Lexical Analyzer using C/C++ and FLEX tool
CO2. Analyze different types of parsing and allocation data structure
Experiment CO’s BT
Objective
No.
9|Page
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Exp:-8 4
Objective: - Write a lex program to count the no. of CO1
vowels and consonants in a C file.
Objective: -Write a YACC program to recognize 4
Exp:-9 CO2
strings aaab, abbb using a^nb^n, where b>=0.
Objective: Write a YACC program to evaluate an 5
Exp:10 arithmetic expression involving operators +,-,* and CO2
/.
Exp:11 Objective: - Write a YACC program to recognize CO2 4
string with grammar a^n b^n, n>=0
Objective: - Write a C program to find first of any 4
CO2
Exp:12 grammar.
* BT - Bloom's Taxonomy
MAPPING OF CO & PO
I 3 3 3 2 3 - - - 1 1 2 2
II 3 3 3 2 2 - - - 1 1 2 2
10 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Compiler is a software which takes as input a program written in a High-Level language and
translates it into its equivalent program in Low Level program. Compilers teaches us how real-
world applications are working and how to design them. Learning Compilers gives us with
both theoretical and practical knowledge that is crucial in order to implement a programming
language. It gives you a new level of understanding of a language in order to make better use
of the language (optimization is just one example). Sometimes just using a compiler is not
enough. You need to optimize the compiler itself for your application. Compilers have a
general structure that can be applied in many other applications, from debuggers to simulators
to 3D applications to a browser and even a cmd / shell. understanding compilers and how they
work makes it super simple to understand all the rest. a bit like a deep understanding of math
will help you to understand geometry or physics. We cannot do physics without the math. not
on the same level. Just using something (read: tool, device, software, programming language)
is usually enough when everything goes as expected. But if something goes wrong, only a true
understanding of the inner workings and details will help to fix it. Even more specifically,
Compilers are super elaborated / sophisticated systems (architecturally speaking). If you will
say that can or have written a compiler by yourself - there will be no doubt as to your
capabilities as a programmer. There is nothing you cannot do in the Software realm. So, better
be a pilot who have the knowledge and mechanics of an airplane than the one who just know
how to fly. Every computer scientist can do much better if have knowledge of compilers apart
from the domain and technical knowledge. Compiler design lab provides deep understanding
of how programming language Syntax, Semantics are used in translation into machine
equivalents apart from the knowledge of various compiler generation tools like LEX,YACC
etc.
11 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
INSTRUCTIONS OF LAB
1.1.1.1 DO’s
1. Please switch off the Mobile/Cell phone before entering Lab.
2. Enter the Lab with complete source code and data.
3. Check whether all peripheral are available at your desktop before proceeding for
program.
4. Intimate the lab in charge whenever you are incompatible in using the system or
in case software get corrupted/ infected by virus.
5. Arrange all the peripheral and seats before leaving the lab.
6. Properly shutdown the system before leaving the lab.
7. Keep the bag outside in the racks.
8. Enter the lab on time and leave at proper time.
9. Maintain the decorum of the lab.
10. Utilize lab hours in the corresponding experiment.
11. Get your CD / Pen drive checked by lab in charge before using it in the lab.
1.1.1.2 DON’TS
1. No one is allowed to bring storage devices like Pan Drive /Floppy etc. in the
lab.
2. Don’t mishandle the system.
3. Don’t leave the system on standing for long
4. Don’t bring any external material in the lab.
5. Don’t make noise in the lab.
6. Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
7. Don’t enter in the lab without permission of lab in charge.
8. Don’t litter in the lab.
12 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
13 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No.1
Aim: - Introduction: Objective, scope and outcome of the course.
14 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 2
Aim: To identify whether given string is keyword or not.
Source Code:-
01 #include<stdio.h>
02
03 #include<conio.h>
04
05 #include<string.h>
06
07 void main()
08
09 {
15 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
10
11 char keyword[32][10]={"auto","double","int","struct","break","else","long",
12
13 "switch","case","enum","register","typedef","char",
14
15 "extern","return","union","const","float","short",
16
17 "unsigned","continue","for","signed","void","default",
18
19 "goto","sizeof","voltile","do","if","static","while"} ;
20
21 char string[10];
22
23 int flag=0,i;
24
25 printf("enter any string:");
26
27 gets(string);
28
29 for(i=0;i<32;i++)
30
31 {
32
33 if(strcmp(string,keyword[i])==0)
34
35 {
36
37 flag=1;
38
39 }
40
16 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
41
42
43 }
44
45
46
47 if(flag==1)
48
49 printf("%s is a keyword",string);
50
51 else
52
53 printf("%s is not a keyword",string);
54
55
56
57 getch();
58
59 }
Output:
17 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 3
Aim: Write a C program to count total no. of keywords in a file. [Taking file from user]
Source Code:-
1. * using Array Structure
2. */
3. #include <stdio.h>
4. #include <string.h>
5. #include <ctype.h>
6. #define KEYMAX 32
7.
8. struct keyword
9. {
10. char word[10];
11. int occur;
12. };
13.
14. int binarysearch(char [], struct keyword[]);
15.
16. int main()
17. {
18. int i = 0, j = 0, pos;
18 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
72. {
73. int low, high, mid;
74.
75. low = 0;
76. high = KEYMAX - 1;
77. while (low <= high)
78. {
79. mid = (low + high) / 2;
80. if (strcmp(word, key[mid].word) < 0)
81. {
82. high = mid - 1;
83. }
84. else if (strcmp(word, key[mid].word) > 0)
85. {
86. low = mid + 1;
87. }
88. else
89. {
90. return mid;
91. }
92. }
93.
94. return -1;
95. }
Output :-
21 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
$ gcc keywordoccur.c
$ ./a.out
Enter string: break, float and double are c keywords. float and double are primitive data type
s. The string entered is: break, float and double are c keywords. float and double are primiti
ve data types.
***********************
Keyword Count
***********************
break 1
double 2
float 2
Experiment No.4
Aim: Write a program to count total no of operators in a file. [Taking file from user].
Source Code:-
#include<stdio.h>
#include<conio.h>
int main()
{
char s[200];
int count = 0, i;
printf(“Enter the string:\n”);
scanf(“%[^\n]s”, s)’;
for ( i = 0; s[ i ] != ‘\0’ ; i++ )
{
if ( s[ i ] = = ‘+’ | | s[ i ] = = ‘-’ | | s[ i ] = = ‘*’ | | s[ i ] = = ‘/’ | | )
22 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
count++;
}
printf(“Number of Operators in a given string are:%d\n”, count);
}
Experiment No. 5
Aim: Write a C Program to Count total occurrence of each character in a given file.
[Taking file from user]
Description: C program to find the frequency of characters in a string: This program counts
the frequency of characters in a string, i.e., which character is present how many times in the
string. For example, in the string "code" each of the characters 'c,' 'd,' 'e,' and 'o' has occurred
one time. Only lower case alphabets are considered, other characters (uppercase and special
characters) are ignored. You can easily modify this program to handle uppercase and special
symbols.
Source Code:
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[100];
7. int c = 0, count[26] = {0}, x;
23 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
8.
9. printf("Enter a string\n");
10. gets(string);
11.
12. while (string[c] != '\0') {
13. /** Considering characters from 'a' to 'z' only and ignoring others. */
14.
15. if (string[c] >= 'a' && string[c] <= 'z') {
16. x = string[c] - 'a';
17. count[x]++;
18. }
19.
20. c++;
21. }
22.
23. for (c = 0; c < 26; c++)
24. printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
25.
26. return 0;
27. }
Output:
24 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 6
Aim: Write a C program to insert, delete and display the entries in Symbol Table.
Source code:-
1. #include<stdio.h>
2. #include<conio.h>
3. #include<alloc.h>
4. #include<string.h>
5. #include<stdlib.h>
6. #define NULL 0
7. int size=0;
8. void Insert();
9. void Display();
10. void Delete();
11. int Search(char lab[]);void Modify();
12. struct SymbTab
13. {
25 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
53. case 6:
54. exit(0);
55. }
56. }while(op<6);
57. getch();
58. }
59. void Insert()
60. {
61. int n;
62. char l[10];
63. printf(“\n\tEnter the label : “);
64. scanf(“%s”,l);
65. n=Search(l);
66. if(n==1)
67. printf(“\n\tThe label exists already in the symbol table\n\tDuplicate can’t be inserted”);
68. else
69. {
70. struct SymbTab *p;
71. p=malloc(sizeof(struct SymbTab));
72. strcpy(p->label,l);
73. printf(“\n\tEnter the symbol : “);
74. scanf(“%s”,p->symbol);
75. printf(“\n\tEnter the address : “);
76. scanf(“%d”,&p->addr);
77. p->next=NULL;
78. if(size==0)
79. {
80. first=p;
81. last=p;
82. }
83. else
84. {
85. last->next=p;
86. last=p;
87. }
88. size++;
89. }
90. printf(“\n\tLabel inserted\n”);
91. }
92. void Display()
27 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
93. {
94. int i;
95. struct SymbTab *p;
96. p=first;
97. printf(“\n\tLABEL\t\tSYMBOL\t\tADDRESS\n”);
98. for(i=0;i<size;i++)
99. {
100. printf(“\t%s\t\t%s\t\t%d\n”,p->label,p->symbol,p->addr);
101. p=p->next;
102. }
103. }
104. int Search(char lab[])
105. {
106. int i,flag=0;
107. struct SymbTab *p;
108. p=first;
109. for(i=0;i<size;i++)
110. {
111. if(strcmp(p->label,lab)==0)
112. flag=1;
113. p=p->next;
114. }
115. return flag;
116. }
117. void Modify()
118. {
119. char l[10],nl[10];
120. int add,choice,i,s;
121. struct SymbTab *p;
122. p=first;
123. printf(“\n\tWhat do you want to modify?\n”);
124. printf(“\n\t1.Only the label\n\t2.Only the address\n\t3.Both the label and address\n”);
125. printf(“\tEnter your choice : “);
126. scanf(“%d”,&choice);
127. switch(choice)
128. {
129. case 1:
130. printf(“\n\tEnter the old label : “);
131. scanf(“%s”,l);
132. s=Search(l);
28 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
133. if(s==0)
134. printf(“\n\tLabel not found\n”);
135. else
136. {
137. printf(“\n\tEnter the new label : “);
138. scanf(“%s”,nl);
139. for(i=0;i<size;i++)
140. {
141. if(strcmp(p->label,l)==0)
142. strcpy(p->label,nl);
143. p=p->next;
144. }
145. printf(“\n\tAfter Modification:\n”);
146. Display();
147. }
148. break;
149. case 2:
150. printf(“\n\tEnter the label where the address is to be modified : “);
151. scanf(“%s”,l);
152. s=Search(l);
153. if(s==0)
154. printf(“\n\tLabel not found\n”);
155. else
156. {
157. printf(“\n\tEnter the new address : “);
158. scanf(“%d”,&add);
159. for(i=0;i<size;i++)
160. {
161. if(strcmp(p->label,l)==0)
162. p->addr=add;
163. p=p->next;
164. }
165. printf(“\n\tAfter Modification:\n”);
166. Display();
167. }
168. break;
169. case 3:
170. printf(“\n\tEnter the old label : “);
171. scanf(“%s”,l);
172. s=Search(l);
29 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
173. if(s==0)
174. printf(“\n\tLabel not found\n”);
175. else
176. {
177. printf(“\n\tEnter the new label : “);
178. scanf(“%s”,nl);
179. printf(“\n\tEnter the new address : “);
180. scanf(“%d”,&add);
181. for(i=0;i<size;i++)
182. {
183. if(strcmp(p->label,l)==0)
184. {
185. strcpy(p->label,nl);
186. p->addr=add;
187. }
188. p=p->next;
189. }
190. printf(“\n\tAfter Modification:\n”);
191. Display();
192. }
193. break;
194. }
195. }
196. void Delete()
197. {
198. int a;
199. char l[10];
200. struct SymbTab *p,*q;
201. p=first;
202. printf(“\n\tEnter the label to be deleted : “);
203. scanf(“%s”,l);
204. a=Search(l);
205. if(a==0)
206. printf(“\n\tLabel not found\n”);
207. else
208. {
209. if(strcmp(first->label,l)==0)
210. first=first->next;
211. else if(strcmp(last->label,l)==0)
212. {
30 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
213. q=p->next;
214. while(strcmp(q->label,l)!=0)
215. {
216. p=p->next;
217. q=q->next;
218. }
219. p->next=NULL;
220. last=p;
221. }
222. else
223. {
224. q=p->next;
225. while(strcmp(q->label,l)!=0)
226. {
227. p=p->next;
228. q=q->next;
229. }
230. p->next=q->next;
231. }
232. size–;
233. printf(“\n\tAfter Deletion:\n”);
234. Display();
235. }
236. }
Experiment No. 7
31 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Source Code:-
Output:
Input: 7017175023
Output: Mobile Number Valid
32 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Input: 0001112223
Output: Mobile Number Invalid
2. Valid 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");
}
Output:
Input: geeksforgeeks
Output: INVALID URL
Input: https://www.geeksforgeeks.org
Output: VALID URL
3. Valid identifier
%{
34 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
#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();
}
Output:
%{
35 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
/* Definition section */
#include<stdio.h>
int i=0, yr=0, valid=0;
%}
/* Rule Section */
%%
([0-2][0-9]|[3][0-1])\/((0(1|3|5|7|8))|(10|12))
\/([1-2][0-9][0-9][-0-9]) {valid=1;}
([0-2][0-9]|30)\/((0(4|6|9))|11)
\/([1-2][0-9][0-9][0-9]) {valid=1;}
([0-1][0-9]|2[0-8])\/02
\/([1-2][0-9][0-9][0-9]) {valid=1;}
29\/02\/([1-2][0-9][0-9][0-9])
{ while(yytext[i]!='/')i++; i++;
while(yytext[i]!='/')i++;i++;
while(i<yyleng)yr=(10*yr)+(yytext[i++]-'0');
if(yr%4==0||(yr%100==0&&yr%400!=0))valid=1;}
%%
// driver code
main()
{
yyin=fopen("vpn.txt", "r");
yylex();
if(valid==1) printf("It is a valid date\n");
else printf("It is not a valid date\n");
}
int yywrap()
36 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
{
return 1;
}
Output:
Input: 02/05/2019
Output: It is a valid date
Input: 05/20/2019
Output: It is not a valid date
Experiment No.8
Aim: Write a lex program to count blank spaces, words, lines in a given file.
Source Code:-
%{
#include<stdio.h>
int lc=0, sc=0, tc=0, ch=0; /*Global variables*/
%}
/*Rule Section*/
%%
37 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
38 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Output:
39 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 9
Aim: Write a lex program to count the no. of vowels and consonants in a C file.
Source Code:-
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
main(){
printf("Enter the string of vowels and consonents:");
yylex();
printf("The number of vowels are: %d\n",vow);
printf("The number of consonants are: %d\n",cons);
return 0;
}
Output:
$$ lex kk.l
$$ gcc kk.c -ll
$$ ./a.out
Enter the string of vowels and consonents
40 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
My name is Khan
The number of vowels are: 4 The number of consonants are: 8
Experiment No. 10
Aim: Write a YACC program to recognize strings aaab, abbb using a^nb^n, where
b>=0.
Source Code:-
%{
/* Definition section */
#include "y.tab.h"
%}
/* Rule Section */
%%
[aA] {return A;}
[bB] {return B;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
Parser Source Code :
%{
41 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
/* Definition section */
#include<stdio.h>
#include<stdlib.h>
%}
%token A B NL
/* Rule Section */
%%
stmt: S NL { printf("valid string\n");
exit(0); }
;
S: A S B |
;
%%
int yyerror(char *msg)
{
printf("invalid string\n");
exit(0);
}
//driver code
main()
{
printf("enter the string\n");
yyparse();
}
Output:
42 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 11
Source Code:-
%
{
/* Definition section*/
#include "y.tab.h"
extern yylval;
%
}
43 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
%%
[0 - 9]
+
{
yylval = atoi(yytext);
return NUMBER;
}
[a - zA - Z] + { return ID; }
[\t] + ;
\n { return 0; }
. { return yytext[0]; }
%%
Parser Source Code:
%{
/* Definition section */
#include
%
}
% token NUMBER ID
// setting the precedence
// and associativity of operators
% left '+' '-'
% left '*' '/'
/* Rule Section */
%
%E:T
44 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
{
printf("Result = %d\n", $$);
return 0;
}
T : T '+' T { $$ = $1 + $3; }
| T '-' T { $$ = $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| '-' NUMBER { $$ = -$2; }
| '-' ID { $$ = -$2; }
| '(' T ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| ID { $$ = $1; };
%%
int main()
{
printf("Enter the expression\n");
yyparse();
}
/* For printing error messages */
int yyerror(char* s)
{
printf("\nExpression is invalid\n");
}
Output:
45 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Experiment No. 12
Source Code:-
YACC PART:
CODE: (gram.y)
46 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B NL
%%
stmt: S NL {printf("valid string\n");
exit(0);}
;
S: A S B |
;
%%
int yyerror(char *msg)
{
printf("invalid string\n");
exit(0);
}
main()
{
printf("enter the string\n");
yyparse();
}
LEX PART:
CODE: (gram.l)
%{
47 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
#include "y.tab.h"
%}
%%
[aA] {return A;}
[bB] {return B;}
\n {return NL;}
. {return yytext[0];}
%%
OUTPUT :
yacc -d gram.y
lex gram.l
cc y.tab.c lex.yy.c -ly -ll
./a.out
enter the string
ab
valid string
./a.out
enter the string
aaabb
invalid string
./a.out
enter the string
aabb
48 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
valid string
./a.out
enter the string
a
invalid string
Experiment No. 13
The functions follow and follow first are both involved in the calculation of the Follow Set of
a given Non-Terminal. The follow set of the start symbol will always contain “$”. Now the
calculation of Follow falls under three broad cases:
Explanation:
Store the grammar on a 2D character array production. Find first function is for
calculating the first of any non-terminal. Calculation of first falls under two broad cases:
If the first symbol in the R.H.S of the production is a Terminal then it can directly be
included in the first set.
If the first symbol in the R.H.S of the production is a Non-Terminal then call the find
first function again on that Non-Terminal. To handle these cases like Recursion is the
best possible solution. Here again, if the First of the new Non-Terminal contains an
epsilon then we have to move to the next symbol of the original production which can
again be a Terminal or a Non-Terminal.
Note: For the second case it is very easy to fall prey to an INFINITE LOOP even if the code
looks perfect. So it is important to keep track of all the function calls at all times and never call
the same function again.
Below is the implementation:
Source Code:-
50 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int count, n = 0;
char calc_first[10][100];
char calc_follow[10][100];
int m = 0;
51 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
char production[10][10];
int k;
char ck;
int e;
int jm = 0;
int km = 0;
int i, choice;
char c, ch;
count = 8;
strcpy(production[0], "E=TR");
strcpy(production[1], "R=+TR");
strcpy(production[2], "R=#");
strcpy(production[3], "T=FY");
strcpy(production[4], "Y=*FY");
strcpy(production[5], "Y=#");
52 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
strcpy(production[6], "F=(E)");
strcpy(production[7], "F=i");
int kay;
char done[count];
calc_first[k][kay] = '!';
c = production[k][0];
point2 = 0;
xxx = 0;
53 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
if(c == done[kay])
xxx = 1;
if (xxx == 1)
continue;
// Function call
findfirst(c, 0, 0);
ptr += 1;
done[ptr] = c;
calc_first[point1][point2++] = c;
if (first[i] == calc_first[point1][lark])
chk = 1;
54 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
break;
if(chk == 0)
calc_first[point1][point2++] = first[i];
printf("}\n");
jm = n;
point1++;
printf("\n");
printf("-----------------------------------------------\n\n");
char donee[count];
ptr = -1;
55 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
calc_follow[k][kay] = '!';
point1 = 0;
int land = 0;
ck = production[e][0];
point2 = 0;
xxx = 0;
// Checking if Follow of ck
if(ck == donee[kay])
xxx = 1;
if (xxx == 1)
continue;
56 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
land += 1;
// Function call
follow(ck);
ptr += 1;
donee[ptr] = ck;
calc_follow[point1][point2++] = ck;
if (f[i] == calc_follow[point1][lark])
chk = 1;
break;
57 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
if(chk == 0)
calc_follow[point1][point2++] = f[i];
printf(" }\n\n");
km = m;
point1++;
void follow(char c)
int i, j;
if(production[0][0] == c) {
f[m++] = '$';
58 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
if(production[i][j] == c)
if(production[i][j+1] != '\0')
followfirst(production[i][j+1], i, (j+2));
follow(production[i][0]);
59 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
int j;
// encounter a Terminal
if(!(isupper(c))) {
first[n++] = c;
if(production[j][0] == c)
if(production[j][2] == '#')
if(production[q1][q2] == '\0')
first[n++] = '#';
60 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
else
first[n++] = '#';
else if(!isupper(production[j][2]))
first[n++] = production[j][2];
else
// at the beginning
61 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
findfirst(production[j][2], j, 3);
int k;
// a Terminal
if(!(isupper(c)))
f[m++] = c;
else
int i = 0, j = 1;
if(calc_first[i][0] == c)
break;
62 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
while(calc_first[i][j] != '!')
if(calc_first[i][j] != '#')
f[m++] = calc_first[i][j];
else
if(production[c1][c2] == '\0')
// end of a production
follow(production[c1][0]);
else
63 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
j++;
Output:
First(E)= { (, i, }
First(R)= { +, #, }
First(T)= { (, i, }
First(Y)= { *, #, }
First(F)= { (, i, }
-----------------------------------------------
Follow(E) = { $, ), }
Follow(R) = { $, ), }
64 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
Follow(T) = { +, $, ), }
Follow(Y) = { +, $, ), }
Follow(F) = { *, +, $, ), }
65 | P a g e
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.
iv
Jaipur Engineering college and research centre, Shri Ram
ki Nangal, via Sitapura RIICO Jaipur- 302 022.