Compiler End
Compiler End
A
Term-Work
On
By
Ayush Budhlakoti
2261123
…
Faculty-in-Charge
Mr. Parthak Mehra
Lab Assistant
STUDENT’S DECLARATION
I, Ayush Budhlakoti, hereby declare the work,which is being presented in the term-work, entitled
“Compiler Design Lab” in partial fulfillment of the requirement for the award of the degree B.Tech in the
session 2024-2025, is an authentic record of my own work carried out under the supervision of Mr.
Parthak Mehra.The matter embodied in this term-work has not been submitted by me for the award of
Date:………… ……………….
CERTIFICATE
The term-work entitled “Compiler Design Lab” being submitted by Ayush Budhlakoti
S/o Mr. Vinod Chandra Budhlakoti enrollment no 220111200 Roll no 2261123 to Graphic Era Hill
University Bhimtal Campus for the award of bonafide work carried out by him. He has worked
under my guidance and supervision and fulfilled the requirement for the submission of report.
(…………………) (……………………)
INDEX
Q1. Design a LEX code to count the number of lines space tab meta character and rest of the
characters in each input pattern in C/C++.
Solution:-
%{
#include <stdio.h>
int line_count = 0, space_count = 0, tab_count = 0, meta_count = 0, other_count = 0;
%}
%%
\n { line_count++; }
[ \t] { space_count += (yytext[0] == ' '); tab_count += (yytext[0] == '\t'); }
[!@#$%^&*()_+=\[\]{}|;:'",.<>?/`~\\] { meta_count++; }
. { other_count++; }
%%
int main() {
printf("Enter input (Ctrl+D to stop):\n");
yylex();
printf("Lines: %d\nSpaces: %d\nTabs: %d\nMeta-characters: %d\nOther characters: %d\n",
line_count, space_count, tab_count, meta_count, other_count);
return 0;
}
6
Q2. Design a LEX code to Identify and print valid identifiers of C/C++ in given input pattern.
Solution:-
%{
#include <stdio.h>
%}
%%
[0-9]+\.[0-9]+ { printf("Float: %s\n", yytext); }
[0-9]+ { printf("Integer: %s\n", yytext); }
[ \t\n]+ ;
. ;
%%
int main() {
printf("Enter input (Ctrl+D or Ctrl+Z to end):\n");
yylex();
return 0;
}
int yywrap() {
return 1;
}
7
Q3. Design a LEX code to Identify and print integer and float values in given input pattern.
Solution:-
%{
#include <stdio.h>
%}
%%
// Match float values (e.g., 123.45, .45, 123.)
[+-]?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*) { printf("Float: %s\n", yytext); }
int main() {
printf("Enter input (Ctrl+D to stop):\n");
yylex();
return 0;
}
8
Q4. Design a LEX code for Tokenizing identify and print operators, seprators,keywords, identifiers)
in the C fragment.
Solution:-
%{
#include <stdio.h>
#include <string.h>
int isKeyword(const char* str) {
const char* keywords[] = {
"int", "float", "char", "if", "else", "while", "for", "return",
"void", "main", "double", "break", "continue", "switch", "case",
"default", "do", "struct", "typedef", "union", "enum", "const",
"static", "sizeof", "volatile", "extern", "unsigned", "signed",
"long", "short", "goto", "register", "auto", NULL
};
for (int i = 0; keywords[i] != NULL; i++) {
if (strcmp(str, keywords[i]) == 0) return 1;
}
return 0;
}
%}
%%
"+"|"-"|"*"|"/"|"="|"=="|"!="|"<"|">"|"<="|">=" { printf("Operator: %s\n", yytext); }
";"|","|"("|")"|"{|"|"}"|"["|"]" { printf("Separator: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* {
if (isKeyword(yytext))
printf("Keyword: %s\n", yytext);
else
printf("Identifier: %s\n", yytext);
}
[0-9]+(\.[0-9]+)? { printf("Number: %s\n", yytext); }
.|\n { /* Ignore invalid characters */ }
%%
int main() {
printf("Enter a C code fragment (Ctrl+D to stop):\n");
yylex();
return 0;
}
9
Q5. Design a LEX code to Count and print the number of total characters, words,white spaces in
given input.txt file.
Solution:-
%{
#include <stdio.h>
int char_count = 0, word_count = 0, space_count = 0;
%}
%%
\n|[ \t] { space_count++; char_count++; }
[a-zA-Z0-9]+ { word_count++; char_count += yyleng; }
. { char_count++; }
%%
int main() {
FILE *file = fopen("input.txt", "r");
if (!file) {
printf("Error: Cannot open input.txt\n");
return 1;
}
// Print counts
printf("Total Characters: %d\n", char_count);
printf("Total Words: %d\n", word_count);
printf("Total White Spaces: %d\n", space_count);
return 0;
}
10
Q6. Design a LEX code to Replace white spaces of input.txt file by a single blank character into
output.txt file.
Solution:-
%{
#include <stdio.h>
#include <stdbool.h>
FILE *outfile;
bool prev_space = false; // Tracks if the last character was a space
%}
%%
[ \t\n]+ {
if (!prev_space) {
fputc(' ', outfile);
prev_space = true;
}
} {
fputc(yytext[0], outfile);
prev_space = false;
}
%%
int main() {
FILE *infile = fopen("input.txt", "r");
if (!infile) {
printf("Error: Cannot open input.txt\n");
return 1;
}
outfile = fopen("output.txt", "w");
if (!outfile) {
printf("Error: Cannot create output.txt\n");
fclose(infile);
return 1;
}
yyin = infile;
yylex();
fclose(infile);
fclose(outfile);
printf("Processed input.txt and created output.txt with white spaces replaced by single blanks.\n");
return 0;
}
11
Q7. Design a LEX code to Remove the comments from any C-program given at runtime and store
into out.c file.
Solution:-
%{
#include <stdio.h>
FILE *outfile;
%}
%%
"/*"([^*]|\*+[^/])*"*/" { /* Multi-line comments: Ignore them */ }
"//".* { /* Single-line comments: Ignore them */ }
.|\n { fputc(yytext[0], outfile); } // Write non-comment characters
%%
int main() {
FILE *infile = fopen("input.c", "r");
if (!infile) {
printf("Error: Cannot open input.c\n");
return 1;
}
// Close files
fclose(infile);
fclose(outfile);
Q8. Design a LEX code to Extract all html tags in the given html file at runtime and store it into text
file given at runtime
Solution:-
%{
#include <stdio.h>
FILE *outfile;
%}
%%
<[^>]+> { fprintf(outfile, "%s\n", yytext); } // Match HTML tags and write to the output file
.|\n { /* Ignore non-tag content */ }
%%
int main() {
char input_filename[100], output_filename[100];
printf("Extracted all HTML tags from %s and stored them in %s.\n", input_filename, output_filename);
return 0;
}
13
Q9. Design a DFA in LEX code Which accepts the string containing even number of 'a' and even
number of 'b' over input alphabet {a,b}.
Solution:-
%{
#include <stdio.h>
enum states { Q0, Q1, Q2, Q3 }; // Define states: Q0 (even a, even b), Q1 (odd a, even b), Q2 (even a, odd
b), Q3 (odd a, odd b)
%%
a {
if (state == Q0) state = Q1; // Even 'a', even 'b' -> odd 'a', even 'b'
else if (state == Q1) state = Q0; // Odd 'a', even 'b' -> even 'a', even 'b'
else if (state == Q2) state = Q3; // Even 'a', odd 'b' -> odd 'a', odd 'b'
else if (state == Q3) state = Q2; // Odd 'a', odd 'b' -> even 'a', odd 'b'
}
b {
if (state == Q0) state = Q2; // Even 'a', even 'b' -> even 'a', odd 'b'
else if (state == Q1) state = Q3; // Odd 'a', even 'b' -> odd 'a', odd 'b'
else if (state == Q2) state = Q1; // Even 'a', odd 'b' -> odd 'a', even 'b'
else if (state == Q3) state = Q0; // Odd 'a', odd 'b' -> even 'a', even 'b'
}
%%
int main() {
printf("Enter input (Ctrl+D to stop):\n");
yylex();
if (state == Q0) {
printf("The string contains an even number of 'a' and an even number of 'b'.\n");
} else {
printf("The string does not contain an even number of 'a' and an even number of 'b'.\n");
}
return 0;
}
14
Q10. Design a DFA in LEX code Which accepts string containing third last element 'a' over input
alphabet{a,b}.
Solution:-
%{
#include <stdio.h>
enum states { Q0, Q1, Q2, Q3, Q4 }; // Define states: Q0, Q1, Q2, Q3, Q4
%%
a {
if (state == Q0) state = Q1; // No 'a' yet, transition to Q1 (1st character is 'a')
else if (state == Q1) state = Q2; // 1st 'a' encountered, transition to Q2
else if (state == Q2) state = Q3; // 2nd 'a' encountered, transition to Q3
else if (state == Q3) state = Q4; // 3rd 'a' encountered, accept
else if (state == Q4) state = Q4; // Stay in Q4 (accepted)
}
b {
if (state == Q0) state = Q1; // No 'a' yet, transition to Q1
else if (state == Q1) state = Q2; // 1st 'a' encountered, transition to Q2
else if (state == Q2) state = Q3; // 2nd 'a' encountered, transition to Q3
else if (state == Q3) state = Q4; // 3rd 'a' encountered, accept
else if (state == Q4) state = Q4; // Stay in Q4 (accepted)
}
%%
int main() {
printf("Enter input (Ctrl+D to stop):\n");
yylex();
if (state == Q4) {
printf("The string contains 'a' as the third last element.\n");
} else {
printf("The string does not contain 'a' as the third last element.\n");
}
return 0;
}
15
Q11. Design a DFA in LEX code To identify and print integers & float constants
and Identifiers.
Solution:-
%{
#include <stdio.h>
#include <ctype.h>
%%
%%
int main() {
printf("Enter input (Ctrl+D to stop):\n");
yylex(); // Process the input
return 0;
}
16
Q12 . Design a YACC/LEX code to recognise valid arithmetic expression with operator +, -, * and /.
Solution:-
%{
#include <stdio.h>
#include "y.tab.h" // Include the header file generated by yacc
%}
%%
%%
int yywrap(void) {
return 1
}
int main(void) {
printf("Enter an arithmetic expression: ");
yylex();
return 0;
}
17
Q13 . Design a YACC/LEX code to Evaluate arithmetic expression involving operators+, -, * and /
Without operator precedence grammar & with operator precedence grammar.
Solutions:-
%{
#include <stdio.h>
#include "y.tab.h" // Include the header file generated by yacc
%}
%%
%%
int yywrap(void) {
return 1; // End of input
}
int main(void) {
printf("Enter an arithmetic expression: ");
yylex(); // Start lexical analysis
return 0;
}
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
%}
%union {
int num;
}
18
%%
program:
expression '\n' { printf("Result: %d\n", $1); } // Print result of expression
;
expression:
term { $$ = $1; }
| expression PLUS term { $$ = $1 + $3; }
| expression MINUS term { $$ = $1 - $3; }
;
term:
factor { $$ = $1; }
| term MUL factor { $$ = $1 * $3; }
| term DIV factor { $$ = $1 / $3; }
;
factor:
NUM { $$ = $1; }
| LPAREN expression RPAREN { $$ = $2; }
;
%%
int main(void) {
printf("Enter an arithmetic expression: ");
yyparse();
return 0;
}