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 (c == '{')
00093 return LBRACE;
00094 if (c == '}')
00095 return RBRACE;
00096 if (c == '=')
00097 return EQUALS;
00098 if (c == ';')
00099 return EOS;
00100 if (c == '#')
00101 return POUND;
00102
00103
00104 if (isdigit(c))
00105 {
00106 ungetc(c, input);
00107 fscanf(input, "%lf", &yylval.val);
00108 return NUM;
00109 }
00110
00111
00112 if (isgraph(c))
00113 {
00114 ungetc(c, input);
00115 fscanf(input, "%[^; \t]s", yylval.tstr);
00116 return ALPHA;
00117 }
00118
00119
00120 return LEXERROR;
00121 }
00122 else
00123 {
00124 while((c = getc(input)) != '\n')
00125 ;
00126 return EOL;
00127 }
00128 }
00129
00130 int yyerror(const char *s)
00131 {
00132 printf("%s: %s\n", __FILE__, s);
00133 return 1;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142