2323#include < boost/phoenix/core.hpp>
2424#include < boost/phoenix/operator.hpp>
2525#include < boost/phoenix/object.hpp>
26+ #include < boost/spirit/include/qi.hpp>
2627
2728#include " ast/expr.hpp"
2829#include " sbg/rational.hpp"
@@ -100,35 +101,18 @@ struct add_symbols_struct : qi::symbols<char, AST::Op> {
100101} add_symbols;
101102
102103struct mult_symbol_struct : qi::symbols<char , AST::Op> {
103- mult_symbol_struct (){
104+ mult_symbol_struct () {
104105 add (" *" , AST::Op::mult);
105106 }
106107} mult_symbol;
107108
108- struct expo_symbol_struct : qi::symbols<char , AST::Op> {
109- expo_symbol_struct () {
110- add (" ^" , AST::Op::expo);
111- }
112- } expo_symbol;
113-
114109struct unary_symbol_struct : qi::symbols<char , AST::UnOp> {
115110 unary_symbol_struct () {
116- add (" -" , AST::UnOp::oppo);
111+ add (" -" , AST::UnOp::oppo)
112+ (" #" , AST::UnOp::card);
117113 }
118114} unary_symbol;
119115
120- struct mixed_unary_symbol_struct : qi::symbols<char , AST::UnOp> {
121- mixed_unary_symbol_struct () {
122- add (" #" , AST::UnOp::card);
123- }
124- } mixed_unary_symbol;
125-
126- struct sbg_unary_symbol_struct : qi::symbols<char , AST::UnOp> {
127- sbg_unary_symbol_struct () {
128- add (" \' " , AST::UnOp::comp);
129- }
130- } sbg_unary_symbol;
131-
132116struct binary_symbol_struct : qi::symbols<char , AST::Op> {
133117 binary_symbol_struct () {
134118 add (" +" , AST::Op::add)
@@ -166,8 +150,6 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
166150 , OANGLE(" <<" )
167151 , CANGLE(" >>" )
168152 , CARTPROD(" x" )
169- , SLO(" *" )
170- , VAR(" x" )
171153 , ADD(" +" )
172154 , SUB(" -" )
173155 , PIPE(" |" )
@@ -181,58 +163,44 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
181163 , MAPB(" mapB:" )
182164 , MAPD(" mapD:" )
183165{
184- ident = qi::lexeme[qi::char_ (" a-xy-zA-XY-Z" )
185- >> *(qi::alnum | qi::char_ (' _' ))]
186- | qi::lexeme[qi::char_ (" x" ) >> +(qi::alnum | qi::char_ (' _' ))];
166+ identifier = qi::lexeme[qi::char_ (" a-zA-Z" )
167+ >> *(qi::alnum | qi::char_ (' _' ))];
187168
188169 nat = qi::lexeme[qi::ulong_long][qi::_val = phx::construct<LIB::NAT>(qi::_1)];
189170
190- int_expr = nat[qi::_val = qi::_1]
191- | ident[qi::_val = qi::_1];
192-
193- rat_legacy = (RAT
171+ rational_legacy = (RAT
194172 >> OPAREN
195- >> int_expr
196- >> COMA
197- >> int_expr
173+ >> qi::lexeme[qi::int_]
174+ >> COMA
175+ >> qi::lexeme[qi::int_]
198176 >> CPAREN)[qi::_val = phx::construct<AST::Rational>(qi::_1, qi::_2)];
199177
200- rat_primary = rat_legacy[qi::_val = qi::_1]
201- | (int_expr >> DIV >> int_expr)
202- [qi::_val = phx::construct<AST::Rational>(qi::_1, qi::_2)]
203- | int_expr[qi::_val = qi::_1]
204- | (OPAREN >> arithmetic_expr>> CPAREN)
178+ rational = rational_legacy[qi::_val = qi::_1]
179+ | (qi::lexeme[qi::int_] >> DIV >> qi::lexeme[qi::int_])
180+ [qi::_val = phx::construct<AST::Rational>(qi::_1, qi::_2)];
181+
182+ primary = rational[qi::_val = qi::_1]
183+ | nat[qi::_val = qi::_1]
184+ | identifier[qi::_val = qi::_1]
185+ | (OPAREN >> arithmetic_expr >> CPAREN)
205186 [qi::_val = phx::construct<AST::ParenExpr>(qi::_1)];
206187
207- rat_term = rat_primary[qi::_val = qi::_1] >> *(mult_symbol >> rat_primary)
188+ factor = (unary_symbol >> primary)
189+ [qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_2)]
190+ | primary[qi::_val = qi::_1];
191+
192+ term = factor[qi::_val = qi::_1] >> *(mult_symbol >> factor)
208193 [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
209194
210- arithmetic_expr = (rat_term[qi::_val = qi::_1] >> *(add_symbols >> rat_term)
211- [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)])
212- | (unary_symbol >> rat_term)
213- [qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_2)]
214- | (mixed_unary_symbol >> sbg_expr)
215- [qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_2)];
195+ arithmetic_expr = term[qi::_val = qi::_1] >> *(add_symbols >> term)
196+ [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
216197
217198 // ------------ //
218199
219- nat_primary = nat[qi::_val = qi::_1]
220- | ident[qi::_val = qi::_1];
221-
222- nat_factor = nat_primary[qi::_val = qi::_1]
223- >> -(expo_symbol >> nat_primary)
224- [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
225-
226- nat_term = nat_factor[qi::_val = qi::_1] >> *(mult_symbol >> nat_factor)
227- [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
228-
229- nat_expr = nat_term[qi::_val = qi::_1] >> *(add_symbols >> nat_term)
230- [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
231-
232200 interval = (OBRACKET
233- >> nat_expr >> COLON
234- >> ((nat_expr >> COLON) | qi::attr (phx::construct<AST::Expr>(1 )))
235- >> nat_expr >> CBRACKET)
201+ >> arithmetic_expr >> COLON
202+ >> ((arithmetic_expr >> COLON) | qi::attr (phx::construct<AST::Expr>(1 )))
203+ >> arithmetic_expr >> CBRACKET)
236204 [qi::_val = phx::construct<AST::Interval>(qi::_1, qi::_2, qi::_3)];
237205
238206 // ------------ //
@@ -320,23 +288,23 @@ ExprRule<Iterator>::ExprRule(Iterator &it) :
320288 | mdlexp
321289 | set
322290 | md_inter
323- | arithmetic_expr;
291+ | arithmetic_expr
292+ | (OPAREN >> sbg_expr >> CPAREN);
324293
325- sbg_factor = (sbg_primary
326- | (OPAREN >> sbg_expr >> CPAREN)
327- [qi::_val = phx::construct<AST::ParenExpr>(qi::_1)])[qi::_val = qi::_1]
328- >> -(sbg_unary_symbol
329- [qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_val)]);
294+ sbg_factor = (unary_symbol >> sbg_primary)
295+ [qi::_val = phx::construct<AST::UnaryOp>(qi::_1, qi::_2)]
296+ | sbg_primary[qi::_val = qi::_1];
330297
331- sbg_expr = (sbg_factor >> binary_symbol >> sbg_factor)
332- [qi::_val = phx::construct<AST::BinOp>(qi::_1, qi::_2, qi::_3)]
333- | sbg_factor[qi::_val = qi::_1];
298+ sbg_expr = sbg_factor[qi::_val = qi::_1] >> *(binary_symbol >> sbg_factor)
299+ [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
334300
335301 // ------------ //
336302
337- expr = sbg_expr[qi::_val = qi::_1] >> -(rel_symbol >> sbg_expr)
303+ relation = sbg_expr[qi::_val = qi::_1] >> -(rel_symbol >> sbg_expr)
338304 [qi::_val = phx::construct<AST::BinOp>(qi::_val, qi::_1, qi::_2)];
339305
306+ expr = relation;
307+
340308 expr_list = expr % COMA;
341309};
342310
0 commit comments