DynareFlex.ll 12.7 KB
Newer Older
1
%{
2
3
using namespace std;

4
5
6
7
8
9
10
11
12
13
14
15
16
/* The prototype for yylex() is defined in ParsingDriver.hh */

#include "ParsingDriver.hh"
#include "DynareBison.hh"

/* Shortcut to access tokens defined by Bison */
typedef yy::parser::token token;

/* By default yylex returns int, we use token_type.
   Unfortunately yyterminate by default returns 0, which is
   not of token_type.  */
#define yyterminate() return yy::parser::token_type (0);

17
18
19
20
21
22
23
int comment_caller;
/* Particular value : when sigma_e command is found 
 this flag is set to 1, when command finished it is set to 0
 */
int sigma_e = 0;
%} 

24
%option case-insensitive noyywrap nounput batch debug never-interactive yylineno
25

26
27
28
29
30
%x COMMENT
%x DYNARE_STATEMENT
%x DYNARE_BLOCK
%x NATIVE

31
32
33
34
%{
// Increments location counter for every token read
#define YY_USER_ACTION yylloc->columns(yyleng);
%}
35
%%
36
37
38
39
40
%{
  // Reset location before reading token
  yylloc->step();
%}

41
 /* spaces, tabs and EOL are ignored */
42
43
<*>[ \t\r\f]+  { yylloc->step(); }
<*>[\n]+       { yylloc->lines(yyleng); yylloc->step(); }
44
45

 /* Comments */
46
47
48
<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK>["%"].*
<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK>["/"]["/"].* 
<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK>"/*"   {comment_caller = YY_START; BEGIN COMMENT;}
49
50

<COMMENT>[^*\n]* 		
51
<COMMENT>"*"+[^/\n]
52
53
54
<COMMENT>"*"+"/"        {BEGIN comment_caller;}

 /* Begin of a Dynare statement */
55
56
57
58
59
<INITIAL>var {BEGIN DYNARE_STATEMENT; return token::VAR;}
<INITIAL>varexo {BEGIN DYNARE_STATEMENT; return token::VAREXO;}
<INITIAL>varexo_det {BEGIN DYNARE_STATEMENT; return token::VAREXO_DET;}
<INITIAL>parameters {BEGIN DYNARE_STATEMENT; return token::PARAMETERS;}
<INITIAL>periods 	{BEGIN DYNARE_STATEMENT; return token::PERIODS;}
sebastien's avatar
sebastien committed
60
<INITIAL>cutoff 	{BEGIN DYNARE_STATEMENT; return token::CUTOFF;}
61
<INITIAL>estimation {BEGIN DYNARE_STATEMENT; return token::ESTIMATION;}
michel's avatar
michel committed
62
63
<INITIAL>prior_analysis {BEGIN DYNARE_STATEMENT; return token::PRIOR_ANALYSIS;}
<INITIAL>posterior_analysis {BEGIN DYNARE_STATEMENT; return token::POSTERIOR_ANALYSIS;}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<INITIAL>varobs 	{BEGIN DYNARE_STATEMENT; return token::VAROBS;}
<INITIAL>unit_root_vars	{BEGIN DYNARE_STATEMENT; return token::UNIT_ROOT_VARS;}
<INITIAL>rplot	 	{BEGIN DYNARE_STATEMENT; return token::RPLOT;}
<INITIAL>osr_params 	{BEGIN DYNARE_STATEMENT; return token::OSR_PARAMS;}
<INITIAL>osr	 	{BEGIN DYNARE_STATEMENT; return token::OSR;}
<INITIAL>dynatype	{BEGIN DYNARE_STATEMENT; return token::DYNATYPE;}
<INITIAL>dynasave 	{BEGIN DYNARE_STATEMENT; return token::DYNASAVE;}
<INITIAL>olr	 	{BEGIN DYNARE_STATEMENT; return token::OLR;}
<INITIAL>olr_inst	 	{BEGIN DYNARE_STATEMENT; return token::OLR_INST;}
<INITIAL>model_comparison 	{BEGIN DYNARE_STATEMENT; return token::MODEL_COMPARISON;}

