00001
00002
00007 %{
00008 #include <stdio.h>
00009 #define YYDEBUG 1
00010 int yydebug = 0;
00011
00012 int yylex();
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
00029 %token EOL
00030 %token LEXERROR
00031 %token <tstr> ALPHA
00032 %token <val> NUM
00033
00034 %%
00035
00036 file :
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 int yylex()
00077 {
00078 int c;
00079
00080 if (in_comment == 0)
00081 {
00082
00083 while ((c = getc(input)) == ' ' ||
00084 c == '\t' ||
00085 c == '\n')
00086 ;
00087
00088
00089 if (c == EOF)
00090 return 0;
00091
00092 if (yydebug != 0)
00093 fprintf(stderr, "yylex(): c = %c\n", c);
00094 if (c == '{')
00095 return LBRACE;
00096 if (c == '}')
00097 return RBRACE;
00098 if (c == '=')
00099 return EQUALS;
00100 if (c == ';')
00101 return EOS;
00102 if (c == '#')
00103 return POUND;
00104
00105
00106 if (isdigit(c))
00107 {
00108 ungetc(c, input);
00109 fscanf(input,
00110 "%lf",
00111 &yylval.val);
00112 if (yydebug != 0)
00113 fprintf(stderr, "yylex(): yylval.val = %lf\n", yylval.val);
00114 return NUM;
00115 }
00116
00117
00118 if (isgraph(c))
00119 {
00120 ungetc(c, input);
00121 fscanf(input,
00122 "%[^; \t=#}\{]s",
00123 yylval.tstr);
00124 if (yydebug != 0)
00125 printf("yylex(): yylval.tstr = %s\n", yylval.tstr);
00126 return ALPHA;
00127 }
00128
00129
00130 return LEXERROR;
00131 }
00132 else
00133 {
00134 while((c = getc(input)) != '\n')
00135 ;
00136 return EOL;
00137 }
00138 }
00139
00140 int yyerror(const char *s)
00141 {
00142 fprintf(stderr, "%s: %s\n", __FILE__, s);
00143 return 1;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161