Thread: A simple C parser problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    A simple C parser problem

    So I'm trying out some parsing right now, it's just some simple if statement checking, and I can't get it to read in the last character from a file properly. I've been screwing around in the code for several hours and have only learned that the problem is in the getnext_arg function.
    Thanks for any help you can give.
    Code:
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    
    char gotten_word[35];
    int getnext_arg(FILE * pfile){
    	int gotten_char='a';
    	int i = 0;
    	if(pfile != NULL){
    		while (gotten_char != EOF && gotten_char != '\n' && gotten_char != ' '){
    			gotten_char = getc(pfile);
    			printf("GC: %c\n", gotten_char);
    			if(gotten_char == EOF){
    				return 2;
    			}else if(gotten_char != '\n' && gotten_char != ' '){
    					gotten_word[i] = gotten_char;
    					i++;
    				}
    			}
    	}else{
    		printf("Error: Null file\n");
    		return 1;
    	}
    	return 0;
    }
    int ifequ(int c1, int c2){
    	if(c1 == c2)
    	{
    		return 1;
    	}else{
    		return 0;
    	}
    }
    int ifnequ(int c1, int c2){
    	if(c1 != c2)
    	{
    		return 1;
    	}else{
    		return 0;
    	}
    }
    int exeCode(){
    	printf("Executing\n");
    }
    int nexeCode(){
    	printf("Not executing\n");
    }
    
    
    int main(void){
    	FILE * pfile;
    	int comp1 = ' ';
    	int comp2 = ' ';
    	char compOp[2];
    	char path[45];
    	int ret;
    
    	printf("Enter path to file: ");
    	gets(path);
    	
    	pfile = fopen(path, "r");
    	
    	if(pfile != NULL){
    		while(getnext_arg(pfile) != 2){
    			if( !strcmp(gotten_word, "if") ){
    				getnext_arg(pfile);
    				comp1 = atoi(gotten_word);
    				printf("%d\n", comp1);
    				getnext_arg(pfile);
    				if(!strcmp(gotten_word, "==") || !strcmp(gotten_word, "!=")){
    					strcpy(compOp, gotten_word);
    					printf("%s\n", compOp);
    				}else{
    					printf("Error: Invalid operator.");
    					return 1;
    				}
    				getnext_arg(pfile);
    				printf("%s\n", gotten_word);
    				comp2 = atoi(gotten_word);
    				printf("%i\n", comp2);
    					if( !strcmp(compOp, "==")){
    						ret = ifequ(comp1, comp2);
    					}
    					if( !strcmp(compOp, "!=")){
    						ret = ifnequ(comp1, comp2);
    					}
    					if(ret == 1){
    						exeCode();
    					}else{
    						nexeCode();
    					}
    				}
    			}
    	} else { 
    		printf("Null error\n");
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use <> bracket for standard header files... The #include Directive (C/C++)
    If you are going to use gotten_word as string, try to null terminate it in your getnext_arg() function.
    and gets() is not recommended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with piping in my simple UNIX shell
    By Integral Birth in forum C Programming
    Replies: 0
    Last Post: 11-28-2010, 07:37 PM
  2. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM