-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
149 lines (140 loc) · 5.18 KB
/
parser.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "lexan.h"
#include "ast.h"
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <unordered_set>
#ifndef MILA_PARSER_H
#define MILA_PARSER_H
namespace mila
{
class ParserException
{
public:
ParserException ( const std::string & );
private:
std::string message;
friend std::ostream & operator << ( std::ostream &, const ParserException & );
};
enum LLType
{
TERMINAL,
NONTERMINAL,
};
enum Nonterminal
{
START, // 0
IDENT, // 1
ARRAY_OR_FUNCTION, // 2
ARRAY_BOUNDS, // 3
FUNCTION_PARAMS, // 4
MINUS_NUMB, // 5
NUMB, // 6
VALUE, // 7
POSITIVE_VALUE, // 8
EXPRESSION, // 9
EXPRESSION2, // 10
OPERATION, // 11
TYPE, // 12
DECLARATIONS, // 13
BLOCK, // 14
VARIABLE, // 15
VARIABLE2, // 16
VARIABLE3, // 17
CONSTANT, // 18
CONSTANT2, // 19
FUNCTION, // 20
PROCEDURE, // 21
PARAMETERS, // 22
PARAMETERS2, // 23
DEFINITION, // 24
STATEMENT, // 25
NEXT_STATEMENT, // 26
NEXT_STATEMENT2, // 27
COMMAND, // 28
CONTROL, // 29
BLOCK_OR_COMMAND, // 30
IF_ELSE_IF, // 31
IF_ELSE, // 32
TO_DOWNTO, // 33
PARAM, // 34
PARAM2, // 35
ASSIGN_OR_CALL, // 36
FOO, // 37
NONTERM_CNT
};
struct LLSymbol
{
LLSymbol ( const std::string & );
LLSymbol ( const char * );
LLSymbol ( const LexicalSymbol & );
LLSymbol ( Nonterminal );
bool isTerminal ( void ) const;
bool isNonterminal ( void ) const;
bool operator == ( const LLSymbol & ) const;
bool operator != ( const LLSymbol & ) const;
bool operator == ( const LexicalSymbol & ) const;
bool operator != ( const LexicalSymbol & ) const;
bool operator == ( const Nonterminal & ) const;
bool operator != ( const Nonterminal & ) const;
friend bool operator == ( const LexicalSymbol &, const LLSymbol & );
friend bool operator != ( const LexicalSymbol &, const LLSymbol & );
friend bool operator == ( const Nonterminal &, const LLSymbol & );
friend bool operator != ( const Nonterminal &, const LLSymbol & );
friend std::ostream & operator << ( std::ostream &, const LLSymbol & );
LLType type;
LexicalSymbol terminal;
Nonterminal nonterminal;
};
class Parser;
typedef std::unique_ptr<ExprAST> ( expandType ) ( const LexicalSymbol & );
typedef std::unique_ptr<ExprAST> ( Parser::*expandPointer ) ( const LexicalSymbol & );
typedef std::unordered_set < LexicalSymbol > LexicalSet;
typedef std::list < LLSymbol > tokenList;
class Parser
{
public:
Parser ( Lexan && lex );
int parseSymbol ( const LexicalSymbol & );
void printQue ( std::ostream & os = std::cerr ) const;
void parse ( void );
private:
void discard ( std::vector < LexicalSymbol > symbols );
int readNumber ( void );
std::string readIdentifier ( void );
std::vector <std::unique_ptr <ExprAST>> getParameters ( void );
std::vector <std::string> getParameterDefinitions ( void );
std::unique_ptr <ExprAST> expression ( void );
std::unique_ptr <ExprAST> ident ( void );
std::unique_ptr <ExprAST> declarations ( void );
std::unique_ptr <ExprAST> command ( void );
std::unique_ptr <ExprAST> control ( void );
std::unique_ptr <ExprAST> block ( void );
std::unique_ptr <ExprAST> blockCommand ( void );
std::unique_ptr <ExprAST> createExpression ( std::vector <std::unique_ptr <ExprAST>> operands, std::vector <OperEnum> operators ) const;
void expansion ( const LexicalSymbol & );
void comparison ( const LexicalSymbol & );
LexicalSymbol getNextLS ( void );
LexicalSymbol peekNextLS ( void );
expandType start;
expandType ident;
expandType expression;
expandType block;
expandType variable;
expandType constant;
expandType constant2;
expandType function;
expandType procedure;
expandType command;
void expect ( tokenList && );
void parserError ( const char *, const LexicalSymbol & ) const;
void parserError ( const LexicalSymbol &, const LexicalSymbol & ) const;
std::map < std::string, int > constants;
std::map < std::string, int > variables;
tokenList expected;
expandPointer parseNonterm [ NONTERM_CNT ];
Lexan lex;
};
}
#endif