<INITIAL>steady {BEGIN DYNARE_STATEMENT; return token::STEADY;}
<INITIAL>check {BEGIN DYNARE_STATEMENT; return token::CHECK;}
<INITIAL>simul {BEGIN DYNARE_STATEMENT; return token::SIMUL;}
<INITIAL>stoch_simul {BEGIN DYNARE_STATEMENT; return token::STOCH_SIMUL;}
<INITIAL>dsample {BEGIN DYNARE_STATEMENT; return token::DSAMPLE;}
<INITIAL>Sigma_e {BEGIN DYNARE_STATEMENT; sigma_e = 1; return token::SIGMA_E;}
sebastien's avatar
sebastien committed
81
<INITIAL>calib {BEGIN DYNARE_STATEMENT; return token::CALIB;}
sebastien's avatar
sebastien committed
82
<INITIAL>planner_objective {BEGIN DYNARE_STATEMENT; return token::PLANNER_OBJECTIVE;}
michel's avatar
michel committed
83
<INITIAL>ramsey_policy {BEGIN DYNARE_STATEMENT; return token::RAMSEY_POLICY;}
84
85

 /* End of a Dynare statement */
86
87
88
89
90
<DYNARE_STATEMENT>; {
  if (!sigma_e)
    BEGIN INITIAL;
  return yy::parser::token_type (yytext[0]);
}
91
92
93


 /* Begin of a Dynare block */
94
95
96
97
98
99
<INITIAL>model {BEGIN DYNARE_BLOCK; return token::MODEL;}
<INITIAL>initval {BEGIN DYNARE_BLOCK; return token::INITVAL;}
<INITIAL>endval {BEGIN DYNARE_BLOCK; return token::ENDVAL;}
<INITIAL>histval {BEGIN DYNARE_BLOCK; return token::HISTVAL;}
<INITIAL>shocks {BEGIN DYNARE_BLOCK; return token::SHOCKS;}
<INITIAL>estimated_params {BEGIN DYNARE_BLOCK; return token::ESTIMATED_PARAMS;}
michel's avatar
michel committed
100
101
 /* priors is an alias for estimated_params */
<INITIAL>priors {BEGIN DYNARE_BLOCK;return token::ESTIMATED_PARAMS;}
102
103
104
105
<INITIAL>estimated_params_init 		{BEGIN DYNARE_BLOCK; return token::ESTIMATED_PARAMS_INIT;}
<INITIAL>estimated_params_bounds 	{BEGIN DYNARE_BLOCK; return token::ESTIMATED_PARAMS_BOUNDS;}
<INITIAL>observation_trends {BEGIN DYNARE_BLOCK; return token::OBSERVATION_TRENDS;}
<INITIAL>optim_weights {BEGIN DYNARE_BLOCK; return token::OPTIM_WEIGHTS;}
sebastien's avatar
sebastien committed
106
<INITIAL>calib_var 	{BEGIN DYNARE_BLOCK; return token::CALIB_VAR;}
107
108

 /* End of a Dynare block */
109
<DYNARE_BLOCK>end[ \t\n]*; 	{BEGIN INITIAL; return token::END;}   
110
111

 /* Inside  of a Dynare statement */
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<DYNARE_STATEMENT>datafile 		{return token::DATAFILE;}
<DYNARE_STATEMENT>nobs 			{return token::NOBS;}
<DYNARE_STATEMENT>first_obs 		{return token::FIRST_OBS;}
<DYNARE_STATEMENT>prefilter 		{return token::PREFILTER;} 
<DYNARE_STATEMENT>presample 		{return token::PRESAMPLE;} 
<DYNARE_STATEMENT>lik_algo  		{return token::LIK_ALGO;}  
<DYNARE_STATEMENT>lik_init  		{return token::LIK_INIT;}  
<DYNARE_STATEMENT>graph   		{return token::GRAPH;}  	  
<DYNARE_STATEMENT>nograph   		{return token::NOGRAPH;}  	  
<DYNARE_STATEMENT>print   		{return token::PRINT;}  	  
<DYNARE_STATEMENT>noprint   		{return token::NOPRINT;}  	  
<DYNARE_STATEMENT>conf_sig  		{return token::CONF_SIG;}  
<DYNARE_STATEMENT>mh_replic 		{return token::MH_REPLIC;} 
<DYNARE_STATEMENT>mh_drop   		{return token::MH_DROP;}   
<DYNARE_STATEMENT>mh_jscale   		{return token::MH_JSCALE;}   
<DYNARE_STATEMENT>mh_init_scale 	{return token::MH_INIT_SCALE;}
<DYNARE_STATEMENT>mode_file 		{return token::MODE_FILE;}
<DYNARE_STATEMENT>mode_compute 	{return token::MODE_COMPUTE;}
<DYNARE_STATEMENT>mode_check 		{return token::MODE_CHECK;}
<DYNARE_STATEMENT>prior_trunc 	{return token::PRIOR_TRUNC;}
<DYNARE_STATEMENT>mh_mode 		{return token::MH_MODE;}
<DYNARE_STATEMENT>mh_nblocks 		{return token::MH_NBLOCKS;}
<DYNARE_STATEMENT>load_mh_file 	{return token::LOAD_MH_FILE;}
<DYNARE_STATEMENT>loglinear 		{return token::LOGLINEAR;}
<DYNARE_STATEMENT>nodiagnostic 	{return token::NODIAGNOSTIC;}
<DYNARE_STATEMENT>kalman_algo 	{return token::KALMAN_ALGO;}
<DYNARE_STATEMENT>kalman_tol 	{return token::KALMAN_TOL;}
<DYNARE_STATEMENT>forecast 	{return token::FORECAST;}
<DYNARE_STATEMENT>smoother 	{return token::SMOOTHER;}
<DYNARE_STATEMENT>bayesian_irf 	{return token::BAYESIAN_IRF;}
<DYNARE_STATEMENT>moments_varendo {return token::MOMENTS_VARENDO;}
<DYNARE_STATEMENT>filtered_vars	{return token::FILTERED_VARS;}
<DYNARE_STATEMENT>filter_step_ahead	{return token::FILTER_STEP_AHEAD;}
<DYNARE_STATEMENT>relative_irf 	{return token::RELATIVE_IRF;}
<DYNARE_STATEMENT>tex		{return token::TEX;}
<DYNARE_STATEMENT>nomoments	{return token::NOMOMENTS;}
<DYNARE_STATEMENT>corr		{return token::CORR;}
<DYNARE_STATEMENT>nocorr	{return token::NOCORR;}
<DYNARE_STATEMENT>optim		{return token::OPTIM;}
<DYNARE_STATEMENT>periods	{return token::PERIODS;}
sebastien's avatar
sebastien committed
152
<DYNARE_STATEMENT>cutoff	{return token::CUTOFF;}
153
154
155
156
157
<DYNARE_STATEMENT>model_comparison_approximation {return token::MODEL_COMPARISON;}
<DYNARE_STATEMENT>laplace {return token::LAPLACE;}
<DYNARE_STATEMENT>modifiedharmonicmean {return token::MODIFIEDHARMONICMEAN;}
<DYNARE_STATEMENT>constant	{return token::CONSTANT;}
<DYNARE_STATEMENT>noconstant	{return token::NOCONSTANT;}
sebastien's avatar
sebastien committed
158
<DYNARE_STATEMENT>covar {return token::COVAR;}
159
160

<DYNARE_STATEMENT>[\$][^$]*[\$] {
161
  strtok(yytext+1, "$");
162
  yylval->string_val = new string(yytext + 1); 
163
164
  return token::TEX_NAME;
}
165
166

 /* Inside a Dynare block */
167
168
169
170
171
<DYNARE_BLOCK>var {return token::VAR;}
<DYNARE_BLOCK>stderr {return token::STDERR;}
<DYNARE_BLOCK>values {return token::VALUES;}
<DYNARE_BLOCK>corr {return token::CORR;}
<DYNARE_BLOCK>periods {return token::PERIODS;}
sebastien's avatar
sebastien committed
172
<DYNARE_BLOCK>filename {return token::FILENAME;}
173
174
175
176
177
178
179
<DYNARE_BLOCK>gamma_pdf {return token::GAMMA_PDF;}
<DYNARE_BLOCK>beta_pdf {return token::BETA_PDF;}
<DYNARE_BLOCK>normal_pdf {return token::NORMAL_PDF;}
<DYNARE_BLOCK>inv_gamma_pdf {return token::INV_GAMMA_PDF;}
<DYNARE_BLOCK>inv_gamma1_pdf {return token::INV_GAMMA_PDF;}
<DYNARE_BLOCK>inv_gamma2_pdf {return token::INV_GAMMA_PDF;}
<DYNARE_BLOCK>uniform_pdf {return token::UNIFORM_PDF;}
180

181
<DYNARE_BLOCK>; {return yy::parser::token_type (yytext[0]);}
182
<DYNARE_BLOCK># {return yy::parser::token_type (yytext[0]);}
183

sebastien's avatar
sebastien committed
184
<DYNARE_BLOCK>autocorr {return token::AUTOCORR;}
185
186

 /* Inside Dynare statement */
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<DYNARE_STATEMENT>solve_algo {return token::SOLVE_ALGO;}
<DYNARE_STATEMENT>dr_algo {return token::DR_ALGO;}
<DYNARE_STATEMENT>simul_algo {return token::SIMUL_ALGO;}
<DYNARE_STATEMENT>drop {return token::DROP;}
<DYNARE_STATEMENT>order {return token::ORDER;}
<DYNARE_STATEMENT>replic {return token::REPLIC;}
<DYNARE_STATEMENT>ar {return token::AR;}
<DYNARE_STATEMENT>nofunctions {return token::NOFUNCTIONS;}
<DYNARE_STATEMENT>irf {return token::IRF;}
<DYNARE_STATEMENT>hp_filter {return token::HP_FILTER;}
<DYNARE_STATEMENT>hp_ngrid {return token::HP_NGRID;}
<DYNARE_STATEMENT>simul_seed {return token::SIMUL_SEED;}
<DYNARE_STATEMENT>qz_criterium {return token::QZ_CRITERIUM;}
<DYNARE_STATEMENT>simul {return token::SIMUL;}
<DYNARE_STATEMENT>olr_beta {return token::OLR_BETA;}
<DYNARE_STATEMENT>xls_sheet {return token::XLS_SHEET;}
<DYNARE_STATEMENT>xls_range {return token::XLS_RANGE;}
michel's avatar
michel committed
204
<DYNARE_STATEMENT>mh_recover {return token::MH_RECOVER;}
michel's avatar
michel committed
205
<DYNARE_STATEMENT>planner_discount {return token::PLANNER_DISCOUNT;}
michel's avatar
michel committed
206
207


208
209
210
<DYNARE_STATEMENT>[\.] {return yy::parser::token_type (yytext[0]);}
<DYNARE_STATEMENT>[\\] {return yy::parser::token_type (yytext[0]);}
<DYNARE_STATEMENT>[\'] {return yy::parser::token_type (yytext[0]);}
211
212

<DYNARE_STATEMENT,DYNARE_BLOCK>use_dll {return token::USE_DLL;}
sebastien's avatar
sebastien committed
213
214
215
<DYNARE_STATEMENT,DYNARE_BLOCK>sparse_dll {return token::SPARSE_DLL;}
<DYNARE_STATEMENT,DYNARE_BLOCK>gcc_compiler {return token::GCC_COMPILER;}
<DYNARE_STATEMENT,DYNARE_BLOCK>lcc_compiler {return token::LCC_COMPILER;}
216
<DYNARE_STATEMENT,DYNARE_BLOCK>linear {return token::LINEAR;}
217
<DYNARE_STATEMENT,DYNARE_BLOCK>[,] {return token::COMMA;}
218
<DYNARE_STATEMENT,DYNARE_BLOCK>[:] {return yy::parser::token_type (yytext[0]);}
219
220
221
222
223
224
225
<DYNARE_STATEMENT,DYNARE_BLOCK>[\(\)] {return yy::parser::token_type (yytext[0]);} 
<DYNARE_STATEMENT,DYNARE_BLOCK>[\[] {return yy::parser::token_type (yytext[0]);}
<DYNARE_STATEMENT,DYNARE_BLOCK>[\]] {
  if (sigma_e)
    sigma_e = 0;
  return yy::parser::token_type (yytext[0]);
}
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<DYNARE_STATEMENT,DYNARE_BLOCK>[+] {return token::PLUS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[-] {return token::MINUS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[*] {return token::TIMES;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[/] {return token::DIVIDE;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[=] {return token::EQUAL;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[\^] {return token::POWER;}
<DYNARE_STATEMENT,DYNARE_BLOCK>exp {return token::EXP;}
<DYNARE_STATEMENT,DYNARE_BLOCK>log {return token::LOG;}
<DYNARE_STATEMENT,DYNARE_BLOCK>log10 {return token::LOG10;}
<DYNARE_STATEMENT,DYNARE_BLOCK>ln {return token::LOG;}
<DYNARE_STATEMENT,DYNARE_BLOCK>sin {return token::SIN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>cos {return token::COS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>tan {return token::TAN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>asin {return token::ASIN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>acos {return token::ACOS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>atan {return token::ATAN;}
<DYNARE_STATEMENT,DYNARE_BLOCK>sinh {return token::SINH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>cosh {return token::COSH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>tanh {return token::TANH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>asinh {return token::ASINH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>acosh {return token::ACOSH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>atanh {return token::ATANH;}
<DYNARE_STATEMENT,DYNARE_BLOCK>sqrt  {return token::SQRT;}
249
250

<DYNARE_STATEMENT,DYNARE_BLOCK>[A-Za-z_][A-Za-z0-9_]* {
251
  yylval->string_val = new string(yytext);
252
253
  return token::NAME;
}
254
255

<DYNARE_STATEMENT,DYNARE_BLOCK>((([0-9]*\.[0-9]+)|([0-9]+\.))([edED][-+]?[0-9]+)?)|([0-9]+[edED][-+]?[0-9]+) {
256
  yylval->string_val = new string(yytext);
257
258
  return token::FLOAT_NUMBER;
}
259
260

<DYNARE_STATEMENT,DYNARE_BLOCK>[0-9]+ {
261
  yylval->string_val = new string(yytext);
262
263
  return token::INT_NUMBER;
}
264
   	
sebastien's avatar
sebastien committed
265
266
 /* an instruction starting with a recognized symbol (which is not a modfile local variable)
    is passed as NAME,
267
268
269
    otherwise it is a native statement until the end of the line
 */
<INITIAL>[A-Za-z_][A-Za-z0-9_]* {	      
sebastien's avatar
sebastien committed
270
  if (driver.symbol_exists_and_is_not_modfile_local_variable(yytext))
271
272
    {
      BEGIN DYNARE_STATEMENT;
273
      yylval->string_val = new string(yytext);
274
275
276
277
      return token::NAME;
    }
  else
    {
sebastien's avatar
sebastien committed
278
      /* Enter a native block */
279
      BEGIN NATIVE;
sebastien's avatar
sebastien committed
280
      yyless(0);
281
282
283
    }
}

sebastien's avatar
sebastien committed
284
285
 /* Enter a native block */
<INITIAL>. { BEGIN NATIVE; yyless(0); }
286

sebastien's avatar
sebastien committed
287
288
 /* Add the native statement */
<NATIVE>.* { driver.add_native(yytext); BEGIN INITIAL; }
289

290
<*>.      { driver.error("Unrecognized character: '" + string(yytext) + "'"); }
291
292
293
294
295
296
297
%%

void
ParsingDriver::scan_begin()
{
  yy_flex_debug = trace_scanning;
  if (!(yyin = fopen(file.c_str(), "r")))
298
    error(string("cannot open file"));
299
300
301
302
303
304
305
}

void
ParsingDriver::scan_end()
{
  fclose(yyin);
}
306

307
308
309
310
311
/*
  Local variables:
  mode: C++
  End:
*/