Q6. Write a C program to find first of any grammar.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
typedef struct production_rules {
char key;
char **values;
int value_count;
} production_rules;
int n_rules;
production_rules *pd;
char **split_values(const char *input, int *count) {
char **values = NULL;
char *token;
char *input_copy = strdup(input);
int value_count = 0;
token = strtok(input_copy, "|");
while (token != NULL) {
values = (char **)realloc(values, (value_count + 1) * sizeof(char *));
if (values == NULL) {
printf("Memory allocation failed.\n");
free(input_copy);
exit(1);
values[value_count++] = strdup(token);
token = strtok(NULL, "|");
free(input_copy);
*count = value_count;
return values;
production_rules *find_production(char key) {
for (int i = 0; i < n_rules; i++) {
if (pd[i].key == key) {
return &pd[i];
return NULL;
}
void compute_first(char key, char *first_set, int *first_size, char *visited) {
if (strchr(visited, key)) {
return;
strncat(visited, &key, 1);
production_rules *rule = find_production(key);
if (rule == NULL) return;
for (int i = 0; i < rule->value_count; i++) {
char *production = rule->values[i];
int j = 0;
while (production[j] != '\0') {
char symbol = production[j];
if (islower(symbol) || !isalpha(symbol)) {
if (strchr(first_set, symbol) == NULL) {
first_set[(*first_size)++] = symbol;
break;
} else {
char temp_first[MAX] = {0};
int temp_size = 0;
compute_first(symbol, temp_first, &temp_size, visited);
for (int k = 0; k < temp_size; k++) {
if (strcmp(&temp_first[k], "\u03B5") != 0 && strchr(first_set,
temp_first[k]) == NULL) {
first_set[(*first_size)++] = temp_first[k];
if (strchr(temp_first, '\u03B5') == NULL) {
break;
j++;
if (production[j] == '\0' && strchr(first_set, '\u03B5') == NULL) {
first_set[(*first_size)++] = '\u03B5';
visited[strlen(visited) - 1] = '\0';
int main() {
printf("Start\n");
printf("ENTER NUMBER OF PRODUCTION RULES: ");
scanf("%d", &n_rules);
getchar();
pd = (production_rules *)malloc(n_rules * sizeof(production_rules));
if (pd == NULL) {
printf("Memory allocation failed.\n");
return 1;
printf("ENTER INPUT PRODUCTIONS (e.g., S->Aa):\n");
for (int i = 0; i < n_rules; i++) {
char input[MAX];
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = '\0';
pd[i].key = input[0];
pd[i].values = split_values(input + 3, &pd[i].value_count);
printf("\nFIRST Sets:\n");
for (int i = 0; i < n_rules; i++) {
char first_set[MAX] = {0};
int first_size = 0;
char visited[MAX] = {0};
compute_first(pd[i].key, first_set, &first_size, visited);
printf("FIRST(%c) = { ", pd[i].key);
for (int j = 0; j < first_size; j++) {
printf("%c ", first_set[j]);
printf("}\n");
for (int i = 0; i < n_rules; i++) {
for (int j = 0; j < pd[i].value_count; j++) {
free(pd[i].values[j]);
free(pd[i].values);
free(pd);
return 0;
Q7.Implementation of LEXICAL ANALYZER for IF statement.
%{
#include <stdio.h>
#include <stdlib.h>
%}
KEYWORD if|else
IDENTIFIER [a-zA-Z_][a-zA-Z0-9_]*
NUMBER [0-9]+
OPERATOR ==|<=|>=|<|>|!=
DELIMITER [(){};]
%%
{KEYWORD} { printf("KEYWORD: %s\n", yytext); }
{IDENTIFIER} { printf("IDENTIFIER: %s\n", yytext); }
{NUMBER} { printf("NUMBER: %s\n", yytext); }
{OPERATOR} { printf("OPERATOR: %s\n", yytext); }
{DELIMITER} { printf("DELIMITER: %s\n", yytext); }
[ \t\n]+ ;
. { printf("UNKNOWN: %s\n", yytext); }
%%
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s/home/maushamkumarray/Desktop/compiler/input.txt\,
argv[0]);
exit(1);
FILE *file = fopen(argv[1], "r");
if (!file) {
perror("Could not open file");
exit(1);
yyin = file;
yylex();
fclose(file);
return 0;
int yywrap() {
return 1;
Q8. 8. To implement parsing algorithms.
LEX.l
%{
#include "y.tab.h"
%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[ \t\n]+ { /* Ignore whitespace */ }
"+" { return '+'; }
"-" { return '-'; }
. { printf("Unknown character: %s\n", yytext); }
%%
int yywrap() {
return 1;
parser.y
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *s);
int result = 0;
%token NUMBER
%left '+' '-'
%%
S:E { printf("Result: %d\n", $1); result = $1; }
E : E '+' E { $$ = $1 + $3; printf("Reducing: %d + %d = %d\n", $1, $3, $$); }
| E '-' E { $$ = $1 - $3; printf("Reducing: %d - %d = %d\n", $1, $3, $$); }
| NUMBER { $$ = $1; printf("Reducing: %d -> NUMBER\n", $1); }
%%
int main() {
printf("Enter an arithmetic expression \n");
yyparse();
return 0;
int yyerror(char *s) {
fprintf(stderr, "Error: %s\n", s);
return 1;
Q 9. Write a lex program to count blank spaces, words, lines in a given file.
%{
#include <stdio.h>
int blank_spaces = 0;
int words = 0;
int lines = 0;
%}
%%
[ \t] { blank_spaces++; }
\n { lines++; }
[^\t\n ]+ { words++; }
%%
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <file>\n", argv[0]);
return 1;
FILE *file = fopen(argv[1], "r");
if (!file) {
perror("Error opening file");
return 1;
yyin = file;
yylex();
fclose(file);
printf("Blank Spaces: %d\n", blank_spaces);
printf("Words: %d\n", words);
printf("Lines: %d\n", lines);
return 0;
10. Write a lex program to count total no of operators in a file (taking file from user).
%{
#include <stdio.h>
int operator_count = 0;
%}
%%
"<<"|">>" { operator_count++; }
"=="|"!="|"<="|">=" { operator_count++; }
"&&"|"||"|"!" { operator_count++; }
"+"|"-"|"*"|"/"|"%" { operator_count++; }
"<"|">"|"=" { operator_count++; }
"&"|"|"|"^"|"~" { operator_count++; }
. {}
\n {}
%%
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
FILE *file = fopen(argv[1], "r");
if (!file) {
printf("Error: Could not open file %s\n", argv[1]);
return 1;
}
yyin = file;
yylex();
fclose(file);
printf("Total number of operators: %d\n", operator_count);
return 0;
Q 11. Write a lex program to count any five keywords in a file (taking file from user)
%{
#include <stdio.h>
int if_count = 0, else_count = 0, for_count = 0, while_count = 0, return_count = 0;
%}
%%
"if" { if_count++; }
"else" { else_count++; }
"for" { for_count++; }
"while" { while_count++; }
"return" { return_count++; }
. {}
\n {}
%%
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
FILE *file = fopen(argv[1], "r");
if (!file) {
printf("Error: Could not open file %s\n", argv[1]);
return 1;
yyin = file;
yylex();
fclose(file);
printf("Keyword Counts:\n");
printf("if: %d\n", if_count);
printf("else: %d\n", else_count);
printf("for: %d\n", for_count);
printf("while: %d\n", while_count);
printf("return: %d\n", return_count);
return 0;