2 * string2.lex: An example of using scanning strings
3 * by using start states.
10 #define isodigit(x) ((x) >= '0' && (x) <= '7')
11 #define hextoint(x) (isdigit((x)) ? (x) - '0' : ((x) - 'A') + 10)
16 void yyerror(char *message)
18 printf("\nError: %s\n",message);
25 hex (x|X)[0-9a-fA-F]{1,2}
32 buffer_size = 1; strcpy(buffer,"");
36 yyerror("Unterminated string");
41 yyerror("EOF in string");
46 buffer = realloc(buffer,buffer_size+yyleng+1);
47 buffer_size += yyleng;
48 strcat(buffer,yytext);
50 <STRING>\\\n /* ignore this */
53 for(loop=yyleng-2; loop>0; loop--){
55 temp += hextoint(toupper(yytext[yyleng-loop]));
57 buffer = realloc(buffer,buffer_size+1);
58 buffer[buffer_size-1] = temp;
59 buffer[buffer_size] = '\0';
64 for(loop=yyleng-1; loop>0; loop--){
66 temp += (yytext[yyleng-loop] - '0');
68 buffer = realloc(buffer,buffer_size+1);
69 buffer[buffer_size-1] = temp;
70 buffer[buffer_size] = '\0';
74 buffer = realloc(buffer,buffer_size+1);
75 switch(yytext[yyleng-1]){
76 case 'b' : buffer[buffer_size-1] = '\b'; break;
77 case 't' : buffer[buffer_size-1] = '\t'; break;
78 case 'n' : buffer[buffer_size-1] = '\n'; break;
79 case 'v' : buffer[buffer_size-1] = '\v'; break;
80 case 'f' : buffer[buffer_size-1] = '\f'; break;
81 case 'r' : buffer[buffer_size-1] = '\r'; break;
82 default : buffer[buffer_size-1] = yytext[yyleng-1];
84 buffer[buffer_size] = '\0';
88 printf("string = \"%s\"",buffer);