Parser.y

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