]> www.ginac.de Git - ginac.git/blob - ginsh/ginsh_parser.yy
6601a2a04e2c950fdcea3c733ef6aed37f1feb74
[ginac.git] / ginsh / ginsh_parser.yy
1 /*
2  *  ginsh.y - GiNaC Interactive Shell, input grammar definition
3  *
4  *  This file must be processed with yacc/bison
5  */
6
7
8 /*
9  *  Definitions
10  */
11
12 %{
13 #include "config.h"
14
15 #include <sys/resource.h>
16
17 #if HAVE_UNISTD_H
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 extern "C" {
27 #include <readline/readline.h>
28 #include <readline/history.h>
29 }
30
31 #include <map>
32 #include <string>
33 #include <stdexcept>
34
35 #include <ginac/ginac.h>
36 #include "ginsh.h"
37
38 // Original readline settings
39 static int orig_completion_append_character;
40 static char *orig_basic_word_break_characters;
41
42 // Expression stack for ", "" and """
43 static void push(const ex &e);
44 static ex exstack[3];
45
46 // Start and end time for the time() function
47 static struct rusage start_time, end_time;
48
49 // Table of functions (a multimap, because one function may appear with different
50 // numbers of parameters)
51 typedef ex (*fcnp)(const exprseq &e);
52 typedef ex (*fcnp2)(const exprseq &e, int serial);
53
54 struct fcn_desc {
55         fcn_desc() : p(NULL), num_params(0) {}
56         fcn_desc(fcnp func, int num) : p(func), num_params(num), is_ginac(false) {}
57         fcn_desc(fcnp2 func, int num, int ser) : p((fcnp)func), num_params(num), is_ginac(true), serial(ser) {}
58
59         fcnp p;         // Pointer to function
60         int num_params; // Number of parameters (0 = arbitrary)
61         bool is_ginac;  // Flag: function is GiNaC function
62         int serial;     // GiNaC function serial number (if is_ginac == true)
63 };
64
65 typedef multimap<string, fcn_desc> fcn_tab;
66 static fcn_tab fcns;
67
68 static fcn_tab::const_iterator find_function(const ex &sym, int req_params);
69
70 static ex lst2matrix(const ex &l);
71 %}
72
73 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
74    of class numeric or symbol (e.g. a constant or the FAIL object)) */
75 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_QUOTE T_QUOTE2 T_QUOTE3
76 %token T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
77
78 %token T_QUIT T_PRINT T_TIME T_XYZZY T_INVENTORY T_LOOK T_SCORE
79
80 /* Operator precedence and associativity */
81 %right '='
82 %left T_EQUAL T_NOTEQ
83 %left '<' '>' T_LESSEQ T_GREATEREQ
84 %left '+' '-'
85 %left '*' '/' '%'
86 %nonassoc NEG
87 %right '^'
88 %nonassoc '!'
89
90 %start input
91
92
93 /*
94  *  Grammar rules
95  */
96
97 %%
98 input   : /* empty */
99         | input line
100         ;
101
102 line    : ';'
103         | exp ';'
104                 {
105                         try {
106                                 cout << $1 << endl;
107                                 push($1);
108                         } catch (exception &e) {
109                                 cerr << e.what() << endl;
110                                 YYERROR;
111                         }
112                 }
113         | T_PRINT '(' exp ')' ';'
114                 {
115                         try {
116                                 $3.printtree(cout);
117                         } catch (exception &e) {
118                                 cerr << e.what() << endl;
119                                 YYERROR;
120                         }
121                 }
122         | T_QUIT                {YYACCEPT;}
123         | T_XYZZY               {cout << "Nothing happens.\n";}
124         | T_INVENTORY           {cout << "You're not carrying anything.\n";}
125         | T_LOOK                {cout << "You're in a twisty little maze of passages, all alike.\n";}
126         | T_SCORE
127                 {
128                         cout << "If you were to quit now, you would score ";
129                         cout << (syms.size() > 350 ? 350 : syms.size());
130                         cout << " out of a possible 350.\n";
131                 }
132         | error ';'             {yyclearin; yyerrok;}
133         ;
134
135 exp     : T_NUMBER              {$$ = $1;}
136         | T_SYMBOL              {$$ = $1.eval();}
137         | '\'' T_SYMBOL '\''    {$$ = $2;}
138         | T_LITERAL             {$$ = $1;}
139         | T_DIGITS              {$$ = $1;}
140         | T_QUOTE               {$$ = exstack[0];}
141         | T_QUOTE2              {$$ = exstack[1];}
142         | T_QUOTE3              {$$ = exstack[2];}
143         | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')'
144                 {
145                         getrusage(RUSAGE_SELF, &end_time);
146                         $$ = (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) +
147                              (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) +
148                              double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 +
149                              double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6;
150                 }
151         | T_SYMBOL '(' exprseq ')'
152                 {
153                         fcn_tab::const_iterator i = find_function($1, $3.nops());
154                         if (i->second.is_ginac) {
155                                 $$ = ((fcnp2)(i->second.p))(static_cast<const exprseq &>(*($3.bp)), i->second.serial);
156                         } else {
157                                 $$ = (i->second.p)(static_cast<const exprseq &>(*($3.bp)));
158                         }
159                 }
160         | T_DIGITS '=' T_NUMBER
161                 {$$ = $3; Digits = ex_to_numeric($3).to_int();}
162         | T_SYMBOL '=' exp
163                 {$$ = $3; const_cast<symbol *>(&ex_to_symbol($1))->assign($3);}
164         | exp T_EQUAL exp       {$$ = $1 == $3;}
165         | exp T_NOTEQ exp       {$$ = $1 != $3;}
166         | exp '<' exp           {$$ = $1 < $3;}
167         | exp T_LESSEQ exp      {$$ = $1 <= $3;}
168         | exp '>' exp           {$$ = $1 > $3;}
169         | exp T_GREATEREQ exp   {$$ = $1 >= $3;}
170         | exp '+' exp           {$$ = $1 + $3;}
171         | exp '-' exp           {$$ = $1 - $3;}
172         | exp '*' exp           {$$ = $1 * $3;}
173         | exp '/' exp           {$$ = $1 / $3;}
174         | exp '%' exp           {$$ = $1 % $3;}
175         | '-' exp %prec NEG     {$$ = -$2;}
176         | '+' exp %prec NEG     {$$ = $2;}
177         | exp '^' exp           {$$ = power($1, $3);}
178         | exp '!'               {$$ = factorial($1);}
179         | '(' exp ')'           {$$ = $2;}
180         | '[' list_or_empty ']' {$$ = $2;}
181         | T_MATRIX_BEGIN matrix T_MATRIX_END    {$$ = lst2matrix($2);}
182         ;
183
184 exprseq : exp                   {$$ = exprseq($1);}
185         | exprseq ',' exp       {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
186         ;
187
188 list_or_empty: /* empty */      {$$ = *new lst;}
189         | list                  {$$ = $1;}
190         ;
191
192 list    : exp                   {$$ = lst($1);}
193         | list ',' exp          {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
194         ;
195
196 matrix  : T_MATRIX_BEGIN row T_MATRIX_END               {$$ = lst($2);}
197         | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END    {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
198         ;
199
200 row     : exp                   {$$ = lst($1);}
201         | row ',' exp           {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
202         ;
203
204
205 /*
206  *  Routines
207  */
208
209 %%
210 const int GINSH_VERSION = 0;
211 const int GINSH_REVISION = 3;
212
213 // Error print routine
214 int yyerror(char *s)
215 {
216         cerr << s << " at " << yytext << endl;
217         return 0;
218 }
219
220 // Push expression "e" onto the expression stack (for ", "" and """)
221 static void push(const ex &e)
222 {
223         exstack[2] = exstack[1];
224         exstack[1] = exstack[0];
225         exstack[0] = e;
226 }
227
228
229 /*
230  *  Built-in functions
231  */
232
233 static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
234 static ex f_denom(const exprseq &e) {return e[0].denom();}
235 static ex f_eval1(const exprseq &e) {return e[0].eval();}
236 static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
237 static ex f_expand(const exprseq &e) {return e[0].expand();}
238 static ex f_gcd(const exprseq &e) {return gcd(e[0], e[1]);}
239 static ex f_lcm(const exprseq &e) {return lcm(e[0], e[1]);}
240 static ex f_lsolve(const exprseq &e) {return lsolve(e[0], e[1]);}
241 static ex f_nops(const exprseq &e) {return e[0].nops();}
242 static ex f_normal1(const exprseq &e) {return e[0].normal();}
243 static ex f_numer(const exprseq &e) {return e[0].numer();}
244 static ex f_power(const exprseq &e) {return power(e[0], e[1]);}
245 static ex f_sqrt(const exprseq &e) {return sqrt(e[0]);}
246 static ex f_subs2(const exprseq &e) {return e[0].subs(e[1]);}
247
248 #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))
249
250 static ex f_charpoly(const exprseq &e)
251 {
252         CHECK_ARG(0, matrix, charpoly);
253         CHECK_ARG(1, symbol, charpoly);
254         return ex_to_matrix(e[0]).charpoly(ex_to_symbol(e[1]));
255 }
256
257 static ex f_coeff(const exprseq &e)
258 {
259         CHECK_ARG(1, symbol, coeff);
260         CHECK_ARG(2, numeric, coeff);
261         return e[0].coeff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
262 }
263
264 static ex f_collect(const exprseq &e)
265 {
266         CHECK_ARG(1, symbol, collect);
267         return e[0].collect(ex_to_symbol(e[1]));
268 }
269
270 static ex f_content(const exprseq &e)
271 {
272         CHECK_ARG(1, symbol, content);
273         return e[0].content(ex_to_symbol(e[1]));
274 }
275
276 static ex f_degree(const exprseq &e)
277 {
278         CHECK_ARG(1, symbol, degree);
279         return e[0].degree(ex_to_symbol(e[1]));
280 }
281
282 static ex f_determinant(const exprseq &e)
283 {
284         CHECK_ARG(0, matrix, determinant);
285         return ex_to_matrix(e[0]).determinant();
286 }
287
288 static ex f_diag(const exprseq &e)
289 {
290         int dim = e.nops();
291         matrix &m = *new matrix(dim, dim);
292         for (int i=0; i<dim; i++)
293                 m.set(i, i, e.op(i));
294         return m;
295 }
296
297 static ex f_diff2(const exprseq &e)
298 {
299         CHECK_ARG(1, symbol, diff);
300         return e[0].diff(ex_to_symbol(e[1]));
301 }
302
303 static ex f_diff3(const exprseq &e)
304 {
305         CHECK_ARG(1, symbol, diff);
306         CHECK_ARG(2, numeric, diff);
307         return e[0].diff(ex_to_symbol(e[1]), ex_to_numeric(e[2]).to_int());
308 }
309
310 static ex f_divide(const exprseq &e)
311 {
312         ex q;
313         if (divide(e[0], e[1], q))
314                 return q;
315         else
316                 return *new fail();
317 }
318
319 static ex f_eval2(const exprseq &e)
320 {
321         CHECK_ARG(1, numeric, eval);
322         return e[0].eval(ex_to_numeric(e[1]).to_int());
323 }
324
325 static ex f_evalf2(const exprseq &e)
326 {
327         CHECK_ARG(1, numeric, evalf);
328         return e[0].evalf(ex_to_numeric(e[1]).to_int());
329 }
330
331 static ex f_has(const exprseq &e)
332 {
333         return e[0].has(e[1]) ? exONE() : exZERO();
334 }
335
336 static ex f_inverse(const exprseq &e)
337 {
338         CHECK_ARG(0, matrix, inverse);
339         return ex_to_matrix(e[0]).inverse();
340 }
341
342 static ex f_is(const exprseq &e)
343 {
344         CHECK_ARG(0, relational, is);
345         return (bool)ex_to_relational(e[0]) ? exONE() : exZERO();
346 }
347
348 static ex f_lcoeff(const exprseq &e)
349 {
350         CHECK_ARG(1, symbol, lcoeff);
351         return e[0].lcoeff(ex_to_symbol(e[1]));
352 }
353
354 static ex f_ldegree(const exprseq &e)
355 {
356         CHECK_ARG(1, symbol, ldegree);
357         return e[0].ldegree(ex_to_symbol(e[1]));
358 }
359
360 static ex f_normal2(const exprseq &e)
361 {
362         CHECK_ARG(1, numeric, normal);
363         return e[0].normal(ex_to_numeric(e[1]).to_int());
364 }
365
366 static ex f_op(const exprseq &e)
367 {
368         CHECK_ARG(1, numeric, op);
369         int n = ex_to_numeric(e[1]).to_int();
370         if (n < 0 || n >= e[0].nops())
371                 throw(std::out_of_range("second argument to op() is out of range"));
372         return e[0].op(n);
373 }
374
375 static ex f_prem(const exprseq &e)
376 {
377         CHECK_ARG(2, symbol, prem);
378         return prem(e[0], e[1], ex_to_symbol(e[2]));
379 }
380
381 static ex f_primpart(const exprseq &e)
382 {
383         CHECK_ARG(1, symbol, primpart);
384         return e[0].primpart(ex_to_symbol(e[1]));
385 }
386
387 static ex f_quo(const exprseq &e)
388 {
389         CHECK_ARG(2, symbol, quo);
390         return quo(e[0], e[1], ex_to_symbol(e[2]));
391 }
392
393 static ex f_rem(const exprseq &e)
394 {
395         CHECK_ARG(2, symbol, rem);
396         return rem(e[0], e[1], ex_to_symbol(e[2]));
397 }
398
399 static ex f_series2(const exprseq &e)
400 {
401         CHECK_ARG(1, symbol, series);
402         return e[0].series(ex_to_symbol(e[1]), exZERO());
403 }
404
405 static ex f_series3(const exprseq &e)
406 {
407         CHECK_ARG(1, symbol, series);
408         return e[0].series(ex_to_symbol(e[1]), e[2]);
409 }
410
411 static ex f_series4(const exprseq &e)
412 {
413         CHECK_ARG(1, symbol, series);
414         CHECK_ARG(3, numeric, series);
415         return e[0].series(ex_to_symbol(e[1]), e[2], ex_to_numeric(e[3]).to_int());
416 }
417
418 static ex f_sqrfree(const exprseq &e)
419 {
420         CHECK_ARG(1, symbol, sqrfree);
421         return sqrfree(e[0], ex_to_symbol(e[1]));
422 }
423
424 static ex f_subs3(const exprseq &e)
425 {
426         CHECK_ARG(1, lst, subs);
427         CHECK_ARG(2, lst, subs);
428         return e[0].subs(ex_to_lst(e[1]), ex_to_lst(e[2]));
429 }
430
431 static ex f_tcoeff(const exprseq &e)
432 {
433         CHECK_ARG(1, symbol, tcoeff);
434         return e[0].tcoeff(ex_to_symbol(e[1]));
435 }
436
437 static ex f_trace(const exprseq &e)
438 {
439         CHECK_ARG(0, matrix, trace);
440         return ex_to_matrix(e[0]).trace();
441 }
442
443 static ex f_transpose(const exprseq &e)
444 {
445         CHECK_ARG(0, matrix, transpose);
446         return ex_to_matrix(e[0]).transpose();
447 }
448
449 static ex f_unassign(const exprseq &e)
450 {
451         CHECK_ARG(0, symbol, unassign);
452         (const_cast<symbol *>(&ex_to_symbol(e[0])))->unassign();
453         return e[0];
454 }
455
456 static ex f_unit(const exprseq &e)
457 {
458         CHECK_ARG(1, symbol, unit);
459         return e[0].unit(ex_to_symbol(e[1]));
460 }
461
462 static ex f_dummy(const exprseq &e)
463 {
464         throw(std::logic_error("dummy function called (shouldn't happen)"));
465 }
466
467
468 /*
469  *  Add all registered GiNaC functions to ginsh
470  */
471
472 static ex f_ginac_function(const exprseq &es, int serial)
473 {
474         return function(serial, es).eval(1);
475 }
476
477 void ginsh_get_ginac_functions(void)
478 {
479         vector<registered_function_info>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
480         unsigned serial = 0;
481         while (i != end) {
482                 fcns.insert(make_pair(i->name, fcn_desc(f_ginac_function, i->nparams, serial)));
483                 i++;
484                 serial++;
485         }
486 }
487
488
489 /*
490  *  Find a function given a name and number of parameters. Throw exceptions on error.
491  */
492
493 static fcn_tab::const_iterator find_function(const ex &sym, int req_params)
494 {
495         const string &name = ex_to_symbol(sym).getname();
496         typedef fcn_tab::const_iterator I;
497         pair<I, I> b = fcns.equal_range(name);
498         if (b.first == b.second)
499                 throw(std::logic_error("unknown function '" + name + "'"));
500         else {
501                 for (I i=b.first; i!=b.second; i++)
502                         if ((i->second.num_params == 0) || (i->second.num_params == req_params))
503                                 return i;
504         }
505         throw(std::logic_error("invalid number of arguments to " + name + "()"));
506 }
507
508
509 /*
510  *  Convert list of lists to matrix
511  */
512
513 static ex lst2matrix(const ex &l)
514 {
515         if (!is_ex_of_type(l, lst))
516                 throw(std::logic_error("internal error: argument to lst2matrix() is not a list"));
517
518         // Find number of rows and columns
519         int rows = l.nops(), cols = 0, i, j;
520         for (i=0; i<rows; i++)
521                 if (l.op(i).nops() > cols)
522                         cols = l.op(i).nops();
523
524         // Allocate and fill matrix
525         matrix &m = *new matrix(rows, cols);
526         for (i=0; i<rows; i++)
527                 for (j=0; j<cols; j++)
528                         if (l.op(i).nops() > j)
529                                 m.set(i, j, l.op(i).op(j));
530                         else
531                                 m.set(i, j, exZERO());
532         return m;
533 }
534
535
536 /*
537  *  Function name completion functions for readline
538  */
539
540 static char *fcn_generator(char *text, int state)
541 {
542         static int len;                         // Length of word to complete
543         static fcn_tab::const_iterator index;   // Iterator to function being currently considered
544
545         // If this is a new word to complete, initialize now
546         if (state == 0) {
547                 index = fcns.begin();
548                 len = strlen(text);
549         }
550
551         // Return the next function which partially matches
552         while (index != fcns.end()) {
553                 const char *fcn_name = index->first.c_str();
554                 index++;
555                 if (strncmp(fcn_name, text, len) == 0)
556                         return strdup(fcn_name);
557         }
558         return NULL;
559 }
560
561 static char **fcn_completion(char *text, int start, int end)
562 {
563         if (rl_line_buffer[0] == '!') {
564                 // For shell commands, revert back to filename completion
565                 rl_completion_append_character = orig_completion_append_character;
566                 rl_basic_word_break_characters = orig_basic_word_break_characters;
567                 return completion_matches(text, filename_completion_function);
568         } else {
569                 // Otherwise, complete function names
570                 rl_completion_append_character = '(';
571                 rl_basic_word_break_characters = " \t\n\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
572                 return completion_matches(text, fcn_generator);
573         }
574 }
575
576
577 /*
578  *  Main program
579  */
580
581 int main(int argc, char **argv)
582 {
583         // Print banner in interactive mode
584         if (isatty(0)) {
585                 cout << "ginsh - GiNaC Interactive Shell V" << GINSH_VERSION << "." << GINSH_REVISION << endl;
586                 cout << "Copyright (C) 1999 Johannes Gutenberg Universitaet Mainz, Germany\n";
587                 cout << "This is free software, and you are welcome to redistribute it\n";
588                 cout << "under certain conditions; see the file COPYING for details.\n"; 
589         }
590
591         // Init table of built-in functions
592         fcns.insert(make_pair(string("beta"), fcn_desc(f_beta, 2)));
593         fcns.insert(make_pair(string("charpoly"), fcn_desc(f_charpoly, 2)));
594         fcns.insert(make_pair(string("coeff"), fcn_desc(f_coeff, 3)));
595         fcns.insert(make_pair(string("collect"), fcn_desc(f_collect, 2)));
596         fcns.insert(make_pair(string("content"), fcn_desc(f_content, 2)));
597         fcns.insert(make_pair(string("degree"), fcn_desc(f_degree, 2)));
598         fcns.insert(make_pair(string("denom"), fcn_desc(f_denom, 1)));
599         fcns.insert(make_pair(string("determinant"), fcn_desc(f_determinant, 1)));
600         fcns.insert(make_pair(string("diag"), fcn_desc(f_diag, 0)));
601         fcns.insert(make_pair(string("diff"), fcn_desc(f_diff2, 2)));
602         fcns.insert(make_pair(string("diff"), fcn_desc(f_diff3, 3)));
603         fcns.insert(make_pair(string("divide"), fcn_desc(f_divide, 2)));
604         fcns.insert(make_pair(string("eval"), fcn_desc(f_eval1, 1)));
605         fcns.insert(make_pair(string("eval"), fcn_desc(f_eval2, 2)));
606         fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf1, 1)));
607         fcns.insert(make_pair(string("evalf"), fcn_desc(f_evalf2, 2)));
608         fcns.insert(make_pair(string("expand"), fcn_desc(f_expand, 1)));
609         fcns.insert(make_pair(string("gcd"), fcn_desc(f_gcd, 2)));
610         fcns.insert(make_pair(string("has"), fcn_desc(f_has, 2)));
611         fcns.insert(make_pair(string("inverse"), fcn_desc(f_inverse, 1)));
612         fcns.insert(make_pair(string("is"), fcn_desc(f_is, 1)));
613         fcns.insert(make_pair(string("lcm"), fcn_desc(f_lcm, 2)));
614         fcns.insert(make_pair(string("lcoeff"), fcn_desc(f_lcoeff, 2)));
615         fcns.insert(make_pair(string("ldegree"), fcn_desc(f_ldegree, 2)));
616         fcns.insert(make_pair(string("lsolve"), fcn_desc(f_lsolve, 2)));
617         fcns.insert(make_pair(string("nops"), fcn_desc(f_nops, 1)));
618         fcns.insert(make_pair(string("normal"), fcn_desc(f_normal1, 1)));
619         fcns.insert(make_pair(string("normal"), fcn_desc(f_normal2, 2)));
620         fcns.insert(make_pair(string("numer"), fcn_desc(f_numer, 1)));
621         fcns.insert(make_pair(string("op"), fcn_desc(f_op, 2)));
622         fcns.insert(make_pair(string("power"), fcn_desc(f_power, 2)));
623         fcns.insert(make_pair(string("prem"), fcn_desc(f_prem, 3)));
624         fcns.insert(make_pair(string("primpart"), fcn_desc(f_primpart, 2)));
625         fcns.insert(make_pair(string("quo"), fcn_desc(f_quo, 3)));
626         fcns.insert(make_pair(string("rem"), fcn_desc(f_rem, 3)));
627         fcns.insert(make_pair(string("series"), fcn_desc(f_series2, 2)));
628         fcns.insert(make_pair(string("series"), fcn_desc(f_series3, 3)));
629         fcns.insert(make_pair(string("series"), fcn_desc(f_series4, 4)));
630         fcns.insert(make_pair(string("sqrfree"), fcn_desc(f_sqrfree, 2)));
631         fcns.insert(make_pair(string("sqrt"), fcn_desc(f_sqrt, 1)));
632         fcns.insert(make_pair(string("subs"), fcn_desc(f_subs2, 2)));
633         fcns.insert(make_pair(string("subs"), fcn_desc(f_subs3, 3)));
634         fcns.insert(make_pair(string("tcoeff"), fcn_desc(f_tcoeff, 2)));
635         fcns.insert(make_pair(string("time"), fcn_desc(f_dummy, 0)));
636         fcns.insert(make_pair(string("trace"), fcn_desc(f_trace, 1)));
637         fcns.insert(make_pair(string("transpose"), fcn_desc(f_transpose, 1)));
638         fcns.insert(make_pair(string("unassign"), fcn_desc(f_unassign, 1)));
639         fcns.insert(make_pair(string("unit"), fcn_desc(f_unit, 2)));
640         ginsh_get_ginac_functions();
641
642         // Init readline completer
643         rl_readline_name = argv[0];
644         rl_attempted_completion_function = (CPPFunction *)fcn_completion;
645         orig_completion_append_character = rl_completion_append_character;
646         orig_basic_word_break_characters = rl_basic_word_break_characters;
647
648         // Parse input, catch all remaining exceptions
649         int result;
650 again:  try {
651                 result = yyparse();
652         } catch (exception &e) {
653                 cerr << e.what() << endl;
654                 goto again;
655         }
656         return result;
657 }