File: output_parser.h

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (214 lines) | stat: -rw-r--r-- 6,732 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
  
  Authors: Marek Kokot
  
  Version: 3.2.4
  Date   : 2024-02-09
*/

#ifndef _OUTPUT_PARSER_H
#define _OUTPUT_PARSER_H

#include "defs.h"
#include <list>
#include <map>
#include "tokenizer.h"
#include "expression_node.h"


/*****************************************************************************************************************************/
// This parser validate below grammar:
// expr -> term sum_op
// sum_op -> PLUSMINUS term sum_op
// sum_op -> TERMINATOR
// 
// term -> argument term_op
// term_op -> MUL argument term_op
// term_op -> TERMINATOR
// argument -> VARIABLE
// argument -> OPEN_BRACKET expr CLOSE_BRACKET
// This code is based on: https://github.com/mikailsheikh/cogitolearning-examples/tree/master/CogPar
/*****************************************************************************************************************************/

template<unsigned SIZE> class COutputParser
{
	std::list<Token> tokens;
	const std::map<std::string, uint32>& input;
	Token curr_token;
	void nextToken();
	CExpressionNode<SIZE>* argument();
	CExpressionNode<SIZE>* term_op(CExpressionNode<SIZE>* left);
	CExpressionNode<SIZE>* term();
	CExpressionNode<SIZE>* sum_op(CExpressionNode<SIZE>* left);
	CExpressionNode<SIZE>* expr();

	void modifier(COperNode<SIZE>* exp);
public:
	COutputParser(std::list<Token>& tokens, const std::map<std::string, uint32>& input) :
		tokens(tokens), input(input)
	{
		curr_token = tokens.front();
	}

	CExpressionNode<SIZE>* Parse();
};


/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/

template<unsigned SIZE>
CExpressionNode<SIZE>* COutputParser<SIZE>::Parse()
{
	CExpressionNode<SIZE>* res = expr();
	if (curr_token.second != TokenType::TERMINATOR)
	{
		std::cerr << "Error: wrong symbol :" << curr_token.first <<"\n";
		exit(1);
	}
#ifdef ENABLE_DEBUG
	std::cout << "\n";
	res->Display();
#endif
	return res;
}

/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/

/*****************************************************************************************************************************/
template<unsigned SIZE> void COutputParser<SIZE>::nextToken()
{
	tokens.pop_front();
	if (tokens.empty())
		curr_token.second = TokenType::TERMINATOR;
	else
		curr_token = tokens.front();
}

/*****************************************************************************************************************************/
template<unsigned SIZE> CExpressionNode<SIZE>* COutputParser<SIZE>::argument()
{
	if (curr_token.second == TokenType::VARIABLE)
	{
		//check if this variable was defined
		auto elem = input.find(curr_token.first);
		if (elem == input.end())
		{
			std::cerr << "Error: variable " << curr_token.first << " was not defined\n";
			exit(1);
		}
		CExpressionNode<SIZE>* res = new CInputNode<SIZE>(elem->second);
		nextToken();
		return res;
	}
	else if (curr_token.second == TokenType::PARENTHESIS_OPEN)
	{
		nextToken();
		CExpressionNode<SIZE>* res = expr();
		if (curr_token.second != TokenType::PARENTHESIS_CLOSE)
		{
			std::cerr << "Error: close  parenthesis expected, but " << curr_token.first << " found\n";
			exit(1);
		}
		nextToken();
		return res;
	}
	return nullptr;
}

/*****************************************************************************************************************************/
template<unsigned SIZE> CExpressionNode<SIZE>* COutputParser<SIZE>::term_op(CExpressionNode<SIZE>* left)
{
	if (curr_token.second == TokenType::MUL_OPER)
	{
		COperNode<SIZE>* res = new CIntersectionNode<SIZE>;
		res->AddLeftChild(left);
		nextToken();
		modifier(res);
		auto right = argument();
		res->AddRightChild(right);
		return term_op(res);
	}
	return left;
}
template<unsigned SIZE> CExpressionNode<SIZE>* COutputParser<SIZE>::term()
{
	auto left = argument();
	return term_op(left);
}

/*****************************************************************************************************************************/
template<unsigned SIZE> void COutputParser<SIZE>::modifier(COperNode<SIZE>* exp)
{	
	if (curr_token.second == TokenType::DIFF_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::DIFF);
		nextToken();
	}
	else if (curr_token.second == TokenType::LEFT_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::FROM_DB1);
		nextToken();
	}
	else if (curr_token.second == TokenType::MAX_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::MAX);
		nextToken();
	}
	else if (curr_token.second == TokenType::MIN_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::MIN);
		nextToken();
	}
	else if (curr_token.second == TokenType::RIGHT_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::FROM_DB2);
		nextToken();
	}
	else if (curr_token.second == TokenType::SUM_MODIFIER)
	{
		exp->SetCounterOpType(CounterOpType::SUM);
		nextToken();
	}
}
/*****************************************************************************************************************************/
template<unsigned SIZE> CExpressionNode<SIZE>* COutputParser<SIZE>::sum_op(CExpressionNode<SIZE>* left)
{
	if (curr_token.second == TokenType::PLUS_OPER || curr_token.second == TokenType::STRICT_MINUS_OPER || curr_token.second == TokenType::COUNTER_MINUS_OPER)
	{
		COperNode<SIZE>* res = nullptr;
		if (curr_token.second == TokenType::PLUS_OPER)
			res = new CUnionNode<SIZE>;
		else if (curr_token.second == TokenType::STRICT_MINUS_OPER)
			res = new CKmersSubtractionNode<SIZE>;
		else
			res = new CCountersSubtractionNode<SIZE>;
		res->AddLeftChild(left);		
		bool modifier_allowed = !(curr_token.second == TokenType::STRICT_MINUS_OPER);
		nextToken();		
		if(modifier_allowed)
			modifier(res);
		auto right = term();
		res->AddRightChild(right);
		return sum_op(res);
	}
	return left;
}

/*****************************************************************************************************************************/
template<unsigned SIZE> CExpressionNode<SIZE>* COutputParser<SIZE>::expr()
{
	auto left = term();
	return sum_op(left);
}



#endif

// ***** EOF