-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLexicalAnalyzer.h
83 lines (78 loc) · 1.33 KB
/
LexicalAnalyzer.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
#pragma once
#include<cctype>
#include<string>
#include<fstream>
#include<iostream>
#include"FaultHandler.h"
using namespace std;
enum Lexical {
IDENFR = 0,//标识符
INTCON,//整形变量
CHARCON,//字符常量
STRCON,//字符串
CONSTTK,//const
INTTK,//int
CHARTK,//char
VOIDTK,//void
MAINTK,//main
IFTK,//if
ELSETK,//else
DOTK,//do
WHILETK,//while
FORTK,//for
SCANFTK,//scanf
PRINTFTK,//printf
RETURNTK,//return
PLUS,//+
MINU,//-
MULT,//*
DIV,// /
LSS,//<
LEQ,//<=
GRE,//>
GEQ,//>=
EQL,//==
NEQ,//!=
ASSIGN,//=
SEMICN,//;
COMMA,//,
LPARENT,//(
RPARENT,//)
LBRACK,//[
RBRACK,//]
LBRACE,//{
RBRACE,//}
UNKNOWN,
END
};
struct Result {
Lexical type;
int value;//value for int or char
string str;//value for string or symbol
};
class LexicalAnalyzer {
public:
LexicalAnalyzer(FaultHandler& f) ;
void readAll(string filename);
Result sym();
Lexical getNextSym();
int lineNumber();
void printResult(ostream& out);
void printResult(ostream& out,Result currentSym);
void homework();
private:
string text;
int ptr;
int line;
int column;
bool newLine;
map<string, Lexical>reservedKey;
Result currentSym;
FaultHandler& faultHandler;
int getUnsignedInteger();
bool isValidChar(char c);
string getString();
string getIdentifier();
map<Lexical, string>lexicalName;
map<Lexical, string>lexicalSymbol;
};