-
Notifications
You must be signed in to change notification settings - Fork 0
/
1 to 3 lex
179 lines (143 loc) · 4.06 KB
/
1 to 3 lex
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
1. Write a C program to solve an arithmetic expression using +, - , * , and / operators.
#include <stdio.h>
int main() {
float num1, num2, result;
char op;
printf("Enter an arithmetic expression using +, -, *, and / operators: ");
scanf("%f %c %f", &num1, &op, &num2);
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator.");
return 1;
}
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
return 0;
}
2a. Write a program in C to convert a declaration statement into a sequence of tokens
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKENS 100
int main() {
char declaration[100];
char tokens[MAX_TOKENS][20];
int num_tokens = 0;
char* token;
printf("Enter a declaration statement:\n");
fgets(declaration, 100, stdin);
token = strtok(declaration, " ");
while (token != NULL && num_tokens < MAX_TOKENS) {
strcpy(tokens[num_tokens], token);
num_tokens++;
token = strtok(NULL, " ");
}
printf("The sequence of tokens is:\n");
for (int i = 0; i < num_tokens; i++) {
printf("%s\n", tokens[i]);
}
return 0;
}
2b. Write a program in C to convert control flow statements (condition and loops into a sequence of tokens)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKENS 100
int main() {
char statement[100];
char tokens[MAX_TOKENS][20];
int num_tokens = 0;
char* token;
printf("Enter a control flow statement:\n");
fgets(statement, 100, stdin);
token = strtok(statement, " ();{\n\t");
while (token != NULL && num_tokens < MAX_TOKENS) {
strcpy(tokens[num_tokens], token);
num_tokens++;
token = strtok(NULL, " ();{\n\t");
}
printf("The sequence of tokens is:\n");
for (int i = 0; i < num_tokens; i++) {
printf("%s\n", tokens[i]);
}
return 0;
}
2c. Write a program in C to convert I/O statements (printf and scanf) into a sequence of tokens
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TOKENS 100
int main() {
char statement[100];
char tokens[MAX_TOKENS][20];
int num_tokens = 0;
char* token;
printf("Enter an I/O statement (printf or scanf):\n");
fgets(statement, 100, stdin);
token = strtok(statement, " ();{\n\t\"");
while (token != NULL && num_tokens < MAX_TOKENS) {
strcpy(tokens[num_tokens], token);
num_tokens++;
token = strtok(NULL, " ();{\n\t\"");
}
printf("The sequence of tokens is:\n");
for (int i = 0; i < num_tokens; i++) {
printf("%s\n", tokens[i]);
}
return 0;
}
3a. Write a program in Lex to count the number of words in a string.
%{
int word_count = 0;
%}
%%
[a-zA-Z]+ { word_count++; } /* Match one or more letters */
[ \t\n]+ { /* Ignore whitespace */ }
. { /* Ignore everything else */ }
%%
int main() {
yylex(); /* Run the lexer */
printf("Number of words: %d\n", word_count);
return 0;
}
lex wordcount.l
cc lex.yy.c -o wordcount -ll
./wordcount
3b. Write a program in Lex to count the number of vowels and consonants in a string
%{
int vowel_count = 0;
int consonant_count = 0;
%}
%%
[aAeEiIoOuU] { vowel_count++; } /* Match vowels */
[a-zA-Z] { consonant_count++; } /* Match consonants */
[ \t\n]+ { /* Ignore whitespace */ }
. { /* Ignore everything else */ }
%%
int yywrap() {
return 1;
}
int main() {
char input[256];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin); /* Read input from user */
yy_scan_string(input); /* Set the input for the lexer */
yylex(); /* Run the lexer */
printf("Number of vowels: %d\n", vowel_count);
printf("Number of consonants: %d\n", consonant_count);
return 0;
}
flex lex.l
gcc lex.yy.c
a.exe