1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 /* shared private header for all 'genr' related modules */
21 
22 #include "libgretl.h"
23 #include "uservar.h"
24 #include "gretl_func.h"
25 #include "gretl_bundle.h"
26 #include "gretl_array.h"
27 
28 #define GENDEBUG 0
29 
30 /* operators, types, punctuation */
31 
32 enum {
33               U_NEG = 1,
34               U_POS,
35               U_NOT,
36               U_ADDR,
37               U_MAX,      /* SEPARATOR: end of unary operators */
38               B_ASN,
39               B_ADD,
40               B_SUB,
41               B_MUL,
42   /* 10 */    B_DIV,
43               B_MOD,
44               B_POW,
45               B_EQ,
46               B_LT,
47               B_GT,
48               B_LTE,
49               B_GTE,
50               B_NEQ,
51               B_AND,
52   /* 20 */    B_OR,
53               B_TRMUL,
54 	      B_RANGE,
55               B_DOTMULT,
56 	      B_DOTDIV,
57 	      B_DOTPOW,
58               B_DOTADD,
59               B_DOTSUB,
60               B_DOTEQ,
61               B_DOTLT,
62   /* 30 */    B_DOTGT,
63 	      B_DOTLTE,
64 	      B_DOTGTE,
65 	      B_DOTNEQ,
66               B_DOTASN,
67               B_KRON,     /* Kronecker product */
68               B_HCAT,     /* horizontal concatenation */
69               B_VCAT,     /* vertical concatenation */
70 	      B_LCAT,     /* list concatentation */
71 	      B_LDIV,     /* matrix left division */
72   /* 40 */    B_ELLIP,    /* list-generating ellipsis */
73 	      B_JOIN,     /* list-joining with separator */
74 	      OP_MAX,     /* SEPARATOR: end of binary operators */
75               G_LPR,      /* grouping: left paren */
76 	      G_RPR,      /* right paren */
77               G_LBR,      /* left bracket */
78               G_RBR,      /* right bracket */
79               G_LCB,      /* left curly bracket */
80 	      G_RCB,      /* right curly bracket */
81 	      P_COM,	  /* punctuation: comma */
82   /* 50 */    P_DOT,	  /* period */
83 	      P_SEMI,	  /* semi-colon */
84 	      P_COL,	  /* colon */
85               PUNCT_MAX,  /* SEPARATOR: end of grouping and punctuation marks */
86 	      NUM,        /* scalar */
87 	      SERIES,	  /* series */
88 	      LIST,       /* list of series */
89 	      MAT,	  /* matrix */
90 	      BUNDLE,     /* gretl bundle (hash table) */
91 	      ARRAY,      /* generic array object */
92   /* 60 */    STR,	  /* string */
93 	      CNUM,	  /* constant (literal) numeric value */
94 	      CSTR,       /* constant (literal) string */
95 	      CON,	  /* named numeric constant */
96 	      DUM,	  /* dummy variable */
97 	      UOBJ,	  /* user-defined object (e.g. model) */
98 	      NUM_P,      /* user scalar++ */
99 	      NUM_M,      /* user scalar-- */
100 	      OBS,	  /* observation from a series */
101               MSL,	  /* matrix plus subspec */
102   /* 70 */    DMSTR,	  /* "dollar" matrix plus string subspec */
103 	      SLRAW,	  /* unevaluated "slice" specification */
104 	      MSPEC,	  /* evaluated matrix subspec */
105 	      SUBSL,	  /* row or column component of MSPEC */
106 	      MDEF,	  /* explicit matrix definition {...} */
107               LAG,        /* variable plus lag length */
108 	      DVAR,	  /* $ "dataset" variable (mostly scalar or series) */
109 	      MVAR,	  /* $ model var (scalar, series, or matrix) */
110 	      LISTVAR,    /* variable in list, dot syntax */
111 	      DBUNDLE,    /* $ bundle accessor */
112   /* 80 */    BMEMB,      /* member of bundle */
113 	      DBMEMB,     /* member of $ bundle */
114 	      MMEMB,      /* member of named model */
115 	      FARGS,	  /* set of n function arguments */
116               WLIST,      /* wildcard list spec */
117               EMPTY,      /* "null" or empty arg slot */
118 	      UNDEF,      /* undefined (allowed in "query" context only) */
119 	      DTYPE_MAX,  /* SEPARATOR: end of "bare" types */
120 	      UFUN,	  /* user-defined function */
121 	      RFUN,       /* GNU R function */
122   /* 90 */    IVEC,       /* array of ints, not a varlist */
123 	      OSL,        /* "slice" of object other than matrix */
124 	      USERIES,    /* named series (defined only for error reporting) */
125 	      SUB_ADDR,   /* "address" of (e.g.) array element */
126               INC,        /* increment */
127               DEC,        /* decrement */
128 	      QUERY,      /* ternary "?" expression */
129 	      PTR,        /* miscellaneous pointer */
130 	      EOT,	  /* end of transmission */
131 	      UNK
132 };
133 
134 /* functions: don't collide with the enumeration above */
135 
136 enum {
137     F1_MIN = 1 << 8,
138     F_ABS,
139     F_SGN,
140     F_CEIL,
141     F_FLOOR,
142     F_SIN,
143     F_COS,
144     F_TAN,
145     F_ASIN,
146     F_ACOS,
147     F_ATAN,
148     F_SINH,
149     F_COSH,
150     F_TANH,
151     F_ASINH,
152     F_ACOSH,
153     F_ATANH,
154     F_LOG,
155     F_LOG10,
156     F_LOG2,
157     F_EXP,
158     F_SQRT,
159     F_GAMMA,
160     F_LNGAMMA,
161     F_DIGAMMA,
162     F_TRIGAMMA,
163     F_INVMILLS,
164     F_ROUND,
165     F_CNORM,
166     F_DNORM,
167     F_QNORM,
168     F_CARG,
169     F_CMOD,
170     F_REAL,
171     F_IMAG,
172     F_LOGISTIC,
173     FP_MAX,      /* separator: end of pointerized functions */
174     F_CONJ,
175     F_TOINT,
176     F_DIFF,	  /* first difference */
177     F_LDIFF,	  /* log difference */
178     F_SDIFF,	  /* seasonal difference */
179     F_SORT,	  /* ascending sort */
180     F_DSORT,	  /* descending sort */
181     F_RANKING,
182     F_ODEV,	  /* orthogonal deviation */
183     F_NOBS,
184     F_CUM,
185     F_MISSING,
186     F_DATAOK,
187     F_MISSZERO,
188     F_ZEROMISS,
189     F_MEDIAN,
190     F_GINI,
191     F_SUMALL,
192     F_MIN,
193     F_MAX,
194     F_SKEWNESS,
195     F_KURTOSIS,
196     F_SST,
197     F_SUMR,
198     F_SUMC,
199     F_PRODR,
200     F_PRODC,
201     F_MEANR,
202     F_MEANC,
203     F_CHOL,
204     F_INV,
205     F_INVPD,
206     F_GINV,
207     F_DIAG,
208     F_TRANSP,
209     F_VEC,
210     F_VECH,
211     F_UNVECH,
212     F_ROWS,
213     F_COLS,
214     F_DET,
215     F_LDET,
216     F_TRACE,
217     F_NORM1,
218     F_INFNORM,
219     F_RCOND,
220     F_RANK,
221     F_OBSNUM,
222     F_ISDISCR,
223     F_ISDUMMY,
224     F_TYPEOF,
225     F_EXISTS,
226     F_NELEM,
227     F_PDF,
228     F_PVAL,
229     F_CDF,
230     F_INVCDF,
231     F_CRIT,
232     F_URCPVAL,
233     F_RANDGEN,
234     F_MRANDGEN,
235     F_RANDGEN1,
236     F_VALUES,
237     F_UNIQ,
238     F_NULLSPC,
239     F_MEXP,
240     F_MINC,
241     F_MAXC,
242     F_MINR,
243     F_MAXR,
244     F_IMINC,
245     F_IMAXC,
246     F_IMINR,
247     F_IMAXR,
248     F_FFT,
249     F_FFT2,
250     F_FFTI,
251     F_UPPER,
252     F_LOWER,
253     F_POLROOTS,
254     F_OBSLABEL,
255     F_BACKTICK,
256     F_STRLEN,
257     F_VARNAME,
258     F_VARNAMES,
259     F_VARNUM,
260     F_TOLOWER,
261     F_TOUPPER,
262     F_IRR,
263     F_ERRMSG,
264     F_GETENV,
265     F_NGETENV,
266     F_PEXPAND,
267     F_FREQ,
268     F_ISNAN,
269     F_TYPESTR,
270     F_STRSTRIP,
271     F_REMOVE,
272     F_ATOF,
273     F_MPI_RECV,
274     F_EASTER,
275     F_CURL,
276     F_NLINES,
277     F_ARRAY,
278     F_TRAMOLIN,
279     F_CNUMBER,
280     F_ECDF,
281     F_SLEEP,
282     F_GETINFO,
283     F_CDUMIFY,
284     F_GETKEYS,
285     F_MCORR,
286     F_ISCMPLX,
287     F_CTRANS,
288     F_MLOG,
289     F_BARRIER,
290     F_LPSOLVE,
291     HF_JBTERMS,
292     HF_SFCGI,
293     F1_MAX,	  /* SEPARATOR: end of single-arg functions */
294     HF_LISTINFO,
295     F_SUM,
296     F_MEAN,
297     F_VCE,
298     F_SD,
299     F_ARGNAME,
300     F_T1,
301     F_T2,
302     F_COV,
303     F_SDC,
304     F_CDEMEAN,
305     F_MCOV,
306     F_DUMIFY,
307     F_SORTBY,
308     F_RUNIFORM,
309     F_RNORMAL,
310     F_FRACDIFF,
311     F_BOXCOX,
312     F_ZEROS,
313     F_ONES,
314     F_MUNIF,
315     F_MNORM,
316     F_QFORM,
317     F_QR,
318     F_EIGSYM,
319     F_QUANTILE,
320     F_CMULT,	  /* complex multiplication */
321     F_HDPROD,     /* horizontal direct product */
322     F_CDIV,	  /* complex division */
323     F_MXTAB,
324     F_MRSEL,
325     F_MCSEL,
326     F_STRSTR,
327     F_INSTRING,
328     F_CNAMESET,
329     F_RNAMESET,
330     F_LJUNGBOX,
331     F_MSORTBY,
332     F_LINCOMB,
333     F_IMHOF,
334     F_XMIN,
335     F_XMAX,
336     F_FRACLAG,
337     F_MREV,
338     F_DESEAS,
339     F_PERGM,
340     F_NPV,
341     F_DSUM,
342     F_POLYFIT,
343     F_INLIST,
344     F_ISCONST,
345     F_INBUNDLE,
346     F_CNAMEGET,
347     F_RNAMEGET,
348     F_PNOBS,
349     F_PMIN,
350     F_PMAX,
351     F_PSUM,
352     F_PMEAN,
353     F_PXSUM,
354     F_PXNOBS,
355     F_PSD,
356     F_PSHRINK,
357     F_RANDINT,
358     F_MREAD,
359     F_BREAD,
360     F_GETLINE,
361     F_ISODATE,
362     F_JULDATE,
363     F_READFILE,
364     F_PRINTF,
365     F_SPRINTF,
366     F_MPI_SEND,
367     F_BCAST,
368     F_ALLREDUCE,
369     F_GENSERIES,
370     F_KPSSCRIT,
371     F_STRINGIFY,
372     F_SQUARE,
373     F_SEASONALS,
374     F_DROPCOLL,
375     F_KSIMDATA,
376     F_HFDIFF,
377     F_HFLDIFF,
378     F_NAALEN,
379     F_KMEIER,
380     F_NORMTEST,
381     F_COR,
382     F_LRCOVAR,
383     F_JSONGETB,
384     F_FIXNAME,
385     F_ATAN2,
386     F_CCODE,
387     F_LSOLVE,
388     F_STRFTIME,
389     F_STRPTIME,
390     F_CONV2D,
391     F_FLATTEN,
392     F_IMAT,
393     F_COMPLEX,
394     F_RANDPERM,
395     F_STDIZE,
396     F_CSWITCH,
397     F_PSDROOT,
398     F_INSTRINGS,
399     F_STRVALS,
400     F_FUNCERR, /* legacy */
401     F_ERRORIF,
402     F_BINCOEFF,
403     F_ASSERT,
404     F_CONTAINS,
405     F2_MAX,	  /* SEPARATOR: end of two-arg functions */
406     F_WMEAN,
407     F_WVAR,
408     F_WSD,
409     F_LLAG,
410     F_HFLAG,
411     F_PRINCOMP,
412     F_BFGSMAX,
413     F_MSHAPE,
414     F_SVD,
415     F_TRIMR,
416     F_TOEPSOLV,
417     F_CORRGM,
418     F_SEQ,
419     F_REPLACE,
420     F_STRNCMP,
421     F_BESSEL,
422     F_WEEKDAY,
423     F_MONTHLEN,
424     F_EPOCHDAY,
425     F_KDENSITY,
426     F_SETNOTE,
427     F_BWFILT,
428     F_VARSIMUL,
429     F_STRSUB,
430     F_REGSUB,
431     F_MLAG,
432     F_EIGSOLVE,
433     F_SIMANN,
434     F_HALTON,
435     F_MWRITE,
436     F_BWRITE,
437     F_AGGRBY,
438     F_IWISHART,
439     F_SSCANF,
440     F_SUBSTR,
441     F_REDUCE,
442     F_SCATTER,
443     F_MWEIGHTS,
444     F_MGRADIENT,
445     F_MLINCOMB,
446     F_HFLIST,
447     F_NMMAX,
448     F_GSSMAX,
449     F_NPCORR,
450     F_DAYSPAN,
451     F_SMPLSPAN,
452     F_FDJAC,
453     F_NUMHESS,
454     F_STRSPLIT,
455     F_HPFILT,
456     F_XMLGET,
457     F_JSONGET,
458     F_FEVD,
459     F_LRVAR,
460     F_BRENAME,
461     F_ISOWEEK,
462     F_BKW,
463     F_FZERO,
464     F_EIGGEN,
465     F_EIGEN,
466     F_SCHUR,
467     F_RESAMPLE,
468     F_STACK,
469     F_GEOPLOT,
470     F_VMA,
471     F_FCSTATS,
472     F_BCHECK,
473     F_MSPLITBY,
474     HF_REGLS,
475     F3_MAX,       /* SEPARATOR: end of three-arg functions */
476     F_BKFILT,
477     F_MOLS,
478     F_MPOLS,
479     F_MRLS,
480     F_FILTER,
481     F_MCOVG,
482     F_KFILTER,
483     F_KSMOOTH,
484     F_KDSMOOTH,
485     F_KSIMUL,
486     F_NRMAX,
487     F_LOESS,
488     F_GHK,
489     F_QUADTAB,
490     F_ISOCONV,
491     F_QLRPVAL,
492     F_BOOTCI,
493     F_BOOTPVAL,
494     F_MOVAVG,
495     F_DEFARRAY,
496     F_DEFBUNDLE,
497     F_DEFLIST,
498     F_DEFARGS,
499     F_KSETUP,
500     F_BFGSCMAX,
501     F_SVM,
502     F_IRF,
503     F_NADARWAT,
504     F_FEVAL,
505     F_CHOWLIN,
506     F_TDISAGG,
507     F_HYP2F1,
508     F_MIDASMULT,
509     HF_CLOGFI,
510     FN_MAX,	  /* SEPARATOR: end of n-arg functions */
511 };
512 
513 enum {
514     CONST_PI = 1,
515     CONST_NA,
516     CONST_INF,
517     CONST_NAN,
518     CONST_WIN32,
519     CONST_EPS,
520     CONST_HAVE_MPI,
521     CONST_MPI_RANK,
522     CONST_MPI_SIZE,
523     CONST_N_PROC,
524     CONST_TRUE,
525     CONST_FALSE,
526     CONST_SYSINFO
527 };
528 
529 enum {
530     DUM_NULL = 1,
531     DUM_DIAG,
532     DUM_UPPER,
533     DUM_LOWER,
534     DUM_REAL,
535     DUM_IMAG,
536     DUM_END,
537     DUM_DATASET,
538     DUM_TREND
539 };
540 
541 #define GENSTRLEN 128
542 #define NO_VNUM -1
543 
544 #define unary_op(s)  (s >= 1 && s < U_MAX)
545 #define binary_op(s) (s > U_MAX && s < OP_MAX)
546 #define bool_comp(s) (s >= B_EQ && s <= B_OR)
547 #define dot_op(s)    (s >= B_DOTMULT && s <= B_DOTNEQ)
548 
549 #define func1_symb(s) (s > F1_MIN && s < F1_MAX)
550 #define func2_symb(s) (s > F1_MAX && s < F2_MAX)
551 #define func3_symb(s) (s > F2_MAX && s < F3_MAX)
552 #define funcn_symb(s) (s > F3_MAX && s < FN_MAX)
553 
554 #define bnsym(s) (s == MDEF || s == FARGS)
555 
556 #define alias_reversed(n) (n->flags & ALS_NODE)
557 
558 /* function with single string argument */
559 #define string_arg_func(s) (s == F_ISDISCR || s == F_OBSNUM || \
560 			    s == F_BACKTICK || s == F_VARNUM || \
561 			    s == F_EXISTS || s == F_REMOVE || \
562 			    s == F_ISCMPLX)
563 
564 /* function with multiple args, string for first arg */
565 #define str0_func(s) (s == F_PVAL || s == F_CDF || s == F_INVCDF || \
566 		      s == F_CRIT || s == F_RANDGEN || s == F_PDF || \
567 		      s == F_BESSEL || s == F_MRANDGEN || s == F_RANDGEN1)
568 
569 /* functions taking a string arg in last position */
570 #define string_last_func(s) (s == F_DESEAS || s == F_AGGRBY || \
571 			     s == F_PRINTF || s == F_SPRINTF || \
572 			     s == F_ALLREDUCE || s == F_NORMTEST || \
573 			     s == F_SSCANF || s == F_NPCORR || \
574 			     s == F_INBUNDLE || s == F_GENSERIES)
575 
576 /* functions taking string arg in middle position */
577 #define string_mid_func(s) (s == F_REDUCE || s == F_SCATTER)
578 
579 /* functions taking one or more "fncall" (string) arguments */
580 #define fncall_func(s) (s == F_BFGSMAX || s == F_NRMAX || \
581 			s == F_FDJAC || s == F_SIMANN || \
582 			s == F_BFGSCMAX || s == F_NMMAX || \
583 			s == F_GSSMAX || s == F_NUMHESS || \
584 			s == F_FZERO)
585 
586 /* functions with "reversing" aliases */
587 #define als_func(s) (s == F_BFGSMAX || s == F_NRMAX || \
588 		     s == F_SIMANN || s == F_BFGSCMAX || \
589 		     s == F_NMMAX || s == F_GSSMAX || \
590 		     s == F_EXISTS)
591 
592 /* functions where the right-hand argument is actually a return
593    location */
594 #define r_return(s) (s == F_QR || s == F_EIGSYM || s == F_EIGEN || \
595                      s == F_MOLS || s == F_MPOLS || s == F_SVD || \
596 		     s == F_EIGGEN)
597 
598 /* functions where the middle argument is actually a return
599    location */
600 #define m_return(s) (s == F_SVD || s == F_EIGEN)
601 
602 #define reusable(p) (p->flags & (P_COMPILE | P_EXEC))
603 
604 typedef struct node NODE;
605 
606 struct branchn {
607     int n_nodes;
608     NODE **n;
609 };
610 
611 union val {
612     struct branchn bn;
613     int idnum;
614     char *str;
615     double xval;
616     double *xvec;
617     int *ivec;
618     gretl_matrix *m;
619     matrix_subspec *mspec;
620     gretl_bundle *b;
621     gretl_array *a;
622     void *ptr;
623 };
624 
625 enum node_flags {
626     AUX_NODE = 1 << 0, /* auxiliary: free on exit */
627     TMP_NODE = 1 << 1, /* temporary: free content on exit */
628     SVL_NODE = 1 << 2, /* holds string-valued series */
629     PRX_NODE = 1 << 3, /* aux node is proxy (don't reuse!) */
630     LHT_NODE = 1 << 4, /* node holds terminal of LHS */
631     MUT_NODE = 1 << 5, /* node is inherently mutable in type */
632     ALS_NODE = 1 << 6  /* function subject to "reversing" alias */
633 };
634 
635 struct node {
636     gint16 t;        /* type identifier */
637     guint8 flags;    /* AUX_NODE etc., see above */
638     int vnum;        /* associated series ID number */
639     char *vname;     /* associated variable name */
640     user_var *uv;    /* associated named variable */
641     union val v;     /* value (of whatever type) */
642     NODE *L, *M, *R; /* up to three child nodes */
643     NODE *aux;       /* auxiliary (result) node */
644     int refcount;    /* reference counter, used by aux nodes */
645 };
646 
647 enum parser_flags {
648     P_DISCARD = 1 <<  0, /* compute and print, don't save */
649     P_START   = 1 <<  1, /* first round of evaluation */
650     P_AUTOREG = 1 <<  2, /* expression is autoregressive */
651     P_DECL    = 1 <<  3, /* statement is actually a declaration */
652     P_PRIV    = 1 <<  4, /* generating a "private" or internal var */
653     P_COMPILE = 1 <<  5, /* compiling the parse tree */
654     P_EXEC    = 1 <<  6, /* evaluating a compiled tree */
655     P_NATEST  = 1 <<  7, /* testing for NAs in expression */
656     P_UFRET   = 1 <<  8, /* returning value generated by user function */
657     P_QUIET   = 1 <<  9, /* don't print any messages or labels */
658     P_GETSTR  = 1 << 10, /* state: flag acceptance of plain strings */
659     P_SLAVE   = 1 << 11, /* running as "slave" of NLS/MLE/GMM */
660     P_MMASK   = 1 << 12, /* genr result is masked matrix */
661     P_SLICING = 1 << 13, /* state: calculating object slice (temporary) */
662     P_LAGPRSE = 1 << 14, /* state: parsing lag spec (temporary) */
663     P_DELTAN  = 1 << 15, /* flag for change in series length */
664     P_CATCH   = 1 << 16, /* "catch" is in force */
665     P_NODECL  = 1 << 17, /* type of result was not specified */
666     P_LISTDEF = 1 << 18, /* expression defines a list */
667     P_ANON    = 1 << 19, /* generating an anonymous object */
668     P_VOID    = 1 << 20, /* function call, no assignment */
669     P_NOEXEC  = 1 << 21, /* just compile, don't evaluate */
670     P_MSAVE   = 1 << 22, /* trying for reuse of an aux matrix */
671     P_OBSVAL  = 1 << 23, /* generating value of observation in series */
672     P_ALIASED = 1 << 24, /* state: handling aliased object (temporary) */
673     P_AND     = 1 << 25, /* state: working on right-hand term of B_AND */
674     P_STACK   = 1 << 26, /* executing stack() */
675     P_ALTINP  = 1 << 27, /* the input string has been substituted */
676     P_OBJQRY  = 1 << 28, /* querying the existence of an object */
677     P_STRVEC  = 1 << 29  /* "complex" calc with string-valued series */
678 };
679 
680 struct lhinfo {
681     int t;                 /* type of pre-existing LHS variable, if any */
682     char name[VNAMELEN];   /* name of LHS variable */
683     char *label;           /* descriptive string for series */
684     int vnum;              /* ID number of pre-existing LHS series */
685     user_var *uv;          /* address of pre-existing LHS variable */
686     char *expr;            /* expression on left */
687     GretlType gtype;       /* gretl type of LHS array, if any, or
688 			      of LHS bundle member */
689     gretl_matrix *mret;    /* matrix output (possibly under bundle or array) */
690 };
691 
692 typedef struct parser_ parser;
693 
694 struct parser_ {
695     const char *input; /* complete input string */
696     const char *point; /* remaining unprocessed input */
697     const char *rhs;   /* for use in labelling */
698     DATASET *dset;     /* convenience pointer to dataset */
699     PRN *prn;          /* for printing messages */
700     PRN *errprn;       /* for storing error message in case @prn is NULL */
701     int flags;         /* various attributes (see @parser_flags above) */
702     int targ;          /* target type */
703     int op;            /* assignment operator (possibly inflected) */
704     struct lhinfo lh;  /* left-hand side info */
705     NODE *lhtree;      /* LHS syntax tree, if needed */
706     NODE *lhres;       /* result of eval() on @lhtree */
707     NODE *tree;        /* RHS syntax tree */
708     NODE *ret;         /* result of eval() on @tree */
709     /* below: parser state variables */
710     NODE *aux;         /* convenience pointer to current auxiliary node */
711     int callcount;
712     int dset_n;
713     int obs;
714     int sym;
715     int upsym;
716     int ch;
717     double xval;
718     int idnum;
719     char *idstr;
720     void *data;
721     int err;
722 };
723 
724 int parser_getc (parser *p);
725 void parser_ungetc (parser *p);
726 void parser_advance (parser *p, int n);
727 int parser_char_index (parser *p, int c);
728 int parser_print_input (parser *p);
729 void free_tree (NODE *t, parser *p, int code);
730 void lex (parser *s);
731 NODE *new_node (int t);
732 NODE *expr (parser *s);
733 NODE *newdbl (double x);
734 NODE *newempty (void);
735 NODE *newb2 (int t, NODE *l, NODE *r);
736 NODE *obs_node (parser *p);
737 const char *getsymb (int t);
738 const char *getsymb_full (int t, const parser *p);
739 void set_parsing_query (int s);
740 void set_doing_genseries (int s);
741 
742 int parser_ensure_error_buffer (parser *p);
743 void context_error (int c, parser *p, const char *func);
744 void undefined_symbol_error (const char *s, parser *p);
745 
746 int realgen (const char *s, parser *p, DATASET *dset,
747 	     PRN *prn, int flags, int targtype);
748 void gen_save_or_print (parser *p, PRN *prn);
749 void gen_cleanup (parser *p);
750 void parser_free_aux_nodes (parser *p);
751 
752 /* name lookup functions */
753 const char *constname (int c);
754 const char *dvarname (int t);
755 const char *mvarname (int t);
756 const char *bvarname (int t);
757 const char *dumname (int t);
758 int is_gretl_accessor (const char *s);
759 int mvar_lookup (const char *s);
760 
761 int install_function_override (const char *funname,
762 			       const char *pkgname,
763 			       gpointer data);
764 int delete_function_override (const char *funname,
765 			      const char *pkgname);
766 
767 /* handling declarations of variables */
768 int check_declarations (char ***pS, parser *p);
769 
770 /* in genfuncs.c, used only internally */
771 int cross_sectional_stat (double *x, const int *list,
772 			  const DATASET *dset,
773 			  int f, int partial_ok);
774 int x_sectional_weighted_stat (double *x, const int *list,
775 			       const int *wlist,
776 			       const DATASET *dset,
777 			       int f, int partial_ok);
778 
779 /* in geneval.c, used only internally */
780 double dvar_get_scalar (int i, const DATASET *dset);
781 int *node_get_list (NODE *n, parser *p);
782 
783 /* in genmain.c, used only internally */
784 int stack_update_parser_input (parser *p);
785 
786 /* in genlex.c, used only internally */
787 void *get_genr_function_pointer (int f);
788 
789 /* helper functions for manual, gretl.lang file */
790 int gen_func_count (void);
791 const char *gen_func_name (int i);
792 int model_var_count (void);
793 const char *model_var_name (int i);
794 int data_var_count (void);
795 const char *data_var_name (int i);
796 int bundle_var_count (void);
797 const char *bundle_var_name (int i);
798 int gretl_const_count (void);
799 const char *gretl_const_name (int i);
800