ssCommonEnviromentYy.y

Go to the documentation of this file.
00001 /* $Id: ssCommonEnviromentYy.y,v 1.4 2005/04/01 17:52:20 pfb Exp $ */
00002 
00007 %{
00008 #include <stdio.h>
00009 #define YYDEBUG 0
00010   int yydebug = 0;
00011 
00012   int ssCommonEnviromentYylex();
00013   int yyerror(const char *s);
00014   extern void pusha(const char *lvalue, const char *rvalue);
00015   extern void pushn(const char *lvalue, double);
00016 
00017   int in_comment = 0;
00018 %}
00019 
00020 %union {
00021   double val;
00022   char   tstr[128];
00023 }
00024 
00025 %token LBRACE RBRACE
00026 %token EQUALS
00027 %token POUND
00028 %token EOS          /* Terminates a statement */
00029 %token EOL          /* End of line */
00030 %token LEXERROR     /* an error in yylex() */
00031 %token <tstr> ALPHA
00032 %token <val>  NUM
00033 
00034 %%
00035 
00036 file          :  /* empty */
00037               | file statement
00038               | file comment
00039               ;
00040 
00041 statement     : lvalue EQUALS ALPHA EOS
00042                 {
00043                 strcpy($<tstr>$, $<tstr>3);
00044                 pusha($<tstr>1, $<tstr>3);
00045                 }
00046               | lvalue EQUALS NUM EOS  
00047                 {
00048                 $<val>$ = $<val>3;
00049                 pushn($<tstr>1, $<val>3);
00050                 }
00051               ;
00052 
00053 comment       : POUND
00054                 {
00055                 in_comment = 1;
00056                 }
00057                 EOL
00058                 {
00059                 in_comment = 0;
00060                 }
00061               ;  
00062 
00063 lvalue        : ALPHA
00064                 {
00065                 strcpy($<tstr>$, $<tstr>1);
00066                 }
00067               ;
00068 
00069 %%
00070 
00071 #include <ctype.h>
00072 #include <stdio.h>
00073 
00074 FILE *input;
00075 
00076 //------------------------------------------------------------------------------
00077 int ssCommonEnviromentYylex()
00078   {
00079   int c;
00080 
00081   if (in_comment == 0)
00082     {
00083     /* skip white space  */
00084     while ((c = getc(input)) == ' ' ||
00085             c == '\t'               ||
00086             c == '\n')
00087       ;
00088 
00089     /* return end-of-file  */
00090     if (c == EOF)                            
00091       return 0;
00092 
00093     if (yydebug != 0)
00094       fprintf(stderr, "ssCommonEnviromentYylex(): c = %c\n", c);
00095     if (c == '{')
00096       return LBRACE;
00097     if (c == '}')
00098       return RBRACE;
00099     if (c == '=')
00100       return EQUALS;
00101     if (c == ';')
00102       return EOS;
00103     if (c == '#')
00104       return POUND;
00105 
00106     /* process numbers   */
00107     if (isdigit(c))                
00108       {
00109       ungetc(c, input);
00110       fscanf(input,
00111              "%lf",
00112              &yylval.val);
00113       if (yydebug != 0)
00114         fprintf(stderr,
00115                 "ssCommonEnviromentYylex(): yylval.val = %lf\n",
00116                 yylval.val);
00117       return NUM;
00118       }
00119   
00120     /* process names */
00121     if (isgraph(c))
00122       {
00123       ungetc(c, input);
00124       fscanf(input,
00125              "%[^; \t=#}\{]s",
00126              yylval.tstr);
00127       if (yydebug != 0)
00128         fprintf(stderr,
00129                 "ssCommonEnviromentYylex(): yylval.tstr = %s\n",
00130                 yylval.tstr);
00131       return ALPHA;
00132       }
00133 
00134     /* return an error */
00135     return LEXERROR;
00136     }
00137   else
00138     {
00139     while((c = getc(input)) != '\n')
00140       ;
00141     return EOL;
00142     }
00143   }
00144 
00145 //------------------------------------------------------------------------------
00146 int ssCommonEnviromentYyerror(const char *s)
00147   {
00148   fprintf(stderr, "%s: %s\n", __FILE__, s);
00149   return 1;
00150   }
00151 
00152 /*
00153  * $Log: ssCommonEnviromentYy.y,v $
00154  * Revision 1.4  2005/04/01 17:52:20  pfb
00155  * The yyparse(), yylex() and yyerror() functions are now prefixed. This is to
00156  * avoid conflicts with the same names in the VTK libraries.
00157  *
00158  * Revision 1.3  2004/07/19 12:18:38  pfb
00159  * Sorted out ALPHA fscanf().
00160  *
00161  * Revision 1.2  2004/07/19 10:11:12  pfb
00162  * Added some yydebug stuff.
00163  *
00164  * Revision 1.1  2004/07/19 04:19:06  pfb
00165  * Initial rev.
00166  *
00167  *
00168  */