0% found this document useful (0 votes)
11 views26 pages

CD 1 Deep-8-18 Merged

The document outlines multiple experiments involving programming concepts, including recognizing arithmetic expressions, validating variable names, implementing a calculator using LEX and YACC, generating abstract syntax trees, and computing ε-closures for NFA with ε transitions. Each experiment includes code snippets and examples of inputs and outputs. The final experiment focuses on converting NFA with epsilon moves to DFA, detailing the necessary code and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

CD 1 Deep-8-18 Merged

The document outlines multiple experiments involving programming concepts, including recognizing arithmetic expressions, validating variable names, implementing a calculator using LEX and YACC, generating abstract syntax trees, and computing ε-closures for NFA with ε transitions. Each experiment includes code snippets and examples of inputs and outputs. The final experiment focuses on converting NFA with epsilon moves to DFA, detailing the necessary code and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

EXPERIMENT 3 (A)

AIM : Program to recognize a valid arithmetic expression that uses


operator +, – , * and /.

//Lex Code(arith.1)
%{
#include "y.tab.h"
%}

DIGIT [0-9]+

%%

{DIGIT} { yylval = atoi(yytext); return


NUMBER; } "+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"(" { return LPAREN; }
")" { return
RPAREN; } [ \t\n]+ ; //
Ignore spaces

%%

//YACC code(arith.y)
%{
#include <stdio.h>
%}

%token NUMBER PLUS MINUS MULT DIV LPAREN RPAREN

%%

expr: expr PLUS term | expr MINUS term |


term; term: term MULT factor | term DIV
factor | factor; factor: NUMBER | LPAREN
expr RPAREN;

%%

int main() {
AVINASH KUMAR 2200910100
THAKUR 040
printf("Enter an
expression: "); yyparse();
return 0;
}

int yyerror(char *s) {


printf("Invalid expression\
n"); return 0;
}

Input:
(5 + 3) * 2

Output:
Valid Expression

AVINASH KUMAR 2200910100


THAKUR 040
EXPERIMENT 3 (B)

AIM : Program to recognize a valid variable which starts with a


letter followed by any number of letters or digits.

//Lex code(var.1)
%{
#include "y.tab.h"
%}

LETTER [a-zA-
Z] DIGIT [0-
9]
IDENTIFIER {LETTER}({LETTER}|{DIGIT})*

%%

{IDENTIFIER} { return IDENTIFIER; }


[ \t\n]+ ; // Ignore spaces

%%

//YACC code(var.y)

%{
#include <stdio.h>
%}

%token IDENTIFIER

%%

stmt: IDENTIFIER { printf("Valid Variable\n"); };

%%

int main()
{ yypars
e();
return
0;
}
AVINASH KUMAR 2200910100
THAKUR 040
int yyerror(char *s) {
printf("Invalid variable\
n");

AVINASH KUMAR 2200910100


THAKUR 040
return 0;
}

Input:
myVar123

Output:
Valid variable

AVINASH KUMAR 2200910100


THAKUR 040
EXPERIMENT3(C)

AIM: Implementation of Calculator using LEX and YACC

//Lex code(calc.1)

%{
#include "y.tab.h"
%}

DIGIT [0-9]+

%%

{DIGIT} { yylval = atoi(yytext); return


NUMBER; } "+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"(" { return LPAREN; }
")" { return
RPAREN; } [ \t\n]+ ; //
Ignore spaces

%%

//YACC code(calc.y)

%{
#include <stdio.h>
%}

%token NUMBER PLUS MINUS MULT DIV LPAREN RPAREN


%left PLUS MINUS
%left MULT DIV

%%

expr: expr PLUS term { $$ = $1 + $3; }


| expr MINUS term { $$ = $1 - $3; }
| term;

term: term MULT factor { $$ = $1 * $3; }


AVINASH KUMAR 2200910100
THAKUR 040
| term DIV factor { $$ = $1 / $3; }

AVINASH KUMAR 2200910100


THAKUR 040
| factor;

factor: NUMBER | LPAREN expr RPAREN { $$ = $2; };

%%

int main() {
printf("Enter an arithmetic
expression: "); yyparse();
return 0;
}

int yyerror(char *s) {


printf("Syntax Error\
n"); return 0;
}

Input:
3+5*2

Output:
Valid Expression

AVINASH KUMAR 2200910100


THAKUR 040
EXPERIMENT 3(D)

AIM : Convert the BNF rules into YACC form and write code to
generate abstract syntax tree

// YACC code(ast.y)

%{
#include <stdio.h>
#include <stdlib.h>

typedef struct
Node { char
*value;
struct Node *left;
struct Node *right;
} Node;

Node* createNode(char *value, Node *left, Node


*right) { Node *node = (Node*)
malloc(sizeof(Node));
node->value =
value; node->left
= left; node-
>right = right;
return node;
}

void printAST(Node *node, int


level) { if (node == NULL)
return;
for (int i = 0; i < level; i++)
printf(" "); printf("%s\n", node-
>value); printAST(node->left,
level + 1); printAST(node-
>right, level + 1);
}

%}

%token NUMBER PLUS MINUS MULT DIV LPAREN RPAREN

AVINASH KUMAR 2200910100


THAKUR 040
%%

expr: expr PLUS term { $$ = createNode("+", $1, $3); }


| expr MINUS term { $$ = createNode("-", $1, $3); }
| term { $$ = $1; };

AVINASH KUMAR 2200910100


THAKUR 040
term: term MULT factor { $$ = createNode("*", $1, $3); }
| term DIV factor { $$ = createNode("/", $1, $3); }
| factor { $$ = $1; };

factor: NUMBER { $$ = createNode(yytext, NULL, NULL); }


| LPAREN expr RPAREN { $$ = $2; };

%%

int main()
{ yypars
e();
return
0;
}

int yyerror(char *s) {


printf("Syntax Error\
n"); return 0;
}

AVINASH KUMAR 2200910100


THAKUR 040
EXPERIMENT 4

AIM : Write program to find ε – closure of all states of any given


NFA with ε transition.

#include <stdio.h>
#include <stdlib.h>

#define MAX_STATES 100

// Stack structure to perform DFS


typedef struct {
int arr[MAX_STATES];
int top;
} Stack;

void initStack(Stack* stack) {


stack->top = -1;
}

int isEmpty(Stack* stack)


{ return stack->top == -
1;
}

void push(Stack* stack, int value)


{ stack->arr[++stack->top] =
value;
}

int pop(Stack* stack)


{ if (!isEmpty(stack))
{
return stack->arr[stack->top--];
}
return -1;
}

// Function to perform DFS and find ε-closure


void findEClosure(int state, int epsilonTransitions[MAX_STATES][MAX_STATES],
int numStates, int epsilonCount[MAX_STATES], int closure[MAX_STATES]) {
Stack stack;
initStack(&stack);

AVINASH KUMAR 2200910100


THAKUR 040
push(&stack, state);

while (!isEmpty(&stack)) {

AVINASH KUMAR 2200910100


THAKUR 040
// If the state is already in closure, skip it
if (closure[currentState]) continue;

// Add the state to closure


closure[currentState] = 1;

// Push all states reachable by ε-transition


for (int i = 0; i < epsilonCount[currentState]; i++) {
int nextState = epsilonTransitions[currentState][i];
if (!closure[nextState]) {
push(&stack, nextState);
}
}
}
}

// Function to compute ε-closure for all states


void computeEClosures(int numStates, int
epsilonTransitions[MAX_STATES][MAX_STATES], int
epsilonCount[MAX_STATES]) {
for (int state = 0; state < numStates; state++)
{ int closure[MAX_STATES] = {0};
findEClosure(state, epsilonTransitions, numStates, epsilonCount, closure);

// Print the ε-closure


printf("ε-Closure of state %d = { ", state);
for (int i = 0; i < numStates; i++) {
if (closure[i]) {
printf("%d ", i);
}
}
printf("}\n");
}
}

int main() {
int numStates, numEpsilonTransitions;

// Input number of states


printf("Enter number of states:
"); scanf("%d", &numStates);

AVINASH KUMAR 2200910100


THAKUR 040
// Initialize epsilon transitions table and epsilon count array
int epsilonTransitions[MAX_STATES][MAX_STATES] = {0};
int epsilonCount[MAX_STATES] = {0};

// Input number of ε-transitions


printf("Enter number of ε-transitions:
"); scanf("%d",
&numEpsilonTransitions);

// Input ε-transitions (from state, to state)


printf("Enter ε-transitions (from state to state):\n");
for (int i = 0; i < numEpsilonTransitions; i++) {
int from, to;
scanf("%d %d", &from, &to); epsilonTransitions[from]
[epsilonCount[from]++] = to;
}

// Compute and print ε-closures


computeEClosures(numStates, epsilonTransitions, epsilonCount);

return 0;
}

Input:
Enter number of states: 4
Enter number of ε-transitions: 4
Enter ε-transitions (from state to state):
01
12
23
33

Output:
ε-Closure of state 0 = { 0 1 2 3 }
ε-Closure of state 1 = { 1 2 3 }
ε-Closure of state 2 = { 2 3
} ε-Closure of state 3 =
{3}

AVINASH KUMAR 2200910100


THAKUR 040
EXPERIEMENT- 6
AIM: Write a code to Implement NFA with epsilon move to DFA Conversion

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100

char NFA_FILE[MAX_LEN];
char buffer[MAX_LEN];
int zz = 0;
struct DFA
{ char
*states; int
count;
} dfa;

int last_index = 0;
FILE *fp;
int symbols;

/* reset the hash map*/


void reset(int ar[], int size) {
int i;

for (i = 0; i < size; i++)


{ ar[i] = 0;
}
}

AVINASH KUMAR 22009101000


THAKUR 40
void check(int ar[], char S[])
{ int i, j;

int len = strlen(S);


for (i = 0; i < len; i++) {

j = ((int)(S[i]) - 65);
ar[j]++;
}
}
void state(int ar[], int size, char S[])
{ int j, k = 0;

for (j = 0; j < size; j++) {


if (ar[j] != 0)
S[k++] = (char)(65 + j);
}

S[k] = '\0';
}

int closure(int ar[], int size)


{ int i;

for (i = 0; i < size; i++) {


if (ar[i] == 1)
return i;
}
return (100);

AVINASH KUMAR 22009101000


THAKUR 40
}

// Check new DFA states can be


// entered in DFA table or not
int indexing(struct DFA *dfa) {
int i;

for (i = 0; i < last_index; i++) {


if (dfa[i].count == 0)
return 1;
}
return -1;
}

/* To Display epsilon closure*/


void Display_closure(int states, int closure_ar[],
char *closure_table[],
char *NFA_TABLE[][symbols + 1],
char *DFA_TABLE[][symbols]) {
int i;

for (i = 0; i < states; i++)


{ reset(closure_ar,
states); closure_ar[i] =
2;

// to neglect blank entry


if (strcmp(&NFA_TABLE[i][symbols], "-") != 0) {

// copy the NFA transition state to buffer


strcpy(buffer, &NFA_TABLE[i][symbols]);
check(closure_ar, buffer);

AVINASH KUMAR 22009101000


THAKUR 40
int z = closure(closure_ar, states);

// till closure get completely saturated


while (z != 100)
{
if (strcmp(&NFA_TABLE[z][symbols], "-") != 0)
{ strcpy(buffer, &NFA_TABLE[z][symbols]);

// call the check function


check(closure_ar, buffer);
}
closure_ar[z]++;
z = closure(closure_ar, states);
}
}

// print the e closure for every states of NFA


printf("\n e-Closure (%c) :\t", (char)(65 + i));

bzero((void *)buffer, MAX_LEN);


state(closure_ar, states, buffer);
strcpy(&closure_table[i], buffer);
printf("%s\n", &closure_table[i]);
}
}

/* To check New States in DFA */


int new_states(struct DFA *dfa, char S[]) {

int i;

AVINASH KUMAR 22009101000


THAKUR 40
for (i = 0; i < last_index; i++) {
if (strcmp(&dfa[i].states, S) == 0)
return 0;
}

// push the new


strcpy(&dfa[last_index++].states, S);

// set the count for new states entered


// to zero
dfa[last_index - 1].count = 0;
return 1;
}
void trans(char S[], int M, char *clsr_t[], int st,
char *NFT[][symbols + 1], char TB[]) {
int len = strlen(S);
int i, j, k, g;
int arr[st];
int sz;
reset(arr, st);
char temp[MAX_LEN], temp2[MAX_LEN];
char *buff;

// Transition function from NFA to DFA


for (i = 0; i < len; i++) {

j = ((int)(S[i] - 65));
strcpy(temp, &NFT[j][M]);

if (strcmp(temp, "-") != 0) {

AVINASH KUMAR 22009101000


THAKUR 40
sz = strlen(temp);
g = 0;

while (g < sz) {


k = ((int)(temp[g] - 65));
strcpy(temp2, &clsr_t[k]);
check(arr, temp2);
g++;
}
}
}

bzero((void *)temp, MAX_LEN);


state(arr, st, temp);
if (temp[0] != '\0') {
strcpy(TB, temp);
} else
strcpy(TB, "-");
}

/* Display DFA transition state table*/


void Display_DFA(int last_index, struct DFA *dfa_states,
char *DFA_TABLE[][symbols]) {
int i, j; printf("\n\n********************************************************\n\
n"); printf("\t\t DFA TRANSITION STATE TABLE \t\t \n\n");
printf("\n STATES OF DFA :\t\t");

for (i = 1; i < last_index; i++)


printf("%s, ", &dfa_states[i].states);

AVINASH KUMAR 22009101000


THAKUR 40
printf("\n");
printf("\n GIVEN SYMBOLS FOR DFA: \t");

for (i = 0; i < symbols; i++)


printf("%d, ", i);
printf("\n\n");
printf("STATES\t");

for (i = 0; i < symbols; i++) printf("|


%d\t", i);
printf("\n");

// display the DFA transition state table


printf(" + \n");
for (i = 0; i < zz; i++) {
printf("%s\t", &dfa_states[i + 1].states);
for (j = 0; j < symbols; j++) { printf("|
%s \t", &DFA_TABLE[i][j]);
}
printf("\n");
}
}

// Driver
Code int
main() { int i,
j, states;
char T_buf[MAX_LEN];

// creating an array dfa structures


struct DFA *dfa_states = malloc(MAX_LEN * (sizeof(dfa)));

AVINASH KUMAR 22009101000


THAKUR 40
states = 6, symbols = 2;

printf("\n STATES OF NFA :\t\t");


for (i = 0; i < states; i++)

printf("%c, ", (char)(65 + i));


printf("\n");
printf("\n GIVEN SYMBOLS FOR NFA: \t");

for (i = 0; i < symbols; i++)

printf("%d, ", i);


printf("eps");
printf("\n\n");
char *NFA_TABLE[states][symbols + 1];

// Hard coded input for NFA table


char *DFA_TABLE[MAX_LEN][symbols];
strcpy(&NFA_TABLE[0][0], "FC");
strcpy(&NFA_TABLE[0][1], "-");
strcpy(&NFA_TABLE[0][2], "BF");
strcpy(&NFA_TABLE[1][0], "-");
strcpy(&NFA_TABLE[1][1], "C");
strcpy(&NFA_TABLE[1][2], "-");
strcpy(&NFA_TABLE[2][0], "-");
strcpy(&NFA_TABLE[2][1], "-");
strcpy(&NFA_TABLE[2][2], "D");
strcpy(&NFA_TABLE[3][0], "E");
strcpy(&NFA_TABLE[3][1], "A");
strcpy(&NFA_TABLE[3][2], "-");

AVINASH KUMAR 22009101000


THAKUR 40
strcpy(&NFA_TABLE[4][0], "A");
strcpy(&NFA_TABLE[4][1], "-");
strcpy(&NFA_TABLE[4][2], "BF");
strcpy(&NFA_TABLE[5][0], "-");
strcpy(&NFA_TABLE[5][1], "-");
strcpy(&NFA_TABLE[5][2], "-");
printf("\n NFA STATE TRANSITION TABLE \n\n\n");
printf("STATES\t");

for (i = 0; i < symbols; i++) printf("|


%d\t", i);
printf("eps\n");

// Displaying the matrix of NFA transition table


printf(" + \n");
for (i = 0; i < states; i++) { printf("%c\
t", (char)(65 + i));

for (j = 0; j <= symbols; j++)


{ printf("|%s \t", &NFA_TABLE[i]
[j]);
}
printf("\n");
}
int closure_ar[states];
char *closure_table[states];

Display_closure(states, closure_ar, closure_table, NFA_TABLE, DFA_TABLE);


strcpy(&dfa_states[last_index++].states, "-");

dfa_states[last_index - 1].count = 1;

AVINASH KUMAR 22009101000


THAKUR 40
bzero((void *)buffer, MAX_LEN);

strcpy(buffer, &closure_table[0]);
strcpy(&dfa_states[last_index++].states, buffer);

int Sm = 1, ind = 1;
int start_index = 1;

// Filling up the DFA table with transition values


// Till new states can be entered in DFA table
while (ind != -1) {
dfa_states[start_index].count = 1;
Sm = 0;
for (i = 0; i < symbols; i++) {

trans(buffer, i, closure_table, states, NFA_TABLE, T_buf);

// storing the new DFA state in buffer


strcpy(&DFA_TABLE[zz][i], T_buf);

// parameter to control new states


Sm = Sm + new_states(dfa_states, T_buf);
}
ind = indexing(dfa_states);
if (ind != -1)
strcpy(buffer, &dfa_states[++start_index].states);
zz++;
}
// display the DFA TABLE
Display_DFA(last_index, dfa_states, DFA_TABLE);

AVINASH KUMAR 22009101000


THAKUR 40
return 0;
}}

OUTPUT:

AVINASH KUMAR 22009101000


THAKUR 40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy