Thread: Parsing an escaped quote inside a string

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Parsing an escaped quote inside a string

    Sorry if this is a double post, I didn't see my post on the board so I am reposting.

    I've written a rudimentary syntax checking program for c code, which just checks to make sure every {, (, or [ is closed in the appropriate order. It works fine right now, except for when I try and break it by adding (,[,{ characters inside strings.

    So if it checks this file:

    Code:
    #include <stdio.h>
    int main(int argc, char **argv){
         printf("hello world");
         return(1);
    }
    It works fine, but if I do this:

    Code:
    printf("there is escaped quotes \" in this string and now ( ( [ ) } will break the syntax check");
    I'm trying to figure out how to parse the character " differently than \" if that makes any sense. Here is my code, and I'll bold out the relevant section.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    #define STRLEN 256
    #define IN_LINE 1
    #define IN_MULTILINE 2
    #define IN_SINGLE_STRING 3
    #define IN_DOUBLE_STRING 4
    #define OUT 0
    #define TRUE 1
    #define FALSE 0
    #define BRACKETS 1
    #define PARENTHESES 2
    
    int check_syntax(FILE *fp);
    
    int main(int argc, char *argv[]){
        FILE *fp;
        int bad_line;
    
    	if (argc < 2){
    	    printf("Usage is syntax [FILE]");
    	    exit(EXIT_FAILURE);
    	}
    
    	if( (fp=fopen(argv[1], "r")) == NULL)
            exit(EXIT_FAILURE);
    
        bad_line = check_syntax(fp);
    
        fclose(fp);
    
        if(bad_line)
            printf("Error on line %d\n", bad_line);
        else
            printf("syntax is correct\n");
    
    	exit(EXIT_SUCCESS);
    }//}99[}
    
    
    int check_syntax(FILE *fp){
    	STACK *stack;
    
    	char cp, nextc;
    	int state, ln;
    	int error;
    
        stack = (STACK *)malloc(sizeof stack);
    
    	state = OUT;
    	ln  = 1;
    	error = FALSE;
    
    	while( (cp = fgetc(fp)) != EOF){
    	    nextc = fgetc(fp);
    	    ungetc(nextc, fp);
    
    		//check for comments first
    		if(state == OUT && cp == '/'){
    			if( nextc == '/'){ //we're in a single line comment
    				state = IN_LINE;
    				continue;
    			}
    			if(nextc == '*'){ //we're in a multi line comment
    				state = IN_MULTILINE;
    				continue;
    			}
    		}
    
    		//check for end of comments next
    		if(state == IN_LINE && cp == '\n'){
    			state = OUT;
    			continue;
    		}
    		if(state == IN_MULTILINE && cp == '*' && nextc == '/'){
    			state = OUT;
    			continue;
    		}
    
    		//check for in string
            if(state == OUT && cp == '\"')
                state = IN_DOUBLE_STRING;
            if(state == OUT && cp == '\'')
                state = IN_SINGLE_STRING;
            if(state == IN_DOUBLE_STRING && cp == '\\' && nextc == '\"'){
                cp = fgetc(fp);
                cp = fgetc(fp);
            }
            if(state == IN_SINGLE_STRING && cp == '\\' && nextc == '\''){
                cp = fgetc(fp);
                cp = fgetc(fp);
            }
            if(state == IN_DOUBLE_STRING && cp == '\"')
                state = OUT;
            if(state == IN_SINGLE_STRING && cp == '\'')
                state = OUT;
    
    		//we are out of a comment, time to check syntax
    		if(state == OUT){
                switch(cp){
                    case '\n':
                        ln++;
                        break;
                    case '{':
                    case '}':
                        if(stack->state != OUT)
                            error = TRUE;
                        break;
                    case '(':
                        stack = add_to_stack(stack, PARENTHESES);
                        break;
                    case ')':
                        if(stack->state != PARENTHESES)
                            error = TRUE;
                        stack = pop_stack(stack);
                        break;
                    case '[':
                        stack = add_to_stack(stack, BRACKETS);
                        break;
                    case ']':
                        if(stack->state != BRACKETS)
                            error = TRUE;
                        stack = pop_stack(stack);
                        break;
                }
                if(error == TRUE)
                    return(ln);
    		}
    	}
    	return(0);
    }
    I hope this makes sense, thanks for looking

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you have a " in a file, and you read it into a string, you don't need to check it for \. If you write a string to a file with a " in it, it just writes a ", not a \".


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    So how should I know when to skip over the escaped quotes in this line.

    Code:
    printf("this is a \"quoted string");
    My code is checking each character one by one, and if it hits a " character, it sets the state to DOUBLE_QUOTE, and will ignore the every character until it gets to another " character setting the state back to OUT. When it checks the above line it will thing the quote has finished at the character before the q. What can I do??

    Thanks for the fast reply by the way.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    read character while not eof
        if quote
            lastchar = char
            read character while not eof
                if quote
                    if lastchar is not backslash
                        break
                lastchar = char
    Something like that should do the trick. That should skip if you were say reading a .c file, and it had escaped characters in a string you were trying to skip.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Thanks Quzah, ended up optimizing and cleaning up my code and it works great, I'm still not 100% sure why it wasn't working before but it looks better now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM