Qucs-core  0.0.19
parse_citi.y
Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 
00003 %{
00004 /*
00005  * parse_citi.y - parser for CITIfiles
00006  *
00007  * Copyright (C) 2006 Stefan Jahn <stefan@lkcc.org>
00008  *
00009  * This is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013  *
00014  * This software is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this package; see the file COPYING.  If not, write to
00021  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00022  * Boston, MA 02110-1301, USA.
00023  *
00024  * $Id$
00025  *
00026  */
00027 
00028 #if HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 
00036 #define YYERROR_VERBOSE 42
00037 #define YYDEBUG 1
00038 #define YYMAXDEPTH 1000000
00039 
00040 #include "logging.h"
00041 #include "complex.h"
00042 #include "object.h"
00043 #include "vector.h"
00044 #include "dataset.h"
00045 #include "strlist.h"
00046 #include "check_citi.h"
00047 
00048 using namespace qucs;
00049 
00050 %}
00051 
00052 %name-prefix "citi_"
00053 
00054 %token InvalidCharacter
00055 %token Float
00056 %token Eol
00057 %token DATA
00058 %token VAR
00059 %token NAME
00060 %token Begin
00061 %token End
00062 %token Version
00063 %token Identifier
00064 %token Integer
00065 %token CITIFILE
00066 %token VarType
00067 %token SegListBegin
00068 %token VarListBegin
00069 %token SegListEnd
00070 %token VarListEnd
00071 %token COMMENT
00072 %token CONSTANT
00073 %token SEG
00074 
00075 %union {
00076   char * ident;
00077   double f;
00078   int d;
00079   qucs::vector * v;
00080   struct citi_package_t * p;
00081   struct citi_header_t * h;
00082 }
00083 
00084 %type <p> Package PackageList
00085 %type <h> HeaderLine Header HeaderList
00086 %type <d> Integer
00087 %type <f> Float
00088 %type <ident> Identifier VarType
00089 %type <v> List VarList Data FloatList DataList
00090 
00091 %%
00092 
00093 Input:
00094   PackageList {
00095     citi_root = $1; /* describes a valid CITIfile */
00096   }
00097 ;
00098 
00099 PackageList: /* nothing */ { $$ = NULL; }
00100   | Package PackageList {
00101     if ($1) {
00102       $1->next = $2;
00103       $$ = $1;
00104     } else {
00105       $$ = $2;
00106     }
00107   }
00108   | Eol PackageList {
00109     $$ = $2;
00110   }
00111 ;
00112 
00113 Package:
00114   Header DataList {
00115     $$ = (struct citi_package_t *) calloc (sizeof (struct citi_package_t), 1);
00116     $$->head = $1;
00117     $$->data = $2;
00118   }
00119 ;
00120 
00121 Header:
00122   CITIFILE Version Eol HeaderList {
00123     $$ = $4;
00124   }
00125 ;
00126 
00127 HeaderList: { $$ = NULL; }
00128   | HeaderLine HeaderList {
00129     if ($1) {
00130       $1->next = $2;
00131       $$ = $1;
00132     } else {
00133       $$ = $2;
00134     }
00135   }
00136 ;
00137 
00138 HeaderLine:
00139   NAME Identifier Eol {
00140     $$ = (struct citi_header_t *) calloc (sizeof (struct citi_header_t), 1);
00141     $$->package = $2;
00142   }
00143   | VAR Identifier VarType Integer Eol {
00144     $$ = (struct citi_header_t *) calloc (sizeof (struct citi_header_t), 1);
00145     $$->var = $2;
00146     $$->type = $3;
00147     $$->n = $4;
00148     $$->i1 = $$->i2 = -1;
00149   }
00150   | DATA Identifier VarType Eol {
00151     $$ = (struct citi_header_t *) calloc (sizeof (struct citi_header_t), 1);
00152     $$->var = $2;
00153     $$->type = $3;
00154     $$->n = $$->i1 = $$->i2 = -1;
00155   }
00156   | DATA Identifier '[' Integer ',' Integer ']' VarType Eol {
00157     $$ = (struct citi_header_t *) calloc (sizeof (struct citi_header_t), 1);
00158     $$->var = $2;
00159     $$->i1 = $4;
00160     $$->i2 = $6;
00161     $$->type = $8;
00162     $$->n = -1;
00163   }
00164   | DATA Identifier '[' Integer ']' VarType Eol {
00165     $$ = (struct citi_header_t *) calloc (sizeof (struct citi_header_t), 1);
00166     $$->var = $2;
00167     $$->i1 = $4;
00168     $$->type = $6;
00169     $$->n = $$->i2 = -1;
00170   }
00171   | CONSTANT Identifier Float ValueList Eol {
00172     /* ignore constants */
00173   }
00174 ;
00175 
00176 ValueList: /* nothing */ { }
00177   | Float ValueList {
00178   }
00179 ;
00180 
00181 List:
00182   SegListBegin Eol SEG Float Float Float Eol SegListEnd Eol {
00183     $$ = new vector (qucs::linspace ($5, $4, (int) $6));
00184   }
00185   | VarListBegin Eol VarList VarListEnd Eol {
00186     $$ = $3;
00187   }
00188 ;
00189 
00190 DataList: { $$ = NULL; }
00191   | Data DataList {
00192     if ($1) {
00193       $1->setNext ($2);
00194       $$ = $1;
00195     } else {
00196       $$ = $2;
00197     }
00198   }
00199 ;
00200 
00201 Data:
00202   Begin Eol FloatList End Eol {
00203     $$ = $3;
00204   }
00205   | List {
00206     $$ = $1;
00207   }
00208 ;
00209 
00210 FloatList: { $$ = new vector (); }
00211   | Float Eol FloatList {
00212     $3->add ($1);
00213     $$ = $3;
00214   }
00215   | Float ',' Float Eol FloatList {
00216     $5->add (nr_complex_t ($1, $3));
00217     $$ = $5;
00218   }
00219 ;
00220 
00221 VarList: { $$ = new vector (); }
00222   | Float Eol VarList {
00223     $3->add ($1);
00224     $$ = $3;
00225   }
00226 ;
00227 
00228 %%
00229 
00230 int citi_error (const char * error) {
00231   logprint (LOG_ERROR, "line %d: %s\n", citi_lineno, error);
00232   return 0;
00233 }