Krishnanand G
DFA 1
#include <stdio.h>
int main() {
char input[100];
printf("Enter the binary string");
scanf("\n%s", input);
for(int i = 0; input[i] != '\0'; i++) {
if (input[i] != '1' && input[i] != '0') {
printf("INVALID INPUT!");
return 0;
}
}
int state = 0;
char ip;
for(int i = 0; input[i] != '\0'; i++) {
ip = input[i];
switch(state) {
case 0 :
if(ip == '0') {
state = 1;
}
break;
case 1 :
if (ip == '0' ) {
state = 2;
}
else {
state = 0;
}
break;
case 2 :
if (ip == '0' ) {
state = 2;
}
else {
state = 0;
}
default:
state = 3;
break;
}
}
if (state == 2) {
printf("\nINPUT VALID");
}
else {
printf("\nINPUT INVALID");
}
return 0;
}
Krishnanand G
DFA 2
#include <stdio.h>
int main() {
char input[100];
printf("Enter the binary string:\n");
scanf("%s", input);
for(int i = 0; input[i] != '\0'; i++) {
if (input[i] != '1' && input[i] != '0') {
printf("NOT IN LANGUAGE");
return 0;
}
}
int state = 0;
char ip;
for(int i = 0; input[i] != '\0'; i++) {
ip = input[i];
switch(state) {
case 0 :
if(ip == '0') {
state = 1;
}
else {
state = 0;
}
break;
case 1 :
if (ip == '0' ) {
state = 2;
}
else {
state = 0;
}
break;
case 2 :
if (ip == '0' ) {
state = 4;
}
else {
state = 3;
}
break;
case 3 :
if (ip == '0' ) {
state = 1;
}
else {
state = 0;
}
break;
case 4 :
break;
default:
state = 4;
break;
}
}
if (state == 3 || state == 1 || state == 0) {
printf("\nVALID INPUT");
}
else {
printf("\nINVALID INPUT");
}
return 0;
}
Krishnanand G
one Star
#include<stdio.h>
#include<string.h>
int main() {
int i, state = 0;
char str[100];
printf("DFA: \n");
printf("Enter String: ");
scanf("%s", str);
for(i = 0; i < strlen(str); i++) {
if(str[i] != '1' && str[i] != '2' && str[i] != '3') {
printf("Rejected\n");
return 0;
}
switch(state) {
case 0:
if(str[i] == '1') {
state = 0;
} else if(str[i] == '2') {
state = 1;
} else if(str[i] == '3') {
state = 2;
}
break;
case 1:
if(str[i] == '2') {
state = 1;
} else if(str[i] == '3') {
state = 2;
}
break;
case 2:
if(str[i] == '3') {
state = 3;
} else {
printf("Rejected\n");
return 0;
}
break;
}
}
if(state == 2) {
printf("Accepted\n");
} else {
printf("Rejected\n");
}
return 0;
}
Krishnanand G
abc Substring
#include<stdio.h>
#include<string.h>
int main() {
char input[100];
int i = 0;
int curState = 0;
printf("Enter a string : ");
scanf("%s", input);
while(input[i] != '\0') {
char symbol = input[i];
if (curState == 0) {
if(symbol == 'a') {
curState = 1;
}
else if (symbol == 'b' || symbol == 'c') {
curState = 0;
}
}
else if (curState == 1) {
if(symbol == 'a') {
curState = 1;
}
else if (symbol == 'b') {
curState = 2;
}
else if (symbol == 'c') {
curState = 0;
}
}
else if (curState == 2) {
if(symbol == 'a') {
curState = 1;
}
else if (symbol == 'b') {
curState = 0;
}
else if (symbol == 'c') {
curState = 3;
}
}
else if(curState == 3) {
if(symbol == 'b' || symbol == 'c' || symbol == 'a') {
curState = 3;
}
}
i++;
}
printf("The String is %s. \n", curState == 3 ? "accepted":"rejected");
return 0;
}
Krishnanand G
Valid Id
#include<stdio.h>
#include<string.h>
int main() {
char input[100];
int i = 0;
int curstate = 0;
printf("Enter a binary string: ");
scanf("%[^\n]", input);
if(strcmp(input, "if")==0 || strcmp(input, "else")== 0 || strcmp(input,
"case")== 0 || strcmp(input, "return")== 0 || strcmp(input, "for")== 0) {
printf("No Keywords");
return 0;
}
while(input[i] != '\0') {
char symbol = input[i];
if(curstate == 0) {
if (symbol >= 97 && symbol <= 122 || symbol >= 65 && symbol <= 90 ||
symbol == 95) {
curstate = 1;
}
else {
curstate = -1;
break;
}
}
else if (curstate == 1) {
if (symbol >= 97 && symbol <= 122 || symbol >= 65 && symbol <= 90 ||
symbol == 95|| symbol >= 48 && symbol <= 57) {
curstate = 1;
}
else {
curstate = -1;
}
}
i++;
}
printf("The String is %s.\n", curstate == 1 ? "accepted" : "rejected");
return 0;
}
Krishnanand G
20222060
Replace Bracket
%{
#include<stdio.h>
#include<stdlib.h>
FILE *dest;
%}
%%
\n {fprintf(dest,"\n");}
"{" {
fprintf(dest, "begin");
}
"}" {
fprintf(dest,"end");
}
. {
fprintf(dest,"%s",yytext);
}
%%
int yywrap(){
return 1;
}
int main(int argc, char* argv[]) {
if(argc!=3){
fprintf(stderr,"Usage:%s <input_file><output_file>\n", argv[0]);
return EXIT_FAILURE;
}
FILE*fd=fopen(argv[1],"r");
if(!fd) {
perror("Failed to open input file");
return EXIT_FAILURE;
}
dest=fopen(argv[2],"w");
if(!dest) {
perror("Failed to open output file");
fclose(fd);
return EXIT_FAILURE;
}
printf("Successfully executed!");
yyin=fd;
yylex();
fclose(fd);
fclose(dest);
return 0;
}
Krishnanand G
20222060
Copy LEX
%{
#include<stdio.h>
#include<string.h>
char line[1024];
FILE *out;
int yywrap(void); // Declare the function prototype
%}
%%
\n {fprintf(out,"%s\n",line);}
.* {strcpy(line,yytext);}
%%
int main()
{
yyin = fopen("[Link]", "r");
out = fopen("[Link]", "w");
if (!yyin || !out) {
printf("Error opening files.\n");
return 1;
}
printf("Copied successfully!\n");
yylex();
fclose(out);
return 0;
}
int yywrap(void) {
return 1;
}
Krishnanand G
20222060
Identifiers
%{
#include <stdio.h>
#include <string.h>
int k = 0, i = 0, o = 0, n = 0;
int yywrap(void) {
return 1;
}
%}
%%
[0-9]+ {n++;}
"for" {k++;}
"while" {k++;}
"float" {k++;}
"int" {k++;}
"+" {o++;}
"-" {o++;}
"*" {o++;}
"/" {o++;}
"=" {o++;}
[a-zA-Z_][a-zA-Z0-9_]* {i++;}
[0-9]+[a-zA-Z_]+ ;
[\t \n] ;
[.] ;
%%
int main(){
FILE *yyin = fopen("[Link]", "r");
if (!yyin) {
fprintf(stderr, "Error: could not open file '[Link]'\n");
return 1;
}
yylex();
printf("Keywords: %d\n", k);
printf("Identifiers: %d\n", i);
printf("Numbers: %d\n", n);
printf("Operators: %d\n", o);
fclose(yyin);
return 0;
Krishnanand G
20222060
count lex
%{
#include<stdio.h>
int char_count=0;
int word_count=0;
int line_count=0;
int in_word = 0;
%}
%%
\n {line_count++; if (in_word) word_count++; in_word = 0;}
[\t]+ {char_count+=yyleng;}
[ ]+ {char_count+=yyleng; if (!in_word) {word_count++; in_word = 1;} }
[a-zA-Z0-9]+ {char_count+=yyleng; if (!in_word) {word_count++; in_word = 1;} }
. {char_count+=yyleng;}
%%
int main(){
yylex();
printf("lines: %d\n", line_count);
printf("words: %d\n", word_count);
printf("characters: %d\n", char_count);
return 0;
}
int yywrap() {
return 1;
}
///KRISHNANAND G
///LOW LEVEL EXPRESSION
LEX:
%{
#include "[Link].h"
%}
%%
[a-zA-Z]+ { return VARIABLE; }
[0-9]+ { return NUM; }
[ \t] { /* Ignore whitespace */ }
"=" { return '='; }
"(" { return '('; }
")" { return ')'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
\n { return 0; }
. { printf("Invalid\n"); exit(0); }
%%
int yywrap() {
return 1;
}
YACC:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void); // Lex function
void yyerror(char *msg); // Error function
%}
%token NUM VARIABLE
%left '+' '-'
%left '*' '/'
%%
S: VARIABLE '=' E {
return 0;
}
| E {
return 0;
}
E: E '+' E
| E '-' E
| E '*' E
| E '/' E
| '(' E ')'
| NUM
| VARIABLE
;
%%
void yyerror(char *msg)
{
printf("Entered arithmetic expression is invalid\n");
}
int main()
{
printf("Enter the expression: \n");
if (yyparse() == 1) {
printf("Entered arithmetic expression is invalid\n");
} else {
printf("Entered arithmetic expression is valid\n");
}
}
///KRISHNANAND G
///IF ELSE STATEMENT
LEX:
%{
#include "[Link].h"
%}
%%
\n ;
\t ;
[ ]+ ;
if {return IF;}
else {return ELSE;}
== {return EQ;}
!= {return NE;}
">" {return REL;}
"<" {return REL;}
[a-z]+ {return ID;}
[0-9]+ {return NUM;}
. {return yytext[0];}
%%
int yywrap() {
return 1;
}
YACC:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *msg);
%}
%token IF ELSE EQ NE ID NUM REL
%%
S: stmt {printf("Valid statement\n"); exit(0); }
;
stmt: IF '(' cond ')' '{' T '}' |
IF '(' cond ')' '{' T '}' ELSE '{' T '}'
;
cond: ID REL ID |
ID REL NUM |
ID EQ NUM |
ID EQ ID |
ID NE NUM |
ID NE ID |
ID |
NUM
;
T: code ';' T | stmt |
/* empty */
;
code: ID '=' ID |
ID '=' NUM |
ID '+' NUM |
ID '-' NUM |
ID '+' ID |
ID '-' ID |
ID '=' NUM '+' NUM
;
%%
void yyerror(const char *msg){
printf("Invalid\n");
return 0;
}
int main(){
extern FILE *yyin;
yyin = fopen("[Link]","r");
if (yyin == NULL) {
perror("Error opening input file");
return 1;
}
yyparse();
return 0;
}
///KRISHNANAND G
///FOR LOOP
LEX:
%{
#include "[Link].h"
int yylex(void);
%}
%%
\n ;
\t ;
[ ]+ ;
for {return FOR;}
int {return INT;}
\+\+ {return INC;}
-- {return DEC;}
"+=" {return PLUSEQ;}
"-=" {return MINUSEQ;}
"==" {return EQ;}
"!=" {return NE;}
"<=" {return LE;}
">=" {return GE;}
"<" {return LT;}
">" {return GT;}
[a-zA-Z_][a-zA-Z0-9_]* {return ID;}
[0-9]+ {return NUM;}
. {return yytext[0];}
%%
int yywrap(){
return 1;
}
YACC:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror(const char *msg);
%}
%token FOR INT ID NUM INC DEC PLUSEQ MINUSEQ EQ NE LT GT LE GE
%%
S: for_stmt {printf("Valid for loop\n"); exit(0);}
;
for_stmt: FOR '(' init ';' condition ';' update ')' '{' body '}'
;
init: INT ID '=' NUM |
INT ID '=' ID |
ID '=' NUM |
ID '=' ID |
/* empty */
;
condition: ID EQ NUM |
ID EQ ID |
ID NE NUM |
ID NE ID |
ID LT NUM |
ID LT ID |
ID GT NUM |
ID GT ID |
ID LE NUM |
ID LE ID |
ID GE NUM |
ID GE ID |
/* empty */
;
update: ID INC |
INC ID |
ID DEC |
DEC ID |
ID PLUSEQ NUM |
ID MINUSEQ NUM |
ID '=' ID '+' NUM |
ID '=' ID '-' NUM |
ID '=' NUM |
ID '=' ID |
/* empty */
;
body: statements |
/* empty */
;
statements: statements statement ';' |
/* empty */
;
statement: ID '=' NUM |
ID '=' ID |
ID '=' ID '+' NUM |
ID '=' ID '-' NUM |
ID '=' ID '+' ID |
ID '=' ID '-' ID |
ID '=' NUM '+' NUM |
ID '=' NUM '-' NUM |
ID INC |
INC ID |
ID DEC |
DEC ID
;
%%
int yyerror(const char *msg){
printf("Invalid for loop\n");
return 0;
}
int main(){
printf("Enter a for loop: ");
yyparse(); // Reads from stdin (terminal)
return 0;
}