Skip to content

Commit ae50e46

Browse files
committed
New parser implemented
1 parent 855f35d commit ae50e46

43 files changed

Lines changed: 710 additions & 687 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ast/expr.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,6 @@ std::ostream &operator<<(std::ostream &out, const Op &op)
369369
return out;
370370
}
371371

372-
std::ostream &operator<<(std::ostream &out, const ExprList &el)
373-
{
374-
for (Expr e : el) out << e << "\n";
375-
376-
return out;
377-
}
378-
379372
UnaryOp::UnaryOp() : op_(), expr_() {}
380373
UnaryOp::UnaryOp(UnOp op, Expr expr) : op_(op), expr_(expr) {}
381374

@@ -389,7 +382,15 @@ bool UnaryOp::operator==(const UnaryOp &other) const
389382

390383
std::ostream &operator<<(std::ostream &out, const UnaryOp &uop)
391384
{
392-
out << uop.op() << uop.expr();
385+
switch (uop.op()) {
386+
case UnOp::comp:
387+
out << uop.expr() << uop.op();
388+
break;
389+
390+
default:
391+
out << uop.op() << uop.expr();
392+
break;
393+
}
393394

394395
return out;
395396
}
@@ -444,6 +445,14 @@ std::ostream &operator<<(std::ostream &out, const Call &c)
444445
return out;
445446
}
446447

448+
std::ostream &operator<<(std::ostream &out, const ExprList &el)
449+
{
450+
for (Expr e : el)
451+
out << e << ";\n";
452+
453+
return out;
454+
}
455+
447456
} // namespace AST
448457

449458
} // namespace SBG

ast/statement.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ bool IsConfig::operator()(ConfigDims v) const { return true; }
6161

6262
std::ostream &operator<<(std::ostream &out, const StatementList &stml)
6363
{
64-
for (Statement s : stml) out << s << "\n";
64+
for (Statement s : stml)
65+
out << s << ";\n";
6566

6667
return out;
6768
}

