CD 1 Deep-8-18 Merged
CD 1 Deep-8-18 Merged
//Lex Code(arith.1)
%{
#include "y.tab.h"
%}
DIGIT [0-9]+
%%
%%
//YACC code(arith.y)
%{
#include <stdio.h>
%}
%%
%%
int main() {
AVINASH KUMAR 2200910100
THAKUR 040
printf("Enter an
expression: "); yyparse();
return 0;
}
Input:
(5 + 3) * 2
Output:
Valid Expression
//Lex code(var.1)
%{
#include "y.tab.h"
%}
LETTER [a-zA-
Z] DIGIT [0-
9]
IDENTIFIER {LETTER}({LETTER}|{DIGIT})*
%%
%%
//YACC code(var.y)
%{
#include <stdio.h>
%}
%token IDENTIFIER
%%
%%
int main()
{ yypars
e();
return
0;
}
AVINASH KUMAR 2200910100
THAKUR 040
int yyerror(char *s) {
printf("Invalid variable\
n");
Input:
myVar123
Output:
Valid variable
//Lex code(calc.1)
%{
#include "y.tab.h"
%}
DIGIT [0-9]+
%%
%%
//YACC code(calc.y)
%{
#include <stdio.h>
%}
%%
%%
int main() {
printf("Enter an arithmetic
expression: "); yyparse();
return 0;
}
Input:
3+5*2
Output:
Valid Expression
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;
%}
%%
int main()
{ yypars
e();
return
0;
}
#include <stdio.h>
#include <stdlib.h>
while (!isEmpty(&stack)) {
int main() {
int numStates, numEpsilonTransitions;
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}
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;
j = ((int)(S[i]) - 65);
ar[j]++;
}
}
void state(int ar[], int size, char S[])
{ int j, k = 0;
S[k] = '\0';
}
int i;
j = ((int)(S[i] - 65));
strcpy(temp, &NFT[j][M]);
if (strcmp(temp, "-") != 0) {
// Driver
Code int
main() { int i,
j, states;
char T_buf[MAX_LEN];
dfa_states[last_index - 1].count = 1;
strcpy(buffer, &closure_table[0]);
strcpy(&dfa_states[last_index++].states, buffer);
int Sm = 1, ind = 1;
int start_index = 1;
OUTPUT: