Qucs-core  0.0.19
parse_csv.y
Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 
00003 %{
00004 /*
00005  * parse_csv.y - parser for CSV files
00006  *
00007  * Copyright (C) 2007 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_csv.h"
00047 
00048 using namespace qucs;
00049 
00050 %}
00051 
00052 %name-prefix "csv_"
00053 
00054 %token InvalidCharacter
00055 %token Float
00056 %token Identifier
00057 %token Eol
00058 
00059 %union {
00060   char * ident;
00061   double f;
00062   qucs::vector * v;
00063   qucs::strlist * list;
00064 }
00065 
00066 %type <list> HeaderList HeaderLine
00067 %type <ident> Identifier Header
00068 %type <f> Float
00069 %type <v> DataLine DataSet DataList
00070 
00071 %%
00072 
00073 Input:
00074     HeaderLine DataSet { /* describes a valid csv */
00075     csv_header = $1;
00076     csv_vector = $2;
00077   }
00078   | DataSet {
00079     csv_vector = $1;
00080   }
00081 ;
00082 
00083 Header:
00084     '"' Identifier '"' {
00085     $$ = strdup ($2);
00086   }
00087   | Identifier {
00088     $$ = strdup ($1);
00089   }
00090 ;
00091 
00092 HeaderList: /* nothing */ { $$ = NULL; }
00093   | Header HeaderList {
00094     if ($2 == NULL) $2 = new strlist ();
00095     $2->add ($1);
00096     $$ = $2;
00097     free ($1);
00098   }
00099 ;
00100 
00101 HeaderLine: /* header line */
00102     HeaderList Eol {
00103     $$ = $1;
00104   }
00105   | Eol HeaderLine { /* skip this line */
00106     $$ = $2;
00107   }
00108 ;
00109 
00110 DataSet: /* nothing */ { $$ = NULL; }
00111   | DataLine Eol DataSet { /* append vector lines */
00112     $1->setNext ($3);
00113     $$ = $1;
00114   }
00115   | DataLine { /* last line, no trailing end-of-line */
00116     $$ = $1;
00117   }
00118   | Eol DataSet { /* skip this line */
00119     $$ = $2;
00120   }
00121 ;
00122 
00123 DataLine: DataList
00124 ;
00125 
00126 DataList: /* nothing */ { $$ = NULL; }
00127   | Float DataList {
00128     if ($2 == NULL) $2 = new vector ();
00129     $2->add ($1);
00130     $$ = $2;
00131   }
00132 ;
00133 
00134 %%
00135 
00136 int csv_error (const char * error) {
00137   logprint (LOG_ERROR, "line %d: %s\n", csv_lineno, error);
00138   return 0;
00139 }