FIRST AND FOLLOW
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int n, m = 0, p, i = 0, j = 0;
char a[10][10], f[10];
void follow(char c);
void first(char c);
int main()
{
int i, z;
char c, ch;
// clrscr();
printf("Enter the no of productions:\n");
scanf("%d", &n);
printf("Enter the productions:\n");
for (i = 0; i < n; i++)
scanf("%s%c", a[i], &ch);
do
{
m = 0;
printf("Enter the elemets whose fisrt & follow is to be found:");
scanf("%c", &c);
first(c);
printf("First(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
strcpy(f, " ");
// flushall();
m = 0;
follow(c);
printf("Follow(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c", &z, &ch);
} while (z == 1);
return (0);
}
void first(char c)
{
int k;
if (!isupper(c))
f[m++] = c;
for (k = 0; k < n; k++)
{
if (a[k][0] == c)
{
if (a[k][2] == '$')
follow(a[k][0]);
else if (islower(a[k][2]))
f[m++] = a[k][2];
else
first(a[k][2]);
}
}
}
void follow(char c)
{
if (a[0][0] == c)
f[m++] = '$';
for (i = 0; i < n; i++)
{
for (j = 2; j < strlen(a[i]); j++)
{
if (a[i][j] == c)
{
if (a[i][j + 1] != '\0')
first(a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(a[i][0]);
}
}
}
}
Input/ Output :-
-------------
Enter the no. of productions : 8
Enter the production string like "E=E+T"
and epsilon as #
Enter production Number 1 : E=TX
Enter production Number 2 : X=+TX
Enter production Number 3 : X=#
Enter production Number 4 : T=FY
Enter production Number 5 : Y=*FY
Enter production Number 6 : Y=#
Enter production Number 7 : F=(E)
Enter production Number 8 : F=i
Find FOLLOW of --> E
VOWEL AND CONSONANT COUNT
%{
#include<stdio.h>
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ] {const_count++;}
%%
int main()
{
yylex();
printf("Vowels = %d and Consonants = %d",vow_count,const_count);
return 0;
}
int yywrap()
{
return 1;
}
//to compile the code
// lex pgm.l
// gcc lex.yy.c
// ./a.out
// Output
// hello
// Vowels = 2 and Consonants = 3
lEX COUNT
%{
#include<stdio.h>
int sc=0,wc=0,lc=0,cc=0;
%}
%%
[\n] { lc++; cc+=yyleng;}
[ \t] { sc++; cc+=yyleng;}
[^\t\n ]+ { wc++; cc+=yyleng;}
%%
int main(int argc ,char* argv[ ])
{
printf("Enter the input:\n");
yylex();
printf("The number of lines=%d\n",lc);
printf("The number of spaces=%d\n",sc);
printf("The number of words=%d\n",wc);
printf("The number of characters are=%d\n",cc);
}
int yywrap( )
{
return 1;
}
// to compile type lex demo.l
// then type cc lex.yy.c
// then type ./a.out
/*
Enter the input: Greetings from CEC
Welcome to Chengannur
Lex Yacc Program
(Note: After input string press enter and ctrl+d)
The number of lines=3
The number of spaces=6
The number of words=9
The number of characters are=58
*/
%%
((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");
}
/* Lex program to check if a date is valid or not */
%{
/* 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()
{
return 1;
}
Valid Time
([01][0-9]2[0-3]):([0-5][0-9]:[0-5][0-9])
OBJECTIVE: To identify whether given string is keyword or not.
PROGRAM:-
#include <stdio.h>
#include <string.h>
int main() {
char a[5][10] = {"printf", "scanf", "if", "else", "break"};
char str[10];
int i, flag = 0;
printf("Enter the string :: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove the newline character from the input
for (i = 0; i < 5; i++) {
if (strcmp(str, a[i]) == 0) {
flag = 1;
break; // No need to continue checking once a match is found
}
}
if (flag == 1)
printf("Keyword\n");
else
printf("String\n");
return 0;
}
OBJECTIVE: Count total no. of keywords in a file. [Taking file from user]
PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static int count = 0;
int isKeyword(char buffer[]) {
char keywords[32][10] = {
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if",
"int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"
};
for (int i = 0; i < 32; ++i) {
if (strcmp(keywords[i], buffer) == 0) {
count++;
return 1;
}
}
return 0;
}
int main() {
char ch, buffer[15];
FILE *fp;
int j = 0;
fp = fopen("KESHAV3.C", "r");
if (fp == NULL) {
printf("Error while opening the file\n");
exit(1); // Use a non-zero exit status to indicate an error.
}
while ((ch = fgetc(fp)) != EOF) {
if (isalnum(ch) || ch == '_') {
buffer[j++] = ch;
} else if ((ch == ' ' || ch == '\n' || ch == '\t' || ch == '(' || ch == ')' || ch == ',' || ch == ';' || ch == '{' || ch == '}') && j != 0) {
buffer[j] = '\0';
j = 0;
isKeyword(buffer);
}
}
printf("Number of keywords: %d\n", count);
fclose(fp);
return 0;
}
OBJECTIVE: Count total no of operators in a file. [Taking file from user]
PROGRAM:-
#include<stdlib.h>
#include<string.h>
#include<ctype.h> static int count=0; int main(){
char ch, buffer[15], operators[] = "+-*/%="; FILE *fp;
int i; clrscr();
fp = fopen("KESHAV3.C","r"); if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){ for(i = 0; i < 6; ++i){
if(ch == operators[i]) {
printf("%c is operator\n", ch); count++;
}
}
}
printf("no of operators= %d", count); fclose(fp);
return 0;
}
OBJECTIVE: Count total occurrence of each character in a given file. [Taking file from user]
PROGRAM:-
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
char string[100];
int count[26] = {0};
int c, x;
fp = fopen("deepa.txt", "r");
if (fp == NULL) {
printf("Error while opening the file\n");
return 1;
}
while (fscanf(fp, "%s", string) != EOF) {
for (c = 0; string[c] != '\0'; c++) {
char ch = string[c];
// Check if the character is a lowercase letter
if (ch >= 'a' && ch <= 'z') {
x = ch - 'a';
count[x]++;
}
}
}
fclose(fp);
for (c = 0; c < 26; c++) {
printf("%c occurs %d times in the string.\n", c + 'a', count[c]);
}
return 0;
}
OBJECTIVE: Write a C program to insert, delete and display the entries in Symbol Table.
PROGRAM:-
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i = 0, x = 0, n;
char c;
char b[15];
char d[15];
char* add[15]; // Changed the type of add to an array of character pointers
printf("Expression terminated by $:");
while ((c = getchar()) != '$') {
b[i] = c;
i++;
}
n = i - 1;
printf("Given Expression:");
for (i = 0; i <= n; i++) {
printf("%c", b[i]);
}
printf("\n Symbol Table\n");
printf("Symbol \t addr \t type");
for (i = 0; i <= n; i++) {
c = b[i];
if (isalpha(c)) {
add[x] = malloc(2); // Allocate memory for the symbol (character + null-terminator)
d[x] = c;
add[x][0] = c;
add[x][1] = '\0'; // Null-terminate the string
printf("\n%c \t %p \t identifier\n", c, (void *)add[x]);
x++;
} else if (c == '+' || c == '-' || c == '*' || c == '=') {
add[x] = malloc(2); // Allocate memory for the operator (character + null-terminator)
d[x] = c;
add[x][0] = c;
add[x][1] = '\0'; // Null-terminate the string
printf("\n%c \t %p \t operator\n", c, (void *)add[x]);
x++;
}
}
// Free the allocated memory
for (i = 0; i < x; i++) {
free(add[i]);
}
return 0;
}