-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
131 lines (111 loc) · 3.05 KB
/
Copy pathparser.cpp
File metadata and controls
131 lines (111 loc) · 3.05 KB
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 "interpreter.h"
#include <iostream>
Parser::Parser(const char *filename, Token *start_token)
{
lexer = new Lexer(filename);
current_token = start_token;
};
void Parser::error()
{
printf("Error parsing input\n");
exit(1);
};
void Parser::validate(int token_type)
{
if (current_token->token_type != token_type)
{
error();
}
};
// factor : (PLUS | MINUS) factor | INTEGER | LPAREN expr RPAREN
ASTNode *Parser::factor()
{
current_token = lexer->get_next_token();
ASTNode *binaryNode = nullptr;
if (current_token->token_type == PLUS)
{
validate(PLUS);
ASTNode *node = new ASTNode(PLUS, current_token->value);
node->right = factor();
node->left = new ASTNode(INTEGER, 0);
return node;
}
if (current_token->token_type == MINUS)
{
validate(MINUS);
ASTNode *node = new ASTNode(MINUS, current_token->value);
node->right = factor();
node->left = new ASTNode(INTEGER, 0);
return node;
}
if (current_token->token_type == LPAREN)
{
validate(LPAREN);
binaryNode = expr();
// this->current_token = get_next_token();
}
if (current_token->token_type == RPAREN)
{
validate(RPAREN);
return binaryNode;
}
validate(INTEGER);
return new ASTNode(INTEGER, current_token->value);
}
// term : factor ((MULTIPLY | DIVIDE) factor)*
ASTNode *Parser::term()
{
ASTNode *left = factor();
ASTNode *binaryNode = nullptr;
// int val = this->current_token->value;
current_token = lexer->get_next_token();
while (current_token->token_type == MULTIPLY || current_token->token_type == DIVIDE)
{
if (current_token->token_type == MULTIPLY)
{
validate(MULTIPLY);
ASTNode *right = factor();
binaryNode = new ASTNode(MULTIPLY, '*', left, right);
}
else if (current_token->token_type == DIVIDE)
{
validate(DIVIDE);
ASTNode *right = factor();
binaryNode = new ASTNode(DIVIDE, '/', left, right);
}
left = binaryNode;
current_token = lexer->get_next_token();
}
return left;
}
// expr : term ((PLUS | MINUS) term)*
ASTNode *Parser::expr()
{
ASTNode *left = term();
ASTNode *binaryNode = nullptr;
while (current_token->token_type == PLUS || current_token->token_type == MINUS)
{
if (current_token->token_type == PLUS)
{
validate(PLUS);
ASTNode *right = term();
binaryNode = new ASTNode(PLUS, '+', left, right);
}
else if (current_token->token_type == MINUS)
{
validate(MINUS);
ASTNode *right = term();
binaryNode = new ASTNode(MINUS, '-', left, right);
}
left = binaryNode;
}
return left;
}
ASTNode *Parser::parse()
{
while (current_token->token_type != END_OF_FILE)
{
current_token = lexer->get_next_token();
std::cout << current_token->token_to_string() << std::endl;
}
}