-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
139 lines (117 loc) · 2.88 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
/*
* Copyright (C) Rida Bazzi, 2020
*
* Do not share this file with anyone
*
* Do not post this file or derivatives of
* of this file online
*
*/
#ifndef __PARSER_H__
#define __PARSER_H__
#include <string>
#include "lexer.h"
//////////////////////////////////////////////////////
// Data structures
//////////////////////////////////////////////////////
// variable/input table
typedef struct input_table {
int next_i;
std::vector<std::pair<std::string, int>> var_map;
std::vector<int> input_map;
int add_var(std::string var_name);
void add_input(int in);
int get_var(std::string str);
input_table();
} input_table;
// polynomial components
typedef struct monomial {
int var_name; // index of the variable within the polynomial's parameters
int exp; // the power to which the variable is raised
monomial();
} monomial;
typedef struct term {
int coefficient;
std::vector<monomial*> m_list;
char op;
term();
} term;
// polynomial declaration table
typedef struct polynomial {
std::string name;
int decl_lineno;
int param_i;
std::map<int, std::string> param_names;
std::vector<term*> polynomial_body;
void add_param(std::string p);
int get_param(std::string p);
void get_var(std::string str);
polynomial();
} polynomial;
// polynomial_evaluation DS
struct arg;
typedef struct poly_eval {
int lineno;
int poly;
std::vector<arg*>* alist;
poly_eval();
} poly_eval;
typedef struct arg {
TokenType etype;
int value;
int index;
poly_eval* peval;
arg();
} arg;
// statement DS
typedef struct stmt {
TokenType stmt_type;
poly_eval* pe;
int variable;
stmt* next;
stmt();
} stmt;
class Parser {
public:
void execute_program(stmt* start);
int evaluate_polynomial(poly_eval* pe);
void parse_input();
stmt* parse_program();
void parse_poly_decl_section();
polynomial* parse_poly_decl();
void parse_polynomial_header(polynomial* p);
void parse_id_list(polynomial* p);
std::string parse_polynomial_name();
void parse_polynomial_body(polynomial* p);
void parse_term_list(polynomial* p);
term* parse_term(polynomial* p);
void parse_monomial_list(term* tr, polynomial* p);
monomial* parse_monomial(polynomial* p);
int parse_exponent();
char parse_add_operator();
int parse_coefficient();
stmt* parse_start();
void parse_inputs();
void parse_statement_list(stmt* st);
stmt* parse_statement();
poly_eval* parse_poly_evaluation_statement();
int parse_input_statement();
poly_eval* parse_polynomial_evaluation();
void parse_argument_list(poly_eval* pe);
arg* parse_argument(poly_eval* pe);
private:
int errorno;
LexicalAnalyzer lexer;
input_table i_table;
std::vector<polynomial*> p_table;
std::vector<int> error_t;
void checkE1();
void error_code_2();
void error_code_3();
void error_code_4();
void error_code_5();
void syntax_error(int lineno);
int get_polyname(std::string str);
Token expect(TokenType expected_type);
};
#endif