parser/expr.cpp

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
171171
, MAPD("mapD:")
172172
{
173173
// Take out "x" as identifier to preserve it for linear expressions
174-
ident = qi::lexeme[qi::char_("a-wy-zA-WY-Z")
174+
ident = qi::lexeme[qi::char_("a-xy-zA-XY-Z")
175175
>> *(qi::alnum | qi::char_('_'))]
176176
| qi::lexeme[qi::char_("x") >> +(qi::alnum | qi::char_('_'))];
177177

@@ -192,7 +192,8 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
192192
rat_primary = rat_legacy[qi::_val = qi::_1]
193193
| (int_expr >> DIV >> int_expr)
194194
[qi::_val = phx::construct<AST::Rational>(qi::_1, qi::_2)]
195-
| int_expr[qi::_val = qi::_1];
195+
| int_expr[qi::_val = qi::_1]
196+
| (OPAREN >> arithmetic_expr>> CPAREN)[qi::_val = qi::_1];
196197

197198
rat_term = rat_primary[qi::_val = qi::_1] >> *(mult_symbol >> rat_primary)
198199
[qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
@@ -238,25 +239,10 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
238239

239240
// ------------ //
240241

241-
lexp_left = (arithmetic_expr >> SLO >> VAR)[qi::_val = qi::_1];
242+
lexp_list = arithmetic_expr % PIPE;
242243

243-
lexp = (lexp_left >> ADD >> arithmetic_expr)
244-
[qi::_val = phx::construct<AST::LinearExp>(qi::_1, qi::_2)]
245-
| (VAR >> ADD >> arithmetic_expr)
246-
[qi::_val = phx::construct<AST::LinearExp>(1, qi::_1)]
247-
| (lexp_left >> SUB >> arithmetic_expr)
248-
[qi::_val = phx::construct<AST::LinearExp>(qi::_1
249-
, phx::construct<AST::UnaryOp>(AST::UnOp::neg, qi::_2))]
250-
| (VAR >> SUB >> arithmetic_expr)
251-
[qi::_val = phx::construct<AST::LinearExp>(1
252-
, phx::construct<AST::UnaryOp>(AST::UnOp::neg, qi::_1))]
253-
| lexp_left[qi::_val = phx::construct<AST::LinearExp>(qi::_1, 0)];
254-
255-
// ------------ //
256-
257-
mdlexp = (lexp % PIPE)[qi::_val = phx::construct<AST::MDLExp>(qi::_1)]
258-
| (OPAREN >> (lexp % PIPE)[qi::_val = phx::construct<AST::MDLExp>(qi::_1)]
259-
>> CPAREN);
244+
mdlexp = (PIPE >> lexp_list >> PIPE)
245+
[qi::_val = phx::construct<AST::MDLExp>(qi::_1)];
260246

261247
// ------------ //
262248

@@ -274,42 +260,32 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
274260
// ------------ //
275261

276262
sbg = (V >> set
277-
>> VMAP >> pwl
278-
>> MAP1 >> pwl
279-
>> MAP2 >> pwl
280-
>> EMAP >> pwl
281-
>> SUBE >> pwl)
282-
[qi::_val = phx::construct<AST::SBG>(
283-
qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6
284-
)]
285-
| (V >> set
286-
>> VMAP >> pwl
287-
>> MAP1 >> pwl
288-
>> MAP2 >> pwl
289-
>> EMAP >> pwl)
290-
[qi::_val = phx::construct<AST::SBG>(
291-
qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, phx::construct<AST::PWLMap>()
292-
)];
263+
>> VMAP >> pwl
264+
>> MAP1 >> pwl
265+
>> MAP2 >> pwl
266+
>> EMAP >> pwl
267+
>> -(SUBE >> pwl))[qi::_val = phx::if_else(qi::_6
268+
, phx::construct<AST::SBG>(qi::_1, qi::_2, qi::_3, qi::_4, qi::_5
269+
, *qi::_6)
270+
, phx::construct<AST::SBG>(qi::_1, qi::_2, qi::_3, qi::_4, qi::_5
271+
, phx::construct<AST::PWLMap>())
272+
)
273+
];
293274

294275
// ------------ //
295276

296277
dsbg = (V >> set
297-
>> VMAP >> pwl
298-
>> MAPB >> pwl
299-
>> MAPD >> pwl
300-
>> EMAP >> pwl
301-
>> SUBE >> pwl)
302-
[qi::_val = phx::construct<AST::DSBG>(
303-
qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, qi::_6
304-
)]
305-
| (V >> set
306-
>> VMAP >> pwl
307-
>> MAPB >> pwl
308-
>> MAPD >> pwl
309-
>> EMAP >> pwl)
310-
[qi::_val = phx::construct<AST::DSBG>(
311-
qi::_1, qi::_2, qi::_3, qi::_4, qi::_5, phx::construct<AST::PWLMap>()
312-
)];
278+
>> VMAP >> pwl
279+
>> MAPB >> pwl
280+
>> MAPD >> pwl
281+
>> EMAP >> pwl
282+
>> -(SUBE >> pwl))[qi::_val = phx::if_else(qi::_6
283+
, phx::construct<AST::DSBG>(qi::_1, qi::_2, qi::_3, qi::_4, qi::_5
284+
, *qi::_6)
285+
, phx::construct<AST::DSBG>(qi::_1, qi::_2, qi::_3, qi::_4, qi::_5
286+
, phx::construct<AST::PWLMap>())
287+
)
288+
];
313289

314290
// ------------ //
315291

@@ -332,31 +308,23 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
332308
| map
333309
| mdlexp
334310
| set
335-
| md_inter;
311+
| md_inter
312+
| arithmetic_expr;
336313

337-
sbg_term = (OPAREN >> sbg_expr >> binary_symbol >> sbg_expr >> CPAREN)
338-
[qi::_val = phx::construct<AST::BinOp>(qi::_1, qi::_2, qi::_3)]
339-
| (sbg_primary >> binary_symbol >> sbg_primary)
340-
[qi::_val = phx::construct<AST::BinOp>(qi::_1, qi::_2, qi::_3)]
341-
| sbg_primary[qi::_val = qi::_1];
314+
sbg_factor = (sbg_primary
315+
| (OPAREN >> sbg_expr >> CPAREN))[qi::_val = qi::_1]
316+
>> -(sbg_unary_symbol[qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_val)]);
342317

343-
sbg_expr = (sbg_unary_symbol >> sbg_term)
344-
[qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_2)]
345-
| sbg_term[qi::_val = qi::_1];
318+
sbg_expr = (sbg_factor >> binary_symbol >> sbg_factor)
319+
[qi::_val = phx::construct<AST::BinOp>(qi::_1, qi::_2, qi::_3)]
320+
| sbg_factor[qi::_val = qi::_1];
346321

347322
// ------------ //
348323

