-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpract3.cpp
More file actions
102 lines (90 loc) · 2.98 KB
/
pract3.cpp
File metadata and controls
102 lines (90 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// Define the token types
typedef enum {
IDENTIFIER,
NUMBER,
OPERATOR,
PUNCTUATION,
UNKNOWN
} TokenType;
// Define the structure of a token
typedef struct {
TokenType type;
char value[100];
} Token;
// Function to classify and tokenize the input string
int tokenize(const char* input, Token tokens[], int maxTokens) {
int i = 0, tokenIndex = 0;
while (input[i] != '\0' && tokenIndex < maxTokens) {
if (isalpha(input[i])) {
// Process identifier
int j = 0;
while (isalnum(input[i]) || input[i] == '_') {
tokens[tokenIndex].value[j++] = input[i++];
}
tokens[tokenIndex].value[j] = '\0';
tokens[tokenIndex].type = IDENTIFIER;
tokenIndex++;
} else if (isdigit(input[i])) {
// Process number
int j = 0;
while (isdigit(input[i])) {
tokens[tokenIndex].value[j++] = input[i++];
}
tokens[tokenIndex].value[j] = '\0';
tokens[tokenIndex].type = NUMBER;
tokenIndex++;
} else if (ispunct(input[i])) {
// Process operator or punctuation
tokens[tokenIndex].value[0] = input[i];
tokens[tokenIndex].value[1] = '\0';
if (input[i] == '=' || input[i] == '+' || input[i] == '-' ||
input[i] == '*' || input[i] == '/' || input[i] == ';') {
tokens[tokenIndex].type = OPERATOR;
} else {
tokens[tokenIndex].type = PUNCTUATION;
}
tokenIndex++;
i++;
} else {
// Process unknown token
tokens[tokenIndex].value[0] = input[i];
tokens[tokenIndex].value[1] = '\0';
tokens[tokenIndex].type = UNKNOWN;
tokenIndex++;
i++;
}
}
return tokenIndex; // Return the total number of tokens generated
}
// Function to print the token details
void printTokens(Token tokens[], int tokenCount) {
for (int i = 0; i < tokenCount; i++) {
printf("Token Type: ");
switch (tokens[i].type) {
case IDENTIFIER: printf("IDENTIFIER"); break;
case NUMBER: printf("NUMBER"); break;
case OPERATOR: printf("OPERATOR"); break;
case PUNCTUATION: printf("PUNCTUATION"); break;
case UNKNOWN: printf("UNKNOWN"); break;
}
printf(", Value: %s\n", tokens[i].value);
}
}
int main() {
char input[100];
Token tokens[100]; // Array to store tokens
int tokenCount;
// Read input from the user
printf("Enter a string to tokenize: ");
fgets(input, sizeof(input), stdin);
// Remove the newline character at the end (if any)
input[strcspn(input, "\n")] = '\0';
// Tokenize the input string
tokenCount = tokenize(input, tokens, 100);
// Print the resulting tokens
printTokens(tokens, tokenCount);
return 0;
}