Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {string} {
- char c;
- int i, error, lookahead;
- i = 0; /* the string index the next character will be placed at */
- error = 1; /* flag that keeps track of the string's proper closure */
- lookahead = 0; /* set when looking ahead for special characters */
- char * s = malloc(2 * sizeof(char));
- *(s + i) = '\0';
- while ((c = input()) != EOF)
- {
- if (lookahead == 1)
- { /* we've already checked this character, move on */
- lookahead = 0;
- continue;
- }
- if (c == '\\')
- { /* check the next character to see if it's special */.
- c = input();
- lookahead = 1;
- if (c == 'n')
- { /* newline */
- *(s + i) = '\n';
- }
- else if (c == 't')
- { /* tab */
- c = '\t';
- *(s + i) = c;
- }
- else if (c == '\\')
- { /* backslash */
- *(s + i) = '\\';
- }
- else if (c == '\"')
- { /* double quotation mark */
- *(s + i) = '"';
- }
- else if (c == '\'')
- { /* single quotation mark */
- *(s + i) = '\'';
- }
- else
- { /* unknown character */
- *(s + i) = '\\';
- i++;
- *(s + i) = c;
- printf("Error: Unknown escape character found in string\n");
- }
- }
- else if (c == '\"')
- { /* end of string */
- *(s + i) = '\0';
- head = add_token(head, yylineno, ++token, "STRING", s);
- error = 0;
- break;
- }
- else
- { /* normal character */
- *(s + i) = c;
- }
- i++;
- s = realloc(s, (i + 2) * sizeof(char));
- *(s + i) = '\0';
- }
- if (error == 1)
- { /* the string wasn't closed properly */
- /* thus, we're not going to store it as a token */
- printf("Error: Unclosed string %s at line %d", s, yylineno);
- }
- free(s);
- /*end of string code*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement