-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUA.hpp
131 lines (118 loc) · 4.22 KB
/
LUA.hpp
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
#include <bits/stdc++.h>
#include "CONSTANT.hpp"
using namespace std;
#define INPUT "input.txt"
#define OUTPUT "output.txt"
struct node{
int type;
string val;
int line;
};
enum state {start, int_i, int_in, int_int, space, id, equals, constant_int, comma};
map <pair<state,char>,state> transition;
void insert_alpha(state curr, state next){
for (size_t i = 0; i < 26; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('a'+i)}, next));
}
for (size_t i = 0; i < 26; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('A'+i)}, next));
}
transition.insert(pair <pair<state,char>,state> ({curr,'_'},next));
}
void insert_star(state curr, state next){
for (size_t i = 0; i < 26; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('a'+i)}, next));
}
for (size_t i = 0; i < 26; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('A'+i)}, next));
}
for (size_t i = 0; i < 10; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('0'+i)}, next));
}
transition.insert(pair <pair<state,char>,state> ({curr,'_'},next));
}
void insert_no(state curr, state next){
for (size_t i = 0; i < 10; i++) {
transition.insert(pair <pair<state,char>,state> ({curr,(char)('0'+i)}, next));
}
}
void init(){
// start
transition.insert(pair <pair<state,char>,state> ({start,' '}, start));
transition.insert(pair <pair<state,char>,state> ({start,'i'}, int_i));
// int_i
transition.insert(pair <pair<state,char>,state> ({int_i,'n'}, int_in));
// int_in
transition.insert(pair <pair<state,char>,state> ({int_in,'t'}, int_int));
// int_int
transition.insert(pair <pair<state,char>,state> ({int_int,' '}, space));
// space
transition.insert(pair <pair<state,char>,state> ({space,' '}, space));
transition.insert(pair <pair<state,char>,state> ({space,','}, comma));
transition.insert(pair <pair<state,char>,state> ({space,'='}, equals));
insert_alpha(space,id);
insert_no(space,constant_int);
// id
insert_star(id,id);
transition.insert(pair <pair<state,char>,state> ({id,';'}, start));
transition.insert(pair <pair<state,char>,state> ({id,'='}, equals));
transition.insert(pair <pair<state,char>,state> ({id,','}, comma));
transition.insert(pair <pair<state,char>,state> ({id,' '}, space));
// equals
insert_no(equals,constant_int);
transition.insert(pair <pair<state,char>,state> ({equals,' '}, space));
// constant_int
insert_no(constant_int,constant_int);
transition.insert(pair <pair<state,char>,state> ({constant_int,','}, comma));
transition.insert(pair <pair<state,char>,state> ({constant_int,';'}, start));
transition.insert(pair <pair<state,char>,state> ({constant_int,' '}, space));
// comma
insert_star(comma,id);
transition.insert(pair <pair<state,char>,state> ({comma,' '}, space));
}
void print(vector<string> code){
for (size_t i = 0; i < code.size(); i++) {
cout<<i<<" --> ";
cout<<code[i]<<endl;
}
}
void print(vector<struct node> parsed){
for (size_t i = 0; i < parsed.size(); i++) {
cout << "----------------------------------" << '\n';
cout << "Type: " << parsed[i].type << '\n';
cout << "Val: " << parsed[i].val << '\n';
cout << "Line: " << parsed[i].line << '\n';
}
}
vector<string> input(){
// reading input file using stringstream
ifstream in(INPUT);
string line;
vector<string> code;
while(getline(in,line)){
// read line by line
stringstream linestream(line);
string value;
while(getline(linestream,value)){
code.push_back(line);
}
}
return code;
}
void dfa(string statement, int line, vector<struct node> parsed){
state current = start;
int curr = 0;
while(statement[curr]!='\0'){
std::cout << STATE[current] << " --> ";
current = transition[{current,statement[curr]}];
curr++;
std::cout << STATE[current] << '\n';
}
}
vector<struct node> parser(vector<string> code){
vector<struct node> parsed;
for (size_t i = 0; i < code.size(); i++) {
dfa(code[i],i,parsed);
}
return parsed;
}