349-
primary = sbg_expr[qi::_val = qi::_1] | arithmetic_expr[qi::_val = qi::_1];
350-
351-
expr = primary[qi::_val = qi::_1] >> -(rel_symbol >> primary)
324+
expr = sbg_expr[qi::_val = qi::_1] >> -(rel_symbol >> sbg_expr)
352325
[qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
353326

354327
expr_list = expr % COMA;
355-
356-
BOOST_SPIRIT_DEBUG_NODE(sbg_expr);
357-
BOOST_SPIRIT_DEBUG_NODE(sbg_term);
358-
BOOST_SPIRIT_DEBUG_NODE(sbg_primary);
359-
BOOST_SPIRIT_DEBUG_NODE(mdlexp);
360328
};
361329

362330
template struct ExprRule<StrIt>;

parser/expr.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ struct ExprRule : qi::grammar<Iterator, Skipper<Iterator>, AST::ExprList()> {
7474
qi::rule<Iterator, Skipper<Iterator>, AST::ExprList()> mdi_list;
7575
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> set;
7676

77-
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> lexp_left;
78-
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> lexp;
79-
77+
qi::rule<Iterator, Skipper<Iterator>, AST::ExprList()> lexp_list;
8078
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> mdlexp;
8179

8280
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> map;
@@ -92,10 +90,10 @@ struct ExprRule : qi::grammar<Iterator, Skipper<Iterator>, AST::ExprList()> {
9290
qi::rule<Iterator, Skipper<Iterator>, AST::Call()> call_expr;
9391

9492
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> sbg_primary;
93+
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> sbg_factor;
9594
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> sbg_term;
9695
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> sbg_expr;
9796

98-
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> primary;
9997
qi::rule<Iterator, Skipper<Iterator>, AST::Expr()> expr;
10098
qi::rule<Iterator, Skipper<Iterator>, AST::ExprList()> expr_list;
10199

parser/main.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,55 @@ void usage()
9999
std::cout << "A brief list of the available expressions:\n";
100100
std::cout << " * Arithmetic.\n";
101101
std::cout << " * Interval.\n";
102-
std::cout << " * Multi-dimensional Interval.\n";
102+
std::cout << " * Multi-dimensional Interval (MDI).\n";
103103
std::cout << " * SBG Set.\n";
104104
std::cout << " * Linear Expression.\n";
105-
std::cout << " * Multi-dimensional Expression.\n";
105+
std::cout << " * Multi-dimensional Linear Expression (MDLE).\n";
106106
std::cout << " * SBG Map.\n";
107107
std::cout << " * SBG Piecewise Linear Map.\n";
108108
std::cout << " * Undirected SBG.\n";
109109
std::cout << " * Directed SBG.\n";
110110
std::cout << " * Function Call.\n\n";
111111

112-
std::cout << "For a more detailed description of the grammar, the /parser\n";
113-
std::cout << "files can be analyzed. Also /test files can be consulted\n";
114-
std::cout << "to start writing basic SBG programs.\n\n";
112+
std::cout << "A brief list of available operators:\n";
113+
std::cout << " * Arithmetic expressions: +, -, *, /.\n";
114+
std::cout << " * SBG expressions:\n";
115+
std::cout << " - For linear expressions and maps: + and -.\n";
116+
std::cout << " - For containers (intervals, MDIs, sets): /\\ \n";
117+
std::cout << " (intersection), \\/ (union), \\ (difference),\n";
118+
std::cout << " \' (complement).\n";
119+
std::cout << " * Relational operators for any expression: <, == (equality).";
120+
std::cout << "\n\n";
121+
122+
std::cout << "As functions definitions are not supported, there is a fixed\n";
123+
std::cout << "list of callable functions:\n";
124+
std::cout << " * isEmpty(expr)\n";
125+
std::cout << " * minElem(expr)\n";
126+
std::cout << " * maxElem(expr)\n";
127+
std::cout << " * compose(expr, expr)\n";
128+
std::cout << " * inv(expr)\n";
129+
std::cout << " * image(expr) and image(expr, expr)\n";
130+
std::cout << " * preImage(expr) and preImage(expr, expr)\n";
131+
std::cout << " * dom(expr)\n";
132+
std::cout << " * combine(expr, expr)\n";
133+
std::cout << " * firstInv(expr)\n";
134+
std::cout << " * minMap(expr, expr)\n";
135+
std::cout << " * reduce(expr)\n";
136+
std::cout << " * minAdj(expr, expr)\n";
137+
std::cout << " * mapInf(expr)\n";
138+
std::cout << " * CC(expr)\n";
139+
std::cout << " * matching(expr, expr)\n";
140+
std::cout << " * scc(expr)\n";
141+
std::cout << " * sort(expr)\n";
142+
std::cout << " * matchSCC(expr, expr)\n";
143+
std::cout << " * matchSCCTS(expr, expr)\n";
144+
std::cout << " * cut(expr)\n";
145+
std::cout << "Then the evaluator will be in charge of analyzing if\n";
146+
std::cout << "arguments have the correct type to call the function\n\n";
147+
148+
std::cout << "For a more detailed description of the grammar, the files\n";
149+
std::cout << "present in /parser may be of use. Examples of such programs\n";
150+
std::cout << "are present in the /test directory.\n\n";
115151
}
116152

117153
void version()

parser/statement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ StmRule<Iterator>::StmRule(Iterator &it) : StmRule::base_type(stms)
4343
cfg_dims = (NMBR_DIMS >> qi::uint_)
4444
[qi::_val = phx::construct<AST::ConfigDims>(qi::_1)];
4545

46-
assign = (expr.ident >> ASSIGN >> expr.arithmetic_expr)
46+
assign = (expr.ident >> ASSIGN >> expr.expr)
4747
[qi::_val = phx::construct<AST::Assign>(qi::_1, qi::_2)];
4848

4949
stm = assign >> expr.SEMI;

test/advection.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ offM12 = r(V0,1) - r(E1,1) + 2;
2929
offM22 = r(V2,1) - r(E1,1);
3030

3131
V: {[V0+1:1:V1] , [V1+1:1:V2] , [V2+1:1:V3]}
32-
Vmap: <<{[V0+1:1:V1]} -> 0*x+1 , {[V1+1:1:V2]} -> 0*x+2 , {[V2+1:1:V3]} -> 0*x+3>>
33-
map1: <<{[E0+1:1:E1]} -> 1*x+offM11 , {[E1+1:1:E2]} -> 1*x+offM12>>
34-
map2: <<{[E0+1:1:E1]} -> 1*x+offM21 , {[E1+1:1:E2]} -> 1*x+offM22>>
35-
Emap: <<{[E0+1:1:E1]} -> 0*x+1 , {[E1+1:1:E2]} -> 0*x+2>>;
32+
Vmap: <<{[V0+1:1:V1]} -> |0*x+1 |, {[V1+1:1:V2]} -> |0*x+2 |, {[V2+1:1:V3]} -> |0*x+3|>>
33+
map1: <<{[E0+1:1:E1]} -> |1*x+offM11 |, {[E1+1:1:E2]} -> |1*x+offM12|>>
34+
map2: <<{[E0+1:1:E1]} -> |1*x+offM21 |, {[E1+1:1:E2]} -> |1*x+offM22|>>
35+
Emap: <<{[E0+1:1:E1]} -> |0*x+1 |, {[E1+1:1:E2]} -> |0*x+2|>>;
3636

3737
matchSCCTS(
3838
V: {[V0+1:1:V1] , [V1+1:1:V2] , [V2+1:1:V3]}
39-
Vmap: <<{[V0+1:1:V1]} -> 0*x+1 , {[V1+1:1:V2]} -> 0*x+2 , {[V2+1:1:V3]} -> 0*x+3>>
40-
map1: <<{[E0+1:1:E1]} -> 1*x+offM11 , {[E1+1:1:E2]} -> 1*x+offM12>>
41-
map2: <<{[E0+1:1:E1]} -> 1*x+offM21 , {[E1+1:1:E2]} -> 1*x+offM22>>
42-
Emap: <<{[E0+1:1:E1]} -> 0*x+1 , {[E1+1:1:E2]} -> 0*x+2>>
39+
Vmap: <<{[V0+1:1:V1]} -> |0*x+1 |, {[V1+1:1:V2]} -> |0*x+2 |, {[V2+1:1:V3]} -> |0*x+3|>>
40+
map1: <<{[E0+1:1:E1]} -> |1*x+offM11 |, {[E1+1:1:E2]} -> |1*x+offM12|>>
41+
map2: <<{[E0+1:1:E1]} -> |1*x+offM21 |, {[E1+1:1:E2]} -> |1*x+offM22|>>
42+
Emap: <<{[E0+1:1:E1]} -> |0*x+1 |, {[E1+1:1:E2]} -> |0*x+2|>>
4343
, 1);

test/arithmetic.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// !!! If this file is modified the corresponding SBG.log file
33
// in all the gt_data directories should be modified.
44

5-
N = r(100, 1)-r(200,1);
5+
N = r(100, 1)-r(200, 1);
66

77
2+3;
88
r(1, 2);
99
2*10+3;
1010
5-3;
1111
r(1, 2)+r(1, 2);
1212
r(100, 1)-r(200,1);
13+
(3+6)-3;

0 commit comments

Comments
 (0)