CDExperiment 3-6
CDExperiment 3-6
C program that recognizes white spaces, counts the number of identifiers, characters, tabs, and the
length of the input string:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char input[1000];
int identifiers = 0, characters = 0, tabs = 0, length = 0;
int inIdentifier = 0;
if (isspace(input[i])) {
if (input[i] == '\t') {
tabs++;
}
inIdentifier = 0;
} else {
if (!inIdentifier && isalnum(input[i])) {
identifiers++;
inIdentifier = 1;
}
if (isalnum(input[i]) || input[i] == '_') {
characters++;
}
}
}
return 0;
}
Example String (Output):
#include <stdio.h>
#include <string.h>
return 0;
}
int main() {
char line[1000];
if (isComment(line)) {
printf("The line is a comment.\n");
} else {
printf("The line is not a comment.\n");
}
return 0;
}
Example String (Output):
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int n, i, j, k;
printf("\\nEnter number of non terminals: ");
scanf("%d", &n);
printf("\\nEnter non terminals one by one: ");
int new_n = n;
for (i = 0; i < n; ++i) {
if (leftrecr[i] == 0) {
continue;
}
new_n++;
char *new_nonter = (char*)malloc(100 * sizeof(char));
sprintf(new_nonter, "%s'", nonter[i]);
nonter = (char**)realloc(nonter, new_n * sizeof(char*));
nonter[new_n - 1] = new_nonter;
int new_k = 0;
for (j = 0; j < k; ++j) {
if (strlen(nonter[i]) <= strlen(prod[i][j]) && strncmp(nonter[i],
prod[i][j], strlen(nonter[i])) == 0) {
new_k++;
}
}
char **new_prod = (char**)malloc((new_k + 1) * sizeof(char*));
int p = 0;
for (j = 0; j < k; ++j) {
if (strlen(nonter[i]) <= strlen(prod[i][j]) && strncmp(nonter[i],
prod[i][j], strlen(nonter[i])) == 0) {
new_prod[p] = (char*)malloc(100 * sizeof(char));
sprintf(new_prod[p], "%s%s'", prod[i][j] + strlen(nonter[i]),
nonter[i]);
p++;
} else {
prod[i][j] = (char*)realloc(prod[i][j], (strlen(prod[i][j]) +
strlen(new_nonter) + 1) * sizeof(char));
strcat(prod[i][j], new_nonter);
}
}
new_prod[p] = (char*)malloc(2 * sizeof(char));
strcpy(new_prod[p], "^");
prod = (char***)realloc(prod, new_n * sizeof(char**));
prod[new_n - 1] = new_prod;
}
printf("\\n\\n");
printf("\\nNew set of non-terminals: ");
for (i = 0; i < new_n; ++i) {
printf("%s ", nonter[i]);
}
printf("\\n\\nNew set of productions: ");
for (i = 0; i < new_n; ++i) {
for (j = 0; prod[i][j] != NULL; ++j) {
printf("\\n%s -> %s", nonter[i], prod[i][j]);
}
}
return 0;
}
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int p, i, j, k, l, e = 1;
char buffer[10];
if (k == 0)
continue;
char comleft[MAX_LEN];
strcpy(comleft, prodleft[i]);
if (k == strlen(prodright[i])) {
sprintf(buffer, "%d", e);
strcat(prodleft[i], buffer);
strcat(prodleft[j], buffer);
strcpy(prodright[i], "^");
strcpy(prodright[j], prodright[j] + k);
} else if (k == strlen(prodright[j])) {
sprintf(buffer, "%d", e);
strcat(prodleft[i], buffer);
strcat(prodleft[j], buffer);
strcpy(prodright[j], "^");
strcpy(prodright[i], prodright[i] + k);
} else {
sprintf(buffer, "%d", e);
strcat(prodleft[i], buffer);
strcat(prodleft[j], buffer);
strcpy(prodright[j], prodright[j] + k);
strcpy(prodright[i], prodright[i] + k);
}
strcpy(prodleft[p], comleft);
sprintf(prodright[p], "%s%s", com, prodleft[i]);
++p;
++e;
}
}
}
printf("\\n\\nNew productions");
for (i = 0; i < p; ++i) {
printf("\\n%s->%s", prodleft[i], prodright[i]);
}
return 0;
}