CD Contenthalf
CD Contenthalf
PRACTICAL-1
Finite automata (FA) are mathematical models used to recognize patterns in strings.
They consist of a finite set of states, a starting state, a set of accepting states, and a
transition function that defines how the automaton moves from one state to another
based on the input symbols.
String validation using finite automata involves determining whether a given string is
accepted or rejected by the automaton. This is done by starting at the initial state and
processing the input symbols one by one, following the transitions defined by the
transition function. If the automaton reaches an accepting state after processing the
entire string, the string is accepted; otherwise, it is rejected.
class FiniteAutomaton:
def __init__(self, states, alphabet, transitions, start_state, accept_states):
self.states = states
self.alphabet = alphabet
self.transitions = transitions
self.start_state = start_state
self.accept_states = accept_states
# Example usage:
states = {'q0', 'q1', 'q2'}
alphabet = {'a', 'b'}
transitions = {
'q0': {'a': 'q1', 'b': 'q0'},
'q1': {'a': 'q2', 'b': 'q1'},
'q2': {'a': 'q2', 'b': 'q2'}
start_state = 'q0'
accept_states = {'q2'}
string1 = "aabbb"
string2 = "ababa"
if fa.validate(string1):
print("String 1 is accepted.")
else:
print("String 1 is rejected.")
if fa.validate(string2):
print("String 2 is accepted.")
else:
print("String 2 is rejected.")
OUTPUT:
PRACTICAL-2
Lex, a lexical analyzer generator, is a powerful tool used in computer science for creating
lexical analyzers, also known as scanners. These analyzers are essential components of
compilers, text editors, and other language processing tools.
Example:
PRACTICAL-3
%{
#include<stdio.h>
#include<string.h>
char word[]="students"; int count=0;
%}
%%
[a-zA-Z]+ {if(strcmp(yytext,word)==0) count++;}
%%
int yywrap(){} int main()
{
yyin=fopen("input.txt","r"); yylex();
printf("%d of %s",count,word);
}
INPUT:
Hi students.
How are you students?
OUTPUT:
%option noyywrap
%{
#include<stdio.h>
int word;
int line;
int ch;
%}
%%
[a-zA-Z0-9] {++ch;}
[.]*[ |\t] {++ch, ++word;}
[.]*[\n] {++ch, ++word, ++line;}
.;
%%
int main(void)
{
yyin=fopen("input.txt","r");
yylex();
printf("Characters : %d\n",ch);
printf("Words : %d\n",word);
printf("Lines : %d\n",line);
return 0;
}
OUTPUT:
c) Ceasor Cypher.
%{
#include<stdio.h>
#include<string.h>
%}
%%
[a-z] {char ch = yytext[0]; ch += 3;
if (ch> 'z') ch -= ('z'+1- 'a'); printf ("%c" ,ch );
}
[A-Z] { char ch = yytext[0] ; ch += 3;
if (ch> 'Z') ch -= ('Z'+1- 'A');
printf("%c",ch);
}
%%
int yywrap(){} int main()
{
yylex();
}
OUTPUT:
%{
#include<stdio.h>
%}
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
int yywrap(){
return 1;
}
int main(){
yyin = fopen("input.c","r");
yyout = fopen("output.c","w");
yylex(); return 0;
}
OUTPUT:
PRACTICAL-4
%option noyywrap
%{
# include "roman.tab.h"
%}
%%
"I" {yylval = 1;return I;}
"II" {yylval = 2;return II;}
"III" {yylval = 3;return III;}
"IV" {yylval = 4;return IV;}
"V" {yylval = 5;return V;}
"VI" {yylval = 6;return VI;}
"VII" {yylval = 7;return VII;}
"VIII" {yylval = 8;return VIII;}
"IX" {yylval = 9;return IX;}
"X" {yylval = 10;return X;}
"XX" {yylval = 20;return XX;}
"XXX" {yylval = 30;return XXX;}
"XL" {yylval = 40;return XL;}
"L" {yylval = 50;return L;}
"LX" {yylval = 60;return LX;}
"LXX" {yylval = 70;return LXX;}
"LXXX" {yylval = 80;return LXXX;}
OUTPUT:
%option noyywrap
%{
int flag=0;
%}
%%
(""[aA][nN][dD]"")|(""[oO][rR]"")|(""[bB][uU][tT]"") {flag=1;}
%%
int main()
{
printf("Enter the sentence\n");
yylex();
if(flag==1)
printf("\nCompound sentence\n");
else
printf("\nSimple sentence\n");
return 0;
}
OUTPUT:
%{
%}
%%
"<"[^>]*> {printf("%s\n", yytext); } . ;
%%
int yywrap(){}
int main(int argc, char*argv[])
{
yyin=fopen("tags.txt","r");
yylex();
return 0;
}
Tags.txt
<html>
<head>
</head>
<body>
<h1>My first heading</h1>
<p>My first paragraph</p>
</body>
</html>
OUTPUT:
d) Write a Lex program which adds line numbers to the given file and
display the same onto the standard output.
%{
int line_number = 1;
%}
line .*\n
%%
{line} { printf("%10d %s", line_number++, yytext); }
%%
int yywrap(){}
int main(int argc, char*argv[])
{
Input.txt
hii
OUTPUT:
PRACTICAL-5
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
#include"ctype.h"
char ip_sym[15], ip_ptr = 0, op[50], tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n = 0;
void e() {
strcpy(op, "TE'");
printf("E=%-25s", op);
printf("E->TE'\n");
t();
e_prime();
}
void e_prime() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++)
if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);
for (n = 0; n < l && op[n] != 'E'; n++);
if (ip_sym[ip_ptr] == '+') {
i = n + 2;
do {
op[i + 2] = op[i];
i++;
} while (i <= l);
op[n++] = '+';
op[n++] = 'T';
op[n++] = 'E';
op[n++] = 39;
printf("E=%-25s", op);
printf("E'->+TE'\n");
advance();
t();
e_prime();
} else {
op[n] = 'e';
for (i = n + 1; i <= strlen(op); i++) op[i] = op[i + 1];
printf("E=%-25s", op);
printf("E'->e");
}
}
void t() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++) if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);
for (n = 0; n < l && op[n] != 'T'; n++); i = n + 1;
do {
op[i + 2] = op[i];
i++;
} while (i < l);
op[n++] = 'F';
op[n++] = 'T';
op[n++] = 39;
printf("E=%-25s", op);
printf("T->FT'\n");
f();
t_prime();
}
void t_prime() {
int i, n = 0, l;
for (i = 0; i <= strlen(op); i++) if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
l = strlen(op);
l = strlen(op);
for (n = 0; n < l && op[n] != 'F'; n++);
if ((ip_sym[ip_ptr] == 'i') || (ip_sym[ip_ptr] == 'I')) { op[n] = 'i';
printf("E=%-25s", op);
printf("F->i\n");
advance();
} else {
if (ip_sym[ip_ptr] == '(') {
advance();
e();
if (ip_sym[ip_ptr] == ')') {
advance();
i = n + 2;
do {
op[i + 2] = op[i];
i++;
} while (i <= l);
op[n++] = '(';
op[n++] = 'E';
op[n++] = ')';
printf("E=%-25s", op);
printf("F->(E)\n");
}
} else {
printf("\n\t syntax error");
getch();
exit(1);
}
}
}
void advance() {
ip_ptr++;
}
void main() {
int i;
clrscr();
printf("\nGrammar without left recursion");
printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' "); printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i");
printf("\n Enter the input expression:");
gets(ip_sym);
printf("Expressions");
printf("\t Sequence of production rules\n");
e();
for (i = 0; i < strlen(ip_sym); i++) {
if (ip_sym[i] != '+' && ip_sym[i] != '*' && ip_sym[i] != '(' && ip_sym[i] != ')' &&
ip_sym[i] != 'i' && ip_sym[i] != 'I') { printf("\nSyntax error");
break;
}
for (i = 0; i <= strlen(op); i++)
if (op[i] != 'e')
tmp[n++] = op[i];
strcpy(op, tmp);
printf("\nE=%-25s", op);
}
getch();
}
OUTPUT: