/* * string1.lex: Handling strings by using yyinput() */ %{ #include #include #define ALLOC_SIZE 32 /* for (re)allocating the buffer */ #define isodigit(x) ((x) >= '0' && (x) <= '7') #define hextoint(x) (isdigit((x)) ? (x) - '0' : ((x) - 'A') + 10) void yyerror(char *message) { printf("\nError: %s\n",message); } %} %% \" { int inch,count,max_size; char *buffer; int temp; buffer = malloc(ALLOC_SIZE); max_size = ALLOC_SIZE; inch = yyinput(); count = 0; while(inch != EOF && inch != '"' && inch != '\n'){ if(inch == '\\'){ inch = yyinput(); switch(inch){ case '\n': inch = yyinput(); break; case 'b' : inch = '\b'; break; case 't' : inch = '\t'; break; case 'n' : inch = '\n'; break; case 'v' : inch = '\v'; break; case 'f' : inch = '\f'; break; case 'r' : inch = '\r'; break; case 'X' : case 'x' : inch = yyinput(); if(isxdigit(inch)){ temp = hextoint(toupper(inch)); inch = yyinput(); if(isxdigit(inch)){ temp = (temp << 4) + hextoint(toupper(inch)); } else { unput(inch); } inch = temp; } else { unput(inch); inch = 'x'; } break; default: if(isodigit(inch)){ temp = inch - '0'; inch = yyinput(); if(isodigit(inch)){ temp = (temp << 3) + (inch - '0'); } else { unput(inch); goto done; } inch = yyinput(); if(isodigit(inch)){ temp = (temp << 3) + (inch - '0'); } else { unput(inch); } done: inch = temp; } } } buffer[count++] = inch; if(count >= max_size){ buffer = realloc(buffer,max_size + ALLOC_SIZE); max_size += ALLOC_SIZE; } inch = yyinput(); } if(inch == EOF || inch == '\n'){ yyerror("Unterminated string."); } buffer[count] = '\0'; printf("String = \"%s\"\n",buffer); free(buffer); } . \n %%