%{ #include #include #include typedef struct { char *name; char *value; } stentry; int lineno = 1; char lvalue[256]; stentry ST[1024]; int nstentry = 0; char *str = NULL, *substr = NULL, rvalue[65536], *rvalptr; int rngbeg, rngend, repcnt; int rngread, expread; int stlookup ( char *name ) { int i = 0; for (i=0; i.*\n { lvalue[0] = rvalue[0] = '\0'; if (str) free(str); if (substr) free(substr); str = substr = NULL; fprintf(stderr, "Line %d cannot be processed\n", lineno); ++lineno; BEGIN INITIAL; } <*>{ws} { } {valname} { if (lvalue[0] == '\0') { strcpy(lvalue, yytext); } else { fprintf(stderr, " *** Invalid lvalue: "); BEGIN ERROR; } } = { if (lvalue[0] == '\0') { fprintf(stderr, " *** Invalid lvalue: "); BEGIN ERROR; } else { rvalue[0] = '\0'; rvalptr = rvalue; BEGIN RVALUE; } } {ows}/\n { fprintf(stderr, " *** Assignment operator missing: "); BEGIN ERROR; } {valstr} { if (str) { fprintf(stderr, " *** Invalid string \"%s\" in rvalue: ", yytext); BEGIN ERROR; } else { str = (char *)malloc((1 + strlen(yytext)) * sizeof(char)); strcpy(str,yytext); rngbeg = 0; rngend = strlen(str) - 1; repcnt = 1; rngread = expread = 0; } } \${valname} { if (str) { fprintf(stderr, " *** Invalid reference \"%s\" in rvalue: ", yytext); BEGIN ERROR; } else { int i = stlookup(yytext + 1); if (i == -1) { fprintf(stderr, " *** Reference to undefined variable \"%s\"\n", yytext + 1); str = (char *)malloc(sizeof(char)); str[0] = '\0'; } else { str = (char *)malloc((1 + strlen(ST[i].value)) * sizeof(char)); strcpy(str, ST[i].value); } rngbeg = 0; rngend = strlen(str) - 1; repcnt = 1; rngread = expread = 0; } } \[ { if (str == NULL) { fprintf(stderr, " *** No string argument: "); BEGIN ERROR; } else { rngbeg = rngend = -1; BEGIN RANGESEL; } } \^ { repcnt = -1; BEGIN EXPONENT; } \. { int i; if (substr == NULL) { substr = (char *)malloc((1 + strlen(str)) * sizeof(char)); strcpy(substr, str); } for (i=0; i\n { int i; if (str == NULL) { fprintf(stderr, " *** No r-value: Line %d cannot be processed\n", lineno); } else { if (substr == NULL) { substr = (char *)malloc((1 + strlen(str)) * sizeof(char)); strcpy(substr, str); } for (i=0; i{int} { if ((rngread) || (expread)) { fprintf(stderr, " *** Invalid range selector: "); BEGIN ERROR; } else { rngbeg = rngend = atoi(yytext); if (rngend >= strlen(str)) rngbeg = rngend + 1; } } , { if ((rngread) || (expread)) { fprintf(stderr, " *** Invalid range selector: "); BEGIN ERROR; } else { BEGIN RANGEENDSEL; } } {int} { rngend = atoi(yytext); if (rngend >= strlen(str)) rngbeg = rngend + 1; rngread = 1; BEGIN RANGESEL; } < { if ((rngread) || (expread) || (rngbeg != -1)) { fprintf(stderr, " *** Invalid prefix specifier: "); BEGIN ERROR; } else { BEGIN PREFIXSEL; } } {int} { int l; rngbeg = 0; rngend = atoi(yytext) - 1; l = strlen(str); if (rngend >= l) rngend = l - 1; rngread = 1; BEGIN RANGESEL; } > { if ((rngread) || (expread) || (rngbeg != -1)) { fprintf(stderr, " *** Invalid suffix specifier: "); BEGIN ERROR; } else { BEGIN SUFFIXSEL; } } {int} { rngend = strlen(str) - 1; rngbeg = rngend - atoi(yytext) + 1; if (rngbeg < 0) rngbeg = 0; rngread = 1; BEGIN RANGESEL; } \] { if ((rngbeg == -1) && (rngend == -1)) { fprintf(stderr, " *** Invalid range selector: "); lvalue[0] = '\0'; BEGIN ERROR; } else { if (rngbeg > rngend) { substr = (char *)malloc(sizeof(char)); substr[0] = '\0'; } else { int i, j; substr = (char *)malloc((rngend - rngbeg + 2) * sizeof(char)); i = 0; for (j=rngbeg; j<=rngend; ++j) substr[i++] = str[j]; substr[i] = '\0'; } rngread = 1; BEGIN RVALUE; } } {int} { if (expread) { fprintf(stderr, " *** Invalid exponent specifier: "); BEGIN ERROR; } else if (repcnt == -1) { repcnt = atoi(yytext); expread = 1; BEGIN RVALUE; } else { BEGIN ERROR; } } <*>. { fprintf(stderr, " *** Invalid character '%s' found. ", yytext); BEGIN ERROR; } %% int yywrap ( ) { return 1; } int main ( int argc, char *argv[] ) { if (argc > 1) yyin = (FILE *)fopen(argv[1],"r"); yylex(); exit(0); }