0% found this document useful (0 votes)
4 views

Practical Implementation of Scanner

The document outlines the practical implementation of a scanner that processes input from a text file, tokenizes it, and identifies keywords, operators, identifiers, and constants. It describes the steps to create static and dynamic tables for storing these elements and provides a code example in C that demonstrates the functionality. The output format for identified tokens is specified, showing how each token is categorized and indexed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practical Implementation of Scanner

The document outlines the practical implementation of a scanner that processes input from a text file, tokenizes it, and identifies keywords, operators, identifiers, and constants. It describes the steps to create static and dynamic tables for storing these elements and provides a code example in C that demonstrates the functionality. The output format for identified tokens is specified, showing how each token is categorized and indexed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical implementation of Scanner

Step 1 Generate text file for given Input


Step 2 Declare two static table for Operator and Keywords
Step 3 Declare two dynamic table for constant and Symbol
Step 4 Read Input file apply STRTOK () to tokenize given input string (get logic from
Help menu)
Step 5 In tokenization While loop, for each token
• Check for keywords from keyword table if it exists then print
[KW#index], where index is the record number in the respective table.
• Else Check for Operator from Operator table if it exists then print
[OP#index], where index is the record number in the respective table.
• Else check that given token is digit then check whether it exists in
constant table then print [CO#index], where index is the record number
in the respective table, else store digits in constant table then print
[CO#index]
• Else check that given token exists in symbol table then print [ID#index],
where index is the record number in the respective table, else store
symbol in symbol table then print [ID#index]
Ex: INPUT :
INT a , b ;
REAL c , d ;
a = b + c * 100 ;
d = a – 90 ;

Static Table:
OP , ; = * + -
TABLE

KW TABLE INT REAL

Dynamic table:
ID TABLE a b c d

CO TABLE 100 90

OUTPUT:

[KW #1] [ID #1] [OP #1] [ID#2][OP#2]


[KW#2] [ID#3][OP#1][ID#4][OP#2]
[ID#1][OP#3][[ID#2][OP#5][ID#3][OP#4][CO#1][OP#2]
[ID#4][OP#3][ID#1][OP#6][CO#2][OP#2]

Code:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h> // TurboC specific header for console I/O

char kw[32][10] = {"int", "float", "while", "for", "do", "char", "break",


"auto", "continue", "default", "double", "if", "else",
"enum", "goto", "long", "switch", "typedef", "union",
"unsigned", "void", "volatile", "extern", "case",
"const", "return", "sizeof", "static", "struct",
"register", "signed"};

char op[15] = {'+', '-', '*', '/', '=', ':', ';', '<', '>',','};

char identifiers[20][10]; // Global array to store identifiers


char constants[20][10]; // Global array to store constants
int ic = 0, cc = 0; // Global counters for identifiers and constants

void analyzeString(char str[]);

int main() {
FILE *file;
char str[100];

file = fopen("input.txt", "r");

if (file == NULL) {
printf("Error opening the file.\n");
getch(); // Wait for a key press
return 1; // Return an error code
}

while (fgets(str, sizeof(str), file) != NULL) {


analyzeString(str);
}

fclose(file);

getch(); // Wait for a key press before closing the console window
return 0;
}

void analyzeString(char str[]) {


char *ptr;
int i, j;

ptr = strtok(str, " \n");

while (ptr != NULL) {


int flag = 0;

for (i = 0; i < 32; i++) {


if (strcmp(ptr, kw[i]) == 0) {
printf("KW#%d ", i + 1);
flag = 1;
break;
}
}

if (flag == 0) {
for (j = 0; j < 10; j++) {
if (ptr[0] == op[j]) {
printf("OP#%d ", j + 1);
flag = 1;
break;
}
}
}

if (flag == 0) {
if (isalpha(ptr[0])) {
int isRepeated = 0;
for (i = 0; i < ic; i++) {
if (strcmp(ptr, identifiers[i]) == 0) {
printf("ID#%d ", i + 1);
isRepeated = 1;
break;
}
}
if (!isRepeated) {
strcpy(identifiers[ic++], ptr);
printf("ID#%d ", ic);
}
} else if (isdigit(ptr[0])) {
int isRepeated = 0;
for (i = 0; i < cc; i++) {
if (strcmp(ptr, constants[i]) == 0) {
printf("CO#%d ", i + 1);
isRepeated = 1;
break;
}
}
if (!isRepeated) {
strcpy(constants[cc++], ptr);
printf("CO#%d ", cc);
}
}
}

ptr = strtok(NULL, " \n");


}
}

File—
int a , b ;
float c , d ;
a = b + c * 100 ;
d = a – 90 ;
c = 90 ;

You might also like