1 /** @file ginsh_parser.yy
3 * Input grammar definition for ginsh.
4 * This file must be processed with yacc/bison.
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/resource.h>
34 #include <sys/types.h>
43 #include <readline/readline.h>
44 #include <readline/history.h>
51 #include <ginac/ginac.h>
54 // Original readline settings
55 static int orig_completion_append_character;
56 static char *orig_basic_word_break_characters;
58 // Expression stack for ", "" and """
59 static void push(const ex &e);
62 // Start and end time for the time() function
63 static struct rusage start_time, end_time;
65 // Table of functions (a multimap, because one function may appear with different
66 // numbers of parameters)
67 typedef ex (*fcnp)(const exprseq &e);
68 typedef ex (*fcnp2)(const exprseq &e, int serial);
71 fcn_desc() : p(NULL), num_params(0) {}
72 fcn_desc(fcnp func, int num) : p(func), num_params(num), is_ginac(false) {}
73 fcn_desc(fcnp2 func, int num, int ser) : p((fcnp)func), num_params(num), is_ginac(true), serial(ser) {}
75 fcnp p; // Pointer to function
76 int num_params; // Number of parameters (0 = arbitrary)
77 bool is_ginac; // Flag: function is GiNaC function
78 int serial; // GiNaC function serial number (if is_ginac == true)
81 typedef multimap<string, fcn_desc> fcn_tab;
84 static fcn_tab::const_iterator find_function(const ex &sym, int req_params);
86 static ex lst2matrix(const ex &l);
89 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
90 of class numeric or symbol (e.g. a constant or the FAIL object)) */
91 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
92 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
94 %token T_QUIT T_PRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
96 /* Operator precedence and associativity */
99 %left '<' '>' T_LESSEQ T_GREATEREQ
124 } catch (exception &e) {
125 cerr << e.what() << endl;
133 } catch (exception &e) {
134 cerr << e.what() << endl;
138 | T_PRINT '(' exp ')' ';'
142 } catch (exception &e) {
143 cerr << e.what() << endl;
148 | T_XYZZY {cout << "Nothing happens.\n";}
149 | T_INVENTORY {cout << "You're not carrying anything.\n";}
150 | T_LOOK {cout << "You're in a twisty little maze of passages, all alike.\n";}
153 cout << "If you were to quit now, you would score ";
154 cout << (syms.size() > 350 ? 350 : syms.size());
155 cout << " out of a possible 350.\n";
157 | error ';' {yyclearin; yyerrok;}
158 | error ':' {yyclearin; yyerrok;}
161 exp : T_NUMBER {$$ = $1;}
162 | T_SYMBOL {$$ = $1.eval();}
163 | '\'' T_SYMBOL '\'' {$$ = $2;}
164 | T_LITERAL {$$ = $1;}
165 | T_DIGITS {$$ = $1;}
166 | T_QUOTE {$$ = exstack[0];}
167 | T_QUOTE2 {$$ = exstack[1];}
168 | T_QUOTE3 {$$ = exstack[2];}
169 | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')'
171 getrusage(RUSAGE_SELF, &end_time);
172 $$ = (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) +
173 (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) +
174 double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 +
175 double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6;
177 | T_SYMBOL '(' exprseq ')'
179 fcn_tab::const_iterator i = find_function($1, $3.nops());
180 if (i->second.is_ginac) {
181 $$ = ((fcnp2)(i->second.p))(static_cast<const exprseq &>(*($3.bp)), i->second.serial);
183 $$ = (i->second.p)(static_cast<const exprseq &>(*($3.bp)));
186 | T_DIGITS '=' T_NUMBER
187 {$$ = $3; Digits = ex_to_numeric($3).to_int();}
189 {$$ = $3; const_cast<symbol *>(&ex_to_symbol($1))->assign($3);}
190 | exp T_EQUAL exp {$$ = $1 == $3;}
191 | exp T_NOTEQ exp {$$ = $1 != $3;}
192 | exp '<' exp {$$ = $1 < $3;}
193 | exp T_LESSEQ exp {$$ = $1 <= $3;}
194 | exp '>' exp {$$ = $1 > $3;}
195 | exp T_GREATEREQ exp {$$ = $1 >= $3;}
196 | exp '+' exp {$$ = $1 + $3;}
197 | exp '-' exp {$$ = $1 - $3;}
198 | exp '*' exp {$$ = $1 * $3;}
199 | exp '/' exp {$$ = $1 / $3;}
200 | exp '%' exp {$$ = $1 % $3;}
201 | '-' exp %prec NEG {$$ = -$2;}
202 | '+' exp %prec NEG {$$ = $2;}
203 | exp '^' exp {$$ = power($1, $3);}
204 | exp '!' {$$ = factorial($1);}
205 | '(' exp ')' {$$ = $2;}
206 | '[' list_or_empty ']' {$$ = $2;}
207 | T_MATRIX_BEGIN matrix T_MATRIX_END {$$ = lst2matrix($2);}
210 exprseq : exp {$$ = exprseq($1);}
211 | exprseq ',' exp {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
214 list_or_empty: /* empty */ {$$ = *new lst;}
218 list : exp {$$ = lst($1);}
219 | list ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
222 matrix : T_MATRIX_BEGIN row T_MATRIX_END {$$ = lst($2);}
223 | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
226 row : exp {$$ = lst($1);}
227 | row ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
236 // Error print routine
239 cerr << s << " at " << yytext << endl;
243 // Push expression "e" onto the expression stack (for ", "" and """)
244 static void push(const ex &e)
246 exstack[2] = exstack[1];
247 exstack[1] = exstack[0];
256 static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
257 static ex f_denom(const exprseq &e) {return e[0].denom();}
258 static ex f_eval1(const exprseq &e) {return e[0].eval();}
259 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
260 static ex f_expand(const exprseq &e) {return e[0].expand();}
261 static ex f_gcd(const exprseq &e) {return gcd(e[0], e[1]);}
262 static ex f_lcm(const exprseq &e) {return lcm(e[0], e[1]);}
263 static ex f_lsolve(const exprseq &e) {return lsolve(e[0], e[1]);}
264 static ex f_nops(const exprseq &e) {return e[0].nops();}
265 static ex f_normal1(const exprseq &e) {return e[0].normal();}
266 static ex f_numer(const exprseq &e) {return e[0].numer();}
267 static ex f_power(const exprseq &e) {return power(e[0], e[1]);}
268 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
269 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
271 #define CHECK_ARG(num, type, fcn) if (!is_ex_of_type(e[num], type)) throw(std::invalid_argument("argument " #num " to " #fcn " must be a " #type))
273 static ex f_charpoly(const exprseq &e)
275 CHECK_ARG(0, matrix, charpoly);
276 CHECK_ARG(1, symbol, charpoly);
277 return ex_to_matrix(e[0]).charpoly(ex_to_symbol(e[1]));
280 static ex f_coeff(const exprseq &e)
282 CHECK_ARG(1, symbol, coeff);
283 CHECK_ARG(2, numeric, coeff);
284 return e[0].coeff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
287 static ex f_collect(const exprseq &e)
289 CHECK_ARG(1, symbol, collect);
290 return e[0].collect(ex_to_symbol(e[1]));
293 static ex f_content(const exprseq &e)
295 CHECK_ARG(1, symbol, content);
296 return e[0].content(ex_to_symbol(e[1]));
299 static ex f_degree(const exprseq &e)
301 CHECK_ARG(1, symbol, degree);
302 return e[0].degree(ex_to_symbol(e[1]));
305 static ex f_determinant(const exprseq &e)
307 CHECK_ARG(0, matrix, determinant);
308 return ex_to_matrix(e[0]).determinant();
311 static ex f_diag(const exprseq &e)
314 matrix &m = *new matrix(dim, dim);
315 for (int i=0; i<dim; i++)
316 m.set(i, i, e.op(i));
320 static ex f_diff2(const exprseq &e)
322 CHECK_ARG(1, symbol, diff);
323 return e[0].diff(ex_to_symbol(e[1]));
326 static ex f_diff3(const exprseq &e)
328 CHECK_ARG(1, symbol, diff);
329 CHECK_ARG(2, numeric, diff);
330 return e[0].diff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
333 static ex f_divide(const exprseq &e)
336 if (divide(e[0], e[1], q))
342 static ex f_eval2(const exprseq &e)
344 CHECK_ARG(1, numeric, eval);
345 return e[0].eval(ex_to_numeric(e[1]).to_int());
348 static ex f_evalf2(const exprseq &e)
350 CHECK_ARG(1, numeric, evalf);
351 return e[0].evalf(ex_to_numeric(e[1]).to_int());
354 static ex f_has(const exprseq &e)
356 return e[0].has(e[1]) ? exONE() : exZERO();
359 static ex f_inverse(const exprseq &e)
361 CHECK_ARG(0, matrix, inverse);
362 return ex_to_matrix(e[0]).inverse();
365 static ex f_is(const exprseq &e)
367 CHECK_ARG(0, relational, is);
368 return (bool)ex_to_relational(e[0]) ? exONE() : exZERO();
371 static ex f_lcoeff(const exprseq &e)
373 CHECK_ARG(1, symbol, lcoeff);
374 return e[0].lcoeff(ex_to_symbol(e[1]));
377 static ex f_ldegree(const exprseq &e)
379 CHECK_ARG(1, symbol, ldegree);
380 return e[0].ldegree(ex_to_symbol(e[1]));
383 static ex f_normal2(const exprseq &e)
385 CHECK_ARG(1, numeric, normal);
386 return e[0].normal(ex_to_numeric(e[1]).to_int());
389 static ex f_op(const exprseq &e)
391 CHECK_ARG(1, numeric, op);
392 int n = ex_to_numeric(e[1]).to_int();
393 if (n < 0 || n >= e[0].nops())
394 throw(std::out_of_range("second argument to op() is out of range"));
398 static ex f_prem(const exprseq &e)
400 CHECK_ARG(2, symbol, prem);
401 return prem(e[0], e[1], ex_to_symbol(e[2]));
404 static ex f_primpart(const exprseq &e)
406 CHECK_ARG(1, symbol, primpart);
407 return e[0].primpart(ex_to_symbol(e[1]));
410 static ex f_quo(const exprseq &e)
412 CHECK_ARG(2, symbol, quo);
413 return quo(e[0], e[1], ex_to_symbol(e[2]));
416 static ex f_rem(const exprseq &e)
418 CHECK_ARG(2, symbol, rem);
419 return rem(e[0], e[1], ex_to_symbol(e[2]));
422 static ex f_series2(const exprseq &e)
424 CHECK_ARG(1, symbol, series);
425 return e[0].series(ex_to_symbol(e[1]), exZERO());
428 static ex f_series3(const exprseq &e)
430 CHECK_ARG(1, symbol, series);
431 return e[0].series(ex_to_symbol(e[1]), e[2]);
434 static ex f_series4(const exprseq &e)
436 CHECK_ARG(1, symbol, series);
437 CHECK_ARG(3, numeric, series);
438 return e[0].series(ex_to_symbol(e[1]), e[2], ex_to_numeric(e[3]).to_int());
441 static ex f_sqrfree(const exprseq &e)
443 CHECK_ARG(1, symbol, sqrfree);
444 return sqrfree(e[0], ex_to_symbol(e[1]));
447 static ex f_subs3(const exprseq &e)
449 CHECK_ARG(1, lst, subs);
450 CHECK_ARG(2, lst, subs);
451 return e[0].subs(ex_to_lst(e[1]), ex_to_lst(e[2]));
454 static ex f_tcoeff(const exprseq &e)
456 CHECK_ARG(1, symbol, tcoeff);
457 return e[0].tcoeff(ex_to_symbol(e[1]));
460 static ex f_trace(const exprseq &e)
462 CHECK_ARG(0, matrix, trace);
463 return ex_to_matrix(e[0]).trace();
466 static ex f_transpose(const exprseq &e)
468 CHECK_ARG(0, matrix, transpose);
469 return ex_to_matrix(e[0]).transpose();
472 static ex f_unassign(const exprseq &e)
474 CHECK_ARG(0, symbol, unassign);
475 (const_cast<symbol *>(&ex_to_symbol(e[0])))->unassign();
479 static ex f_unit(const exprseq &e)
481 CHECK_ARG(1, symbol, unit);
482 return e[0].unit(ex_to_symbol(e[1]));
485 static ex f_dummy(const exprseq &e)
487 throw(std::logic_error("dummy function called (shouldn't happen)"));
492 * Add all registered GiNaC functions to ginsh
495 static ex f_ginac_function(const exprseq &es, int serial)
497 return function(serial, es).eval(1);
500 void ginsh_get_ginac_functions(void)
502 vector<registered_function_info>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
505 fcns.insert(make_pair(i->name, fcn_desc(f_ginac_function, i->nparams, serial)));
513 * Find a function given a name and number of parameters. Throw exceptions on error.
516 static fcn_tab::const_iterator find_function(const ex &sym, int req_params)
518 const string &name = ex_to_symbol(sym).getname();
519 typedef fcn_tab::const_iterator I;
520 pair<I, I> b = fcns.equal_range(name);
521 if (b.first == b.second)
522 throw(std::logic_error("unknown function '" + name + "'"));
524 for (I i=b.first; i!=b.second; i++)
525 if ((i->second.num_params == 0) || (i->second.num_params == req_params))
528 throw(std::logic_error("invalid number of arguments to " + name + "()"));
533 * Convert list of lists to matrix
536 static ex lst2matrix(const ex &l)
538 if (!is_ex_of_type(l, lst))
539 throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
541 // Find number of rows and columns
542 int rows = l.nops(), cols = 0, i, j;
543 for (i=0; i<rows; i++)
544 if (l.op(i).nops() > cols)
545 cols = l.op(i).nops();
547 // Allocate and fill matrix
548 matrix &m = *new matrix(rows, cols);
549 for (i=0; i<rows; i++)
550 for (j=0; j<cols; j++)
551 if (l.op(i).nops() > j)
552 m.set(i, j, l.op(i).op(j));
554 m.set(i, j, exZERO());
560 * Function name completion functions for readline
563 static char *fcn_generator(char *text, int state)
565 static int len; // Length of word to complete
566 static fcn_tab::const_iterator index; // Iterator to function being currently considered
568 // If this is a new word to complete, initialize now
570 index = fcns.begin();
574 // Return the next function which partially matches
575 while (index != fcns.end()) {
576 const char *fcn_name = index->first.c_str();
578 if (strncmp(fcn_name, text, len) == 0)
579 return strdup(fcn_name);
584 static char **fcn_completion(char *text, int start, int end)
586 if (rl_line_buffer[0] == '!') {
587 // For shell commands, revert back to filename completion
588 rl_completion_append_character = orig_completion_append_character;
589 rl_basic_word_break_characters = orig_basic_word_break_characters;
590 return completion_matches(text, filename_completion_function);
592 // Otherwise, complete function names
593 rl_completion_append_character = '(';
594 rl_basic_word_break_characters = " \t\n\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
595 return completion_matches(text, fcn_generator);
604 int main(int argc, char **argv)
606 // Print banner in interactive mode
608 cout << "ginsh - GiNaC Interactive Shell (" << PACKAGE << " " << VERSION << ")\n";
609 cout << "Copyright (C) 1999 Johannes Gutenberg Universitaet Mainz, Germany\n";
610 cout << "This is free software, and you are welcome to redistribute it\n";
611 cout << "under certain conditions; see the file COPYING for details.\n";
614 // Init table of built-in functions
615 fcns.insert(make_pair(string("beta"), fcn_desc(f_beta, 2)));
616 fcns.insert(make_pair(string("charpoly"), fcn_desc(f_charpoly, 2)));
617 fcns.insert(make_pair(string("coeff"), fcn_desc(f_coeff, 3)));
618 fcns.insert(make_pair(string("collect"), fcn_desc(f_collect, 2)));
619 fcns.insert(make_pair(string("content"), fcn_desc(f_content, 2)));
620 fcns.insert(make_pair(string("degree"), fcn_desc(f_degree, 2)));
621 fcns.insert(make_pair(string("denom"), fcn_desc(f_denom, 1)));
622 fcns.insert(make_pair(string("determinant"), fcn_desc(f_determinant, 1)));
623 fcns.insert(make_pair(string("diag"), fcn_desc(f_diag, 0)));
624 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff2, 2)));
625 fcns.insert(make_pair(string("diff"), fcn_desc(f_diff3, 3)));
626 fcns.insert(make_pair(string("divide"), fcn_desc(f_divide, 2)));
627 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval1, 1)));
628 fcns.insert(make_pair(string("eval"), fcn_desc(f_eval2, 2)));
629 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf1, 1)));
630 fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf2, 2)));
631 fcns.insert(make_pair(string("expand"), fcn_desc(f_expand, 1)));
632 fcns.insert(make_pair(string("gcd"), fcn_desc(f_gcd, 2)));
633 fcns.insert(make_pair(string("has"), fcn_desc(f_has, 2)));
634 fcns.insert(make_pair(string("inverse"), fcn_desc(f_inverse, 1)));
635 fcns.insert(make_pair(string("is"), fcn_desc(f_is, 1)));
636 fcns.insert(make_pair(string("lcm"), fcn_desc(f_lcm, 2)));
637 fcns.insert(make_pair(string("lcoeff"), fcn_desc(f_lcoeff, 2)));
638 fcns.insert(make_pair(string("ldegree"), fcn_desc(f_ldegree, 2)));
639 fcns.insert(make_pair(string("lsolve"), fcn_desc(f_lsolve, 2)));
640 fcns.insert(make_pair(string("nops"), fcn_desc(f_nops, 1)));
641 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal1, 1)));
642 fcns.insert(make_pair(string("normal"), fcn_desc(f_normal2, 2)));
643 fcns.insert(make_pair(string("numer"), fcn_desc(f_numer, 1)));
644 fcns.insert(make_pair(string("op"), fcn_desc(f_op, 2)));
645 fcns.insert(make_pair(string("power"), fcn_desc(f_power, 2)));
646 fcns.insert(make_pair(string("prem"), fcn_desc(f_prem, 3)));
647 fcns.insert(make_pair(string("primpart"), fcn_desc(f_primpart, 2)));
648 fcns.insert(make_pair(string("quo"), fcn_desc(f_quo, 3)));
649 fcns.insert(make_pair(string("rem"), fcn_desc(f_rem, 3)));
650 fcns.insert(make_pair(string("series"), fcn_desc(f_series2, 2)));
651 fcns.insert(make_pair(string("series"), fcn_desc(f_series3, 3)));
652 fcns.insert(make_pair(string("series"), fcn_desc(f_series4, 4)));
653 fcns.insert(make_pair(string("sqrfree"), fcn_desc(f_sqrfree, 2)));
654 fcns.insert(make_pair(string("sqrt"), fcn_desc(f_sqrt, 1)));
655 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs2, 2)));
656 fcns.insert(make_pair(string("subs"), fcn_desc(f_subs3, 3)));
657 fcns.insert(make_pair(string("tcoeff"), fcn_desc(f_tcoeff, 2)));
658 fcns.insert(make_pair(string("time"), fcn_desc(f_dummy, 0)));
659 fcns.insert(make_pair(string("trace"), fcn_desc(f_trace, 1)));
660 fcns.insert(make_pair(string("transpose"), fcn_desc(f_transpose, 1)));
661 fcns.insert(make_pair(string("unassign"), fcn_desc(f_unassign, 1)));
662 fcns.insert(make_pair(string("unit"), fcn_desc(f_unit, 2)));
663 ginsh_get_ginac_functions();
665 // Init readline completer
666 rl_readline_name = argv[0];
667 rl_attempted_completion_function = (CPPFunction *)fcn_completion;
668 orig_completion_append_character = rl_completion_append_character;
669 orig_basic_word_break_characters = rl_basic_word_break_characters;
671 // Init input file list, open first file
672 num_files = argc - 1;
673 file_list = argv + 1;
675 yyin = fopen(*file_list, "r");
677 cerr << "Can't open " << *file_list << endl;
684 // Parse input, catch all remaining exceptions
688 } catch (exception &e) {
689 cerr << e.what() << endl;