SPCC Exp3
SPCC Exp3
Lab Outcome: Identify and Validate tokens for given high level language and
Implement synthesis phase of compiler.
_______________________________
Practical Incharge
EXPERIMENT NO-03
Theory:
ALGORITHM / PROCEDURE/PROGRAM:
1. Start
2. Read the input file/text
3. Initialize the counters for characters, words, lines to zero
4. Scan the characters, words, lines and
5. increment the respective counters
6. Display the counts
7. End
Program:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
FILE *input, *output;
int l = 1;
int t = 0;
int j = 0;
int i, flag;
char keyword[6][6] = {"int", "main", "if", "else", "do", "while"};
char ch, str[20];
input = fopen("input.txt", "r");
output = fopen("output.txt", "w");
fprintf(output, "Line no. \t Token no. \t Token \t Lexeme\n\n");
while (!feof(input)) {
i = 0;
flag = 0;
ch = fgetc(input);
ch = fgetc(input);
str[i] = '\0';
for (j = 0; j <= 5; j++) {
if (strcmp(str, keyword[j]) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
fprintf(output, "%7d\t\t %7d\t\t Keyword\t %7s\n", l, t, str);
t++;
} else {
fprintf(output, "%7d\t\t %7d\t\t Identifier\t %7s\n", l, t, str);
t++;
}
} else if (ch == '\n') {
l++;
}
}
fclose(input);
fclose(output);
return 0;
}
Input.txt:
Output: