#include #include #include #include "lex.yy.c" typedef struct { char *name; char *value; } stpair; stpair ST[4096]; int nstentry = 0; int STindex ( char *name ) { int i; for (i=0; i ID = EXPR */ lval = strdup(yytext); token = yylex(); if (token != ASG) llerror("= expected"); token = yylex(); rval = EXPR(); printf("+++ Storing %s = %s\n", lval, rval); STstore(lval,rval); } char *EXPR ( ) { char *arg, *rest; int l; if ((token != STR) && (token != REF) && (token != LPN)) llerror("String/Reference/( expected"); /* EXPR -> ARG REST */ arg = ARG(); rest = REST(); if (rest) { l = strlen(arg); arg = (char *)realloc(arg, (l + strlen(rest) + 1) * sizeof(char)); strcpy(arg+l,rest); free(rest); } return arg; } char *REST ( ) { if ( (token == RPN) || (token == EOL) ) { /* REST -> EMPTY */ return NULL; } if (token == DOT) { /* REST -> . EXPR */ token = yylex(); return EXPR(); } llerror("End or continuation of expression expected"); } char *ARG ( ) { int repcnt, i, l; char *arg, *base, *xpnt, *p; if ((token != STR) && (token != REF) && (token != LPN)) llerror("String/Reference/( expected"); /* ARG -> BASE XPNT */ base = BASE(); xpnt = XPNT(); repcnt = (xpnt == NULL) ? 1 : atoi(xpnt); if (xpnt) free(xpnt); if (repcnt == 0) { base = (char *)realloc(base, sizeof(char)); base[0] = '\0'; return base; } if (repcnt == 1) return base; l = strlen(base); arg = (char *)malloc((repcnt * l + 1) * sizeof(char)); p = arg; for (i=0; i EMPTY */ return NULL; } if (token == CRT) { /* XPNT -> ^ NUM */ token = yylex(); if (token != NUM) llerror("Number expected as exponent"); xpnt = strdup(yytext); token = yylex(); return xpnt; } llerror("Exponent expected"); } char *BASE ( ) { char *base; if (token == STR) { /* BASE -> STR */ base = strdup(yytext); token = yylex(); return base; } if (token == REF) { /* BASE -> $ID */ base = STload(yytext + 1); token = yylex(); return base; } if (token == LPN) { /* BASE -> ( EXPR ) */ token = yylex(); base = EXPR(); if (token != RPN) llerror(") expected"); token = yylex(); return base; } llerror("Valid base expected"); } int main ( int argc, char *argv[] ) { if (argc > 1) yyin = (FILE *)fopen(argv[1], "r"); while (1) { token = yylex(); if (token == 0) break; if (token == EOL) continue; LINE(); if (token != EOL) { fprintf(stderr, "*** Parse error: New line expected\n"); exit(1); } } exit(0); }