ParserYy.y

Go to the documentation of this file.
00001 /* $Id: ParserYy.y,v 1.3 2004/07/14 14:29:59 pfb Exp $ */
00002 
00007 %{
00008 #include <stdio.h>
00009 #define YYDEBUG 1
00010 #define YYERROR_VERBOSE
00011 
00012   int yylex();
00013   FILE *input;
00014 
00015 %}
00016 
00017 %union {
00018   double val;
00019   char   tstr[128];
00020 }
00021 
00022 %token LBRACE RBRACE
00023 %token EQUALS
00024 %token <tstr> ALPHA
00025 %token <val>  NUM
00026 /* %type  <tstr> lvalue
00027 %type  <val>  rvalue */
00028 
00029 %%
00030 
00031 file               :  /* empty */
00032                    | blocks
00033                    ;
00034 
00035 blocks             : LBRACE blocks RBRACE
00036                    | LBRACE statements RBRACE
00037                    ;
00038 
00039 statements         : lvalue EQUALS rvalue
00040                    ;
00041 
00042 lvalue             : ALPHA
00043                        {
00044                        // printf("lvalue = %s\n", $<tstr>1);
00045                        }
00046                    ;
00047 
00048 rvalue             : ALPHA
00049                        {
00050                        // printf("rvalue = %s\n", $<tstr>1);
00051                        }
00052                    | NUM
00053                        {
00054                        // printf("rvalue = %f\n", $<val>1);
00055                        }
00056                    ;
00057 
00058 %%
00059 
00060 #include <ctype.h>
00061 #include <stdio.h>
00062 
00063 extern FILE *input;
00064 
00065 int yylex()
00066   {
00067   int c;
00068 
00069   /* skip white space  */
00070   while ((c = getc(input)) == ' ' ||
00071           c == '\t' ||
00072           c == '\n')  
00073     ;
00074   
00075   if (c == '{')
00076     return LBRACE;
00077   if (c == '}')
00078     return RBRACE;
00079   if (c == '=')
00080     return EQUALS;
00081 
00082   /* process numbers   */
00083   if (isdigit(c))                
00084     {
00085     ungetc(c, input);
00086     fscanf(input, "%lf", &yylval.val);
00087     return NUM;
00088     }
00089   
00090   /* process names */
00091   if (isalpha(c))
00092     {
00093     ungetc(c, input);
00094     fscanf(input, "%s", &yylval.tstr);
00095     return ALPHA;
00096     }
00097 
00098   /* return end-of-file  */
00099   if (c == EOF)                            
00100     return 0;
00101   /* return single chars */
00102   return c;                                
00103   }
00104 
00105 int yyerror(const char *s)
00106   {
00107   printf("%s: %s\n", __FILE__, s);
00108   }
00109 
00110 /*
00111  * $Log: ParserYy.y,v $
00112  * Revision 1.3  2004/07/14 14:29:59  pfb
00113  * Added debug stuff.
00114  *
00115  * Revision 1.2  2004/07/14 06:30:10  pfb
00116  * Took out debut POs.
00117  *
00118  * Revision 1.1  2004/07/13 13:49:12  pfb
00119  * Initial rev.
00120  *
00121  *
00122  */