-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexicalAnalyzer.c
176 lines (160 loc) · 4.09 KB
/
LexicalAnalyzer.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "LexicalAnalyzer.h"
Token tokens[TOKENS_MAX];
int isSymb(char c) {
switch(c) {
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=':
case '<':
case '>':
case ',':
case '.':
case ';':
case ':':
return 1;
default:
return 0;
}
}
int is2LetSymb(char c1, char c2) {
switch (c1) {
case '<':
if (c2 == '>')
return 1;
case '>':
case ':':
if (c2 == '=')
return 1;
default:
return 0;
}
}
int getTknType(char *tkn) {
if (!strcmp(tkn, "begin")
|| !strcmp(tkn, "end")
|| !strcmp(tkn, "if")
|| !strcmp(tkn, "then")
|| !strcmp(tkn, "while")
|| !strcmp(tkn, "do")
|| !strcmp(tkn, "return")
|| !strcmp(tkn, "function")
|| !strcmp(tkn, "var")
|| !strcmp(tkn, "const")
|| !strcmp(tkn, "odd")
|| !strcmp(tkn, "write")
|| !strcmp(tkn, "writeln"))
return Keyword;
else if (!strcmp(tkn, "+")
|| !strcmp(tkn, "-")
|| !strcmp(tkn, "*")
|| !strcmp(tkn, "/")
|| !strcmp(tkn, "(")
|| !strcmp(tkn, ")")
|| !strcmp(tkn, "=")
|| !strcmp(tkn, "<")
|| !strcmp(tkn, ">")
|| !strcmp(tkn, "<>")
|| !strcmp(tkn, "<=")
|| !strcmp(tkn, ">=")
|| !strcmp(tkn, ",")
|| !strcmp(tkn, ".")
|| !strcmp(tkn, ";")
|| !strcmp(tkn, ":="))
return Symbol;
else if (atoi(tkn))
return Number;
else
return Ident;
}
char *getTknTypeStr(TknType type) {
switch(type) {
case 0:
return "Keyword";
case 1:
return "Symbol";
case 2:
return "Ident";
case 3:
return "Number";
}
}
// tknCnt++ -> 表示 の方式へ変更すべし, lexResShwで判断
void lexicalAnalize(FILE *fp, bool lexResShw) {
// 字句配列位置指示等用
int tknCnt = 0;
int idx = 0;
// 計数用
int line = 1;
int chr = 0;
char c;
while ((c = getc(fp)) != EOF) {
/* エラー処理用 ------------------------------------------------------ */
if (c != '\n')
chr++;
/* ------------------------------------------------------------------- */
if (isdigit(c)) { /* 数字: Ident/Keyword | Number */
if (idx != 0 && isSymb(tokens[tknCnt].string[0])) { // 開始文字: 記号 -> Symbol
tknCnt++;
idx = 0;
}
tokens[tknCnt].string[idx] = c;
idx++;
} else if (isalpha(c)) { /* 英字: Ident/Keyword */
if (idx != 0 && !isalpha(tokens[tknCnt].string[0])) { // 開始文字: 非英字 -> Number | Symbol
tknCnt++;
idx = 0;
}
tokens[tknCnt].string[idx] = c;
idx++;
} else if(isSymb(c)) { /* 記号: Symbol */
if (idx != 0) // 非開始文字時
if (!isSymb(tokens[tknCnt].string[0])) { // 開始文字: 非記号 -> Ident/Keyword | Number
tknCnt++;
idx = 0;
} else if (!is2LetSymb(tokens[tknCnt].string[idx - 1], c)) { // 開始文字: 非特定 -> 次字句へ
tknCnt++;
idx = 0;
}
tokens[tknCnt].string[idx] = c;
idx++;
} else if (isspace(c) || c == '\t' || c == '\n') { /* {空格 | タブ | 改行} 文字 */
/* エラー処理用 -------------------------------------------------- */
if (c == '\n') {
line++;
chr = 0;
}
/* --------------------------------------------------------------- */
if (idx == 0) // 開始文字 -> 棄却
continue;
tokens[tknCnt].string[idx + 1] = '\0';
tknCnt++;
idx = 0;
} else {
printf("[エラー] 認識不能な文字が出現\n");
printf(" 場所: 第%d行 第%d字\n", line, chr);
printf(" 位置: 第%d字句 第%d字後\n", tknCnt, idx);
printf(" \"%s%c\"\n", tokens[tknCnt].string, c);
printf(" 文字: %c\n", c);
printf("\n");
exit(-1);
}
}
/* TknType 付与 ----------------------------------------------------------- */
for (int i = 0; i < tknCnt; i++)
tokens[i].type = getTknType(tokens[i].string);
/* ------------------------------------------------------------------------ */
/* 字句解析結果表示 ------------------------------------------------------- */
if (lexResShw)
for (int i = 0; i < tknCnt; i++)
printf("%3d: %15s %15s\n", i, tokens[i].string, getTknTypeStr(tokens[i].type));
/* ------------------------------------------------------------------------ */
}