/
compiler.c
95 lines (84 loc) · 1.8 KB
/
compiler.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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
// Terminals
// level l[0-9]+
// FirstName [A-Z][a-z]*
// LastName [a|c-z]+
// cid [0-9]+
// battle b
enum TYPE {
LEVEL,
...
};
struct Scanner {
char * input;
char * forword;
char * lexeme;
};
struct Parser {
struct Scanner * scanner;
enum TYPE type;
};
char *
user_input(void) {
size_t capa = 80 + 1; // last char: '\n'
char * input = malloc(capa);
fgets(input, capa, stdin);
return input;
}
struct Scanner *
new_scanner(void) {
struct Scanner * scanner = malloc(sizeof(struct Scanner));
char * input = user_input();
scanner->input = input;
scanner->forword = input;
scanner->lexeme = input;
return scanner;
}
bool
level_p(char c) {
return c == 'l';
}
struct Token *
scan(struct Scanner * scanner) {
char * forword = scanner->forword;
char * lexeme = scanner->lexeme;
enum TYPE type;
if(level_p(* forword)) {
forword += 1;
if(!number_p(* forword)) {
raise_error();
}
do {
forword += 1;
} while(number_p(* forword));
type = LEVEL;
} else if(first_name_p(forword)) {
} else if(last_name_p(forword)) {
} else if(cid_p(forword)) {
} else if(battle_p(forword)) {
} else if(end_of_line_p(forword)) {
}
scanner->forword = forword;
scanner->lexeme = forword;
return new_token(type, lexeme, forword - lexeme);
}
void
parse(struct Parser * parser) {
next(parser);
parse_Proc(parser);
}
void
next(struct Parser * parser) {
struct Token * token = scan(scanner);
parser->type = token->type;
}
int
main(void) {
struct Scanner * scanner = new_scanner();
struct Parser * parser = new_parser();
parse(parser);
return 0;
}