2 * string1.lex: Handling strings by using input()
9 #define ALLOC_SIZE 32 /* for (re)allocating the buffer */
11 #define isodigit(x) ((x) >= '0' && (x) <= '7')
12 #define hextoint(x) (isdigit((x)) ? (x) - '0' : ((x) - 'A') + 10)
14 void yyerror(char *message)
16 printf("\nError: %s\n",message);
24 int inch,count,max_size;
28 buffer = malloc(ALLOC_SIZE);
29 max_size = ALLOC_SIZE;
32 while(inch != EOF && inch != '"' && inch != '\n'){
36 case '\n': inch = input(); break;
37 case 'b' : inch = '\b'; break;
38 case 't' : inch = '\t'; break;
39 case 'n' : inch = '\n'; break;
40 case 'v' : inch = '\v'; break;
41 case 'f' : inch = '\f'; break;
42 case 'r' : inch = '\r'; break;
44 case 'x' : inch = input();
46 temp = hextoint(toupper(inch));
49 temp = (temp << 4) + hextoint(toupper(inch));
64 temp = (temp << 3) + (inch - '0');
71 temp = (temp << 3) + (inch - '0');
80 buffer[count++] = inch;
81 if(count >= max_size){
82 buffer = realloc(buffer,max_size + ALLOC_SIZE);
83 max_size += ALLOC_SIZE;
87 if(inch == EOF || inch == '\n'){
88 yyerror("Unterminated string.");
91 printf("String = \"%s\"\n",buffer);