00001
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
00027
00028
00029 %%
00030
00031 file :
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
00045 }
00046 ;
00047
00048 rvalue : ALPHA
00049 {
00050
00051 }
00052 | NUM
00053 {
00054
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
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
00083 if (isdigit(c))
00084 {
00085 ungetc(c, input);
00086 fscanf(input, "%lf", &yylval.val);
00087 return NUM;
00088 }
00089
00090
00091 if (isalpha(c))
00092 {
00093 ungetc(c, input);
00094 fscanf(input, "%s", &yylval.tstr);
00095 return ALPHA;
00096 }
00097
00098
00099 if (c == EOF)
00100 return 0;
00101
00102 return c;
00103 }
00104
00105 int yyerror(const char *s)
00106 {
00107 printf("%s: %s\n", __FILE__, s);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122