-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.c
More file actions
112 lines (94 loc) · 2.57 KB
/
tokenizer.c
File metadata and controls
112 lines (94 loc) · 2.57 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
103
104
105
106
107
108
109
110
111
112
/*
CS3500 - Software Engineering Project
Created by Jonathan Hanley
Contributed to by:
Karol Przestrzelski
Colin Kelleher
Liam de la Cour
*/
/*
include statements & defining output file
*/
#include <stdio.h>
#include<string.h>
#include <ctype.h>
#include "common.h"
#define OUTPUT_FILE "tokenized.txt"
#define NOMAIN_
/*
Function to reset the output file back to an empty file
*/
void rest_output_files(){
FILE *output;
output = fopen(OUTPUT_FILE, "w");
fclose(output);
}
/*
Adding the appropriate token to the corresponding
input from the file
for example 'O' is assigned to all operators [-,+,/]
and is then written out
*/
char *keys[] = {"F", "I", "O", "L", "R"};
void convert_char_2_object(CharacterType *outputType, char character){
FILE *output;
output = fopen(OUTPUT_FILE, "a");
if (isdigit(character) || character == '.'){
outputType->content[outputType->pointer] = character;
outputType->pointer ++;
if (character == '.'){
outputType->type = 0;
}
}
else if (outputType->pointer > 0){
outputType->content[outputType->pointer] = '\0';
resultToFile(output, outputType, keys);
}
if(character == '*' ||
character == '-' ||
character == '+' ||
character == '/'){
outputType->content[outputType->pointer++] = (char) character;
outputType->type = 2;
resultToFile(output, outputType, keys);
}
else if(character == '('){
outputType->content[outputType->pointer++] = (char) character;
outputType->type = 3;
resultToFile(output, outputType, keys);
}
else if(character == ')'){
outputType->content[outputType->pointer++] = (char) character;
outputType->type = 4;
resultToFile(output, outputType, keys);
}
fclose(output);
}
/*
main function
Used for running the tokenizer
File Handling & Dealing with strctures
*/
int startTokenizer(){
rest_output_files();
char singleLine[150];
char character;
FILE *fPointer;
fPointer = fopen("input.txt", "r");
fgets(singleLine, 150, fPointer);
int len = strlen(singleLine);
CharacterType *outputType = newCharacterType();
for (int index = 0; index <= len; index++) {
character = singleLine[index];
convert_char_2_object(outputType, character);
}
return 0;
}
/*
Used for testing
*/
#ifdef NOMAIN
int main(){
return startTokenizer();
}
#endif