diff options
Diffstat (limited to 'expr.y')
-rw-r--r-- | expr.y | 100 |
1 files changed, 41 insertions, 59 deletions
@@ -15,45 +15,17 @@ typedef struct { } u; } expr_yystype; +static char **expr_args; + #define YYSTYPE expr_yystype -#include "y.tab.h" +//#include "y.tab.h" static int yylex(void); -extern int yyerror(const char *s); - -static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2) -{ - if (y1->type == STRING) { - fprintf(stderr, "expr: %s is not an integer\n", y1->u.s); - exit(1); - } - if (y2->type == STRING) { - fprintf(stderr, "expr: %s is not an integer\n", y2->u.s); - exit(1); - } -} - -static int expr_compare(YYSTYPE *y1, YYSTYPE *y2) -{ - if (y1->type == INTEGER && y2->type == INTEGER) { - return y1->u.i - y2->u.i; - } +static int yyerror(const char *s); +static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2); +static int expr_compare(YYSTYPE *y1, YYSTYPE *y2); - char buf[64]; - char *s1 = y1->u.s; - char *s2 = y2->u.s; - - if (y1->type == INTEGER) { - snprintf(buf, sizeof(buf), "%d", y1->u.i); - s1 = buf; - } else if (y2->type == INTEGER) { - snprintf(buf, sizeof(buf), "%d", y2->u.i); - s2 = buf; - } - - return strcmp(s1, s2); -} %} %token INTEGER @@ -154,33 +126,38 @@ expr : STRING { %% +static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2) +{ + if (y1->type == STRING) { + fprintf(stderr, "expr: %s is not an integer\n", y1->u.s); + exit(1); + } + if (y2->type == STRING) { + fprintf(stderr, "expr: %s is not an integer\n", y2->u.s); + exit(1); + } +} -/* - -"|" return '|'; -"&" return '&'; -"=" return '='; -">" return '>'; -">=" return GE; -"<" return '<'; -"<=" return LE; -"!=" return NE; -"+" return '+'; -"-" return '-'; -"*" return '*'; -"/" return '/'; -"%" return '%'; -"(" return '('; -")" return ')'; -":" return ':'; -{DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; } --{DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; } -.+ { yylval.u.s = strdup(yytext); return yylval.type = STRING; } -\n ; - -*/ +static int expr_compare(YYSTYPE *y1, YYSTYPE *y2) +{ + if (y1->type == INTEGER && y2->type == INTEGER) { + return y1->u.i - y2->u.i; + } -static char **expr_args; + char buf[64]; + char *s1 = y1->u.s; + char *s2 = y2->u.s; + + if (y1->type == INTEGER) { + snprintf(buf, sizeof(buf), "%d", y1->u.i); + s1 = buf; + } else if (y2->type == INTEGER) { + snprintf(buf, sizeof(buf), "%d", y2->u.i); + s2 = buf; + } + + return strcmp(s1, s2); +} static int yylex(void) { @@ -225,6 +202,11 @@ static int yylex(void) return yylval.type; } +static int yyerror(const char *s) +{ + fprintf(stderr, "expr: %s\n", s); +} + int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); |