CD File
CD File
#include
<stdio.h>
#include
<ctype.h>
#include
<string.h>
// Token
types
typedef
enum {
TOKEN_IDENTIFIER,
TOKEN_KEYWORD,
TOKEN_NUMBER,
TOKEN_OPERATOR,
TOKEN_SYMBOL,
TOKEN_EOF
} TokenType;
// List of keywords
const char *keywords[] = { "if", "else", "while", "return", "int", "float",
"char", "void"
};
#define KEYWORDS_COUNT (sizeof(keywords) / sizeof(keywords[0]))
if (ch == EOF) {
token.type = TOKEN_EOF;
strcpy(token.value, "EOF");
return token;
}
// Identifier or Keyword
if (isalpha(ch) || ch ==
'_') { int i = 0;
char buffer[MAX_TOKEN_LENGTH];
buffer[i++] = ch;
character if (isKeyword(buffer)) {
token.type = TOKEN_KEYWORD;
} else {
token.type = TOKEN_IDENTIFIER;
}
strcpy(token.value,
buffer); return token;
}
// Number
if (isdigit(ch))
{ int i = 0;
char buffer[MAX_TOKEN_LENGTH];
buffer[i++] = ch;
ungetc(ch, file);
token.type =
TOKEN_NUMBER;
strcpy(token.value, buffer);
return token;
}
// Operators
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' || ch == '<' ||
ch == '>') { token.type = TOKEN_OPERATOR;
token.value[0] =
ch; token.value[1]
= '\0'; return
token;
}
// Symbols
if (ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == ';' || ch ==
',') { token.type = TOKEN_SYMBOL;
token.value[0] =
ch; token.value[1]
= '\0'; return
token;
}
// Unknown character
token.type =
TOKEN_EOF;
strcpy(token.value,
"UNKNOWN"); return token;
}
fclose(file)
; return
0;
}
=>input.txt
int main()
{ int a =
10;
float b =
20.5; if (a <
b) {
return a;
}
}
Output –
KEYWORD: int
IDENTIFIER:
main SYMBOL: (
SYMBOL: )
SYMBOL:
{ KEYWORD: int
IDENTIFIER: a
OPERATOR: =
%%
%%
int main()
{ yylex(
);
return
0;
}
int
yywrap()
{ return
1;
}
//Input.txt
AVINASH KUMAR THAKUR 2200910100040
int main()
{ int a
= 10;
float b = 20.5;
Output-
KEYWORD: int
IDENTIFIER:
main SYMBOL: (
SYMBOL: )
SYMBOL:
{ KEYWORD: int
IDENTIFIER: a
OPERATOR: =
NUMBER: 10
SYMBOL: ;
KEYWORD:
float
IDENTIFIER: b
OPERATOR: =
NUMBER: 20.5
SYMBOL: ;
KEYWORD: if
SYMBOL:
( IDENTIFIER:
a OPERATOR:
<=
IDENTIFIER: b
SYMBOL: )
SYMBOL:
{ IDENTIFIER:
a OPERATOR:
= IDENTIFIER:
a OPERATOR:
+ NUMBER: 5
SYMBOL: ;
SYMBOL: }
SYMBOL: }
//Lex Code(arith.1)
%{
#include "y.tab.h"
%}
DIGIT [0-9]+
%%
%%
//YACC code(arith.y)
%{
#include <stdio.h>
%}
%%
%%
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() {
yyparse();
return 0;
}
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
// YACC code(ast.y)
%{
#include <stdio.h>
#include <stdlib.h>
typedef struct
Node { char
*value;
struct Node *left;
struct Node *right;
} Node;
%}
%%
%%
int main() {
yyparse();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
while (!isEmpty(&stack)) {
AVINASH KUMAR THAKUR 2200910100040
int currentState = pop(&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 }
#include <stdlib.h>
#define MAX_ALPHABET 10
char alphabet[MAX_ALPHABET];
int epsilonTransitions[MAX_STATES][MAX_STATES];
int transitions[MAX_STATES][MAX_ALPHABET][MAX_STATES];
int epsilonCount[MAX_STATES];
int transitionCount[MAX_STATES][MAX_ALPHABET];
typedef struct {
int arr[MAX_STATES];
int top;
} Stack;
{ stack->top = -1;
{ stack->arr[++stack->top] =
value;
{ Stack stack;
initStack(&stack);
push(&stack, state);
if (!closure[current])
{ closure[current] = 1;
{ push(&stack, epsilonTransitions[current]
[i]);
void convertNFA() {
if (closure[q]) {
newTransitions[state][i][newTransitionCount[state][i]++] = nextState;
transitions[i][j][k] = newTransitions[i][j][k];
printf("}\n");
}
AVINASH KUMAR THAKUR 2200910100040
}
int main() {
scanf("%d", &numStates);
scanf("%d", &numAlphabet);
// Input ε-transitions
scanf("%d", &numEpsilonTransitions);
[epsilonCount[from]++] = to;
scanf("%d", &numRegularTransitions);
char symbol;
if (alphabet[j] == symbol)
symbolIndex = j;
[symbolIndex]++] = to;
convertNFA();
return 0;
Input:
01
12
1a2
2b0
Output:
δ(0, a) -> { 2 }
δ(0, b) -> { 0 }
δ(1, a) -> { 2 }
δ(1, b) -> { 0 }
δ(2, a) -> { }
δ(2, b) -> { 0 }