-
Notifications
You must be signed in to change notification settings - Fork 1
/
s2.c
132 lines (116 loc) · 3.67 KB
/
s2.c
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
/*
==================================================
EECS3301 Project 1 Version A
Jun Lin Chen chen256@my.yorku.ca
Vishal Malik vishal27@my.yorku.ca
Tong Wu malan52.82@gmail.com
==================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
/*====================================================*/
/* Data Type and Variable Declaration */
/*====================================================*/
#define MAX_LEXEME_LENGTH 100
#define CLASS_OTHERS 0
#define CLASS_LETTER 1
#define CLASS_DIGIT 2
#define CLASS_SYMBOL 3
struct Lexeme{
int tokenClass;
char * value;
size_t length;
struct Lexeme * next;
};
struct Lexeme * lexemeList;
/*========================================*/
/* Buffer & Lexical Analysis */
/*========================================*/
/*
Create New Lexeme String Function
Allocate memory space for Lexeme. The lexeme will be stored as a string (char[]).
Maximum size is defined by MAX_LEXEME_LENGTH.
Return:
pointer to char[]. the string of lexeme
*/
char * createNewLexemeString(){
char * temp = malloc(MAX_LEXEME_LENGTH);
return temp;
}
/*
Create New Lexeme Structure Function
Allocate memory space for lexeme structure. One lexeme structure contains one lexeme and its character class.
Return:
pointer to a empty and new lexeme structure
*/
struct Lexeme * createNewLexeme(){
struct Lexeme * temp = malloc(sizeof(struct Lexeme));
temp->length = 0;
temp->value = createNewLexemeString();
return temp;
}
/*
Read Source Function
Read script from standard input. And store it in buffer (lexemeList).
This is like a light compiling process.
Argument:
pointer - allows you to continue writing on the buffer.
*/
void readSource(struct Lexeme * pointer){
if (pointer == NULL){
pointer = createNewLexeme();
lexemeList = pointer;
}
bool appendLexeme = false;
char c;
while ((c = getchar()) != EOF){
if (isdigit(c)) {
if (!appendLexeme){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_DIGIT;
appendLexeme = true;
}
if (pointer->length < MAX_LEXEME_LENGTH){ // avoid out of index problem
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
}
}else if (isalpha(c)) {
if (!appendLexeme){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_LETTER;
appendLexeme = true;
}
if (pointer->length < MAX_LEXEME_LENGTH){ // avoid out of index problem
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
}
}else if (ispunct(c)){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_SYMBOL;
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
appendLexeme = false;
}else{
appendLexeme = false;
continue;
}
}
}
/*========================================*/
/* MAIN */
/*========================================*/
//Main Function
int main(){
readSource(NULL);
struct Lexeme * p = lexemeList;
while ((p = p->next)!=NULL){
printf("Lexeme is %s\n", p->value);
}
return 0;
}