-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexicalAnalyzer.class.h
135 lines (131 loc) · 2.62 KB
/
LexicalAnalyzer.class.h
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
#ifndef __LEXICALANALYZER_CLASS_H
#define __LEXICALANALYZER_CLASS_H
#include<iostream>
#include<fstream>
#include<string>
#include<deque>
class LexicalAnalyzer
{
public:
class InputReader
{
protected:
int curline,curchar,curindex,prevline,prevchar;
public:
InputReader():curline(1),curchar(0),curindex(0)
{
}
virtual char peekchar() const=0;
virtual char getchar()=0;
virtual int get_curline() const
{
return this->curline;
}
virtual int get_curchar() const
{
return this->curchar;
}
virtual int get_prevline() const
{
return this->prevline;
}
virtual int get_prevchar() const
{
return this->prevchar;
}
virtual bool eof() const=0;
};
class ConsoleInputReader:public InputReader
{
public:
char peekchar() const
{
if(eof()) return -1;
return std::cin.peek();
}
char getchar()
{
if(eof()) return -1;
char c=std::cin.get();
this->prevline=this->curline;
this->prevchar=this->curchar;
++this->curindex;
if(c=='\n')
{
++this->curline;
this->curchar=0;
}
else ++this->curchar;
return c;
}
bool eof() const
{
return std::cin.eof();
}
};
class FileReader:public InputReader
{
private:
std::string buffer;
public:
bool open(const std::string& path)
{
std::ifstream file(path.c_str(),std::ios::in);
if(file.fail()) return false;
std::getline(file,this->buffer,(char)-1);
file.close();
curline=1;
curchar=curindex=0;
return true;
}
char peekchar() const
{
if(eof()) return -1;
return this->buffer[this->curindex];
}
char getchar()
{
char c=peekchar();
this->prevline=this->curline;
this->prevchar=this->curchar;
++this->curindex;
if(c=='\n')
{
++this->curline;
this->curchar=0;
}
else ++this->curchar;
return c;
}
bool eof() const
{
return this->curindex >= this->buffer.size();
}
};
private:
ConsoleInputReader consoleinputreader;
FileReader filereader;
InputReader *inputreader;
std::string cursymword,prevsymword;
int cursymtype,prevsymtype;
int cursymline,cursymchar,prevsymline,prevsymchar;
public:
bool open(const std::string &path="");
void next();
int peeksymtype() const;
int getsymtype();
std::string peeksymword() const;
std::string getsymword();
int getprevsymtype() const;
std::string getprevsymword() const;
bool eof() const;
int get_curline() const;
int get_curchar() const;
int get_prevline() const;
int get_prevchar() const;
private:
int get_keyword(std::string) const;
int get_operator(const std::string&) const;
bool getsym(std::string&,int&);
};
#endif