writing an interpreter for this programming language - segmentation error

This is a discussion on writing an interpreter for this programming language - segmentation error within the C Programming forums, part of the General Programming Boards category; i am writing a brainf**k interpreter for fun, but i'm getting a segmentation error and i do not understand why, ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    writing an interpreter for this programming language - segmentation error

    i am writing a brainf**k interpreter for fun, but i'm getting a segmentation error and i do not understand why, could anyone tell me, please? i am quite new to c programming.
    this is the c source code:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
    	int i;
    	int a[30000];
    	int p;
    	int c;
    	if (argc == 1){
    		printf("Usage: %s filename\n", argv[0]);
    	}
    	else {
    		FILE *file = fopen(argv[1], "r");
    		if (file == 0){
    			printf("Error: Could not open file\n");
    		}
    		else {
    			int length;
    			while(fgetc(file) != EOF){
    				length++;
    			}
    			for(i = 0; i < length; i++){
    				fseek(file,i,SEEK_SET);
    				switch(fgetc(file)){
    					case 60:
    						p--;
    						break;
    
    
    					case 62:
    						p++;
    						break;
    
    
    					case 43:
    						a[p] += 1;
    						break;
    
    
    					case 45:
    						a[p] -= 1;
    						break;
    
    
    					case 46:
    						putchar(a[p]);
    						break;
    
    
    					case 44:
    						a[p] = getchar();
    						break;
    
    
    					case 91:
    						c = i;
    						if(a[p] == 0){
    							for(i = i; i < length; i++){
    								fseek(file,i,SEEK_SET);
    								if(fgetc(file) == 93){
    									i = ftell(file);
    									break;
    								}
    							}
    						}
    						break;
    
    
    					case 93:
    						if(a[p] == 0){
    							break;
    						}
    						else {
    							c++;
    							i = c;
    							break;
    						}
    				}
    			}
    		}
    	}
    	return 0;
    }
    and this is the code i am trying t execute:
    Code:
     ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    You do not initialize length to zero.You should initialize it to zero.

    Same story for p .I do not know with which value you want to initialize it.Probably with zero.
    Last edited by std10093; 11-11-2012 at 11:48 AM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Quote Originally Posted by std10093 View Post
    You do not initialize length to zero.You should initialize it to zero.

    Same story for p .I do not know with which value you want to initialize it.Probably with zero.
    That did not quite solve it, I am not getting a segmentation error anymore, but I am not getting the expected output ("Hello world!"), either, just some gibberish.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    Can you please post the updated code?

    Also it would be nice to use '+' for example than the ascii code.

    Edit : Also at case 91 you manipulate the counter of the external loop.This is a common mistake.Are you sure you handle it correct?
    Last edited by std10093; 11-11-2012 at 12:09 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Quote Originally Posted by std10093 View Post
    Can you please post the updated code?

    Also it would be nice to use '+' for example than the ascii code.
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
    	int i;
    	int a[30000];
    	int p = 0;
    	int c = 0;
    	if (argc == 1){
    		printf("Usage: %s filename\n", argv[0]);
    	}
    	else {
    		FILE *file = fopen(argv[1], "r");
    		if (file == 0){
    			printf("Error: Could not open file\n");
    		}
    		else {
    			int length = 0;
    			while(fgetc(file) != EOF){
    				length++;
    			}
    			for(i = 0; i < length; i++){
    				fseek(file,i,SEEK_SET);
    				switch(fgetc(file)){
    					case 60:
    						p--;
    						break;
    
    
    					case 62:
    						p++;
    						break;
    
    
    					case 43:
    						a[p] += 1;
    						break;
    
    
    					case 45:
    						a[p] -= 1;
    						break;
    
    
    					case 46:
    						putchar(a[p]);
    						break;
    
    
    					case 44:
    						a[p] = getchar();
    						break;
    
    
    					case 91:
    						c = i;
    						if(a[p] == 0){
    							for(i = i; i < length; i++){
    								fseek(file,i,SEEK_SET);
    								if(fgetc(file) == 93){
    									i = ftell(file);
    									break;
    								}
    							}
    						}
    						break;
    
    
    					case 93:
    						if(a[p] == 0){
    							break;
    						}
    						else {
    							c++;
    							i = c;
    							break;
    						}
    				}
    			}
    		}
    	}
    	return 0;
    }
    this is the updated code.
    yes i am probably going to do that later, but does performance differ in any way or is it just the same when using ascii codes?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    The performance is affected by the heaviest operation of your code.This is that you read the file and then you read it again...The ascii code does not play a role in your's code performance.
    Aslo see my edit in previous post for case 91

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    am i not allowed to modify it, or is it just a bad way to do it?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    The question is not clear to me.You are allowed to modify your own code.You write it

    I suggest you take a second look at case 91.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    i meant, does modifying i cause any problems at all?

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    If the modifications made are correct,then the result will be correct.
    If the modifications made are incorrect,then the result will be incorrect.

    First make the code do what you want it to do and then improve performance.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    my problem is just, i can't get it to work. thats why i posted here in the first place. i need help. the loops characters (cases 91 and 93) wont work

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    987
    Try to break the problem down a little more, and explain what is your overall idea or algorithm. If you have a problem with cases 91 and 93, why not move them off into separate functions and then try to debug them separately.

    For example, what exactly do you want to do in case 91? Are you trying to seek ahead to find where 93 occurs? Does 93 represent an "REP" instruction? Why not try it like thisÍ

    Code:
         
    case BF_FOR:
        i = lookahead(file, BF_REP);
        break;
    // ...
    And then define what BF_FOR, BF_REP are and what lookahead should do and what values it should return, etc.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    Quote Originally Posted by c99tutorial View Post
    Try to break the problem down a little more, and explain what is your overall idea or algorithm. If you have a problem with cases 91 and 93, why not move them off into separate functions and then try to debug them separately.

    For example, what exactly do you want to do in case 91? Are you trying to seek ahead to find where 93 occurs? Does 93 represent an "REP" instruction? Why not try it like thisÍ

    Code:
         
    case BF_FOR:
        i = lookahead(file, BF_REP);
        break;
    // ...
    And then define what BF_FOR, BF_REP are and what lookahead should do and what values it should return, etc.
    What I am trying to do is, in case 91, look for the closest "]" character and continue the loop from there.
    In case 93, I am trying to repeat to run the code between the "[" before the "]" and the "]"

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,304
    This might not be causing any problems in your code, but I thought that I should mention it: If a file is opened and fails to open it returns "NULL". The actual value of NULL is implementation defined with its definition in the header stddef.

    Most times it will be defined as ((void *)0), but you can not guarantee it.

    Code:
    if (file == 0)
    
    /* should be */
    
    if (file == NULL)
    Last edited by Click_here; 11-11-2012 at 05:00 PM.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Click_here View Post
    Code:
    if (file == 0)
    
    /* should be */
    
    if (file == NULL)
    This is not a problem. A plain 0 in a pointer context is treated as a null pointer. Other equivalent forms are treated the same way, such as the expression (!file) which is equivalent to (file != 0), which is a valid comparison against a null pointer.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creation of a Command Language Interpreter
    By Sicilian_10 in forum C Programming
    Replies: 18
    Last Post: 04-16-2010, 06:36 AM
  2. What Language are you writing in?
    By C-Compiler in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 12-24-2008, 10:51 AM
  3. Replies: 0
    Last Post: 04-06-2007, 04:55 PM
  4. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  5. Register tranfer language interpreter in C
    By Nutcasey in forum C Programming
    Replies: 18
    Last Post: 01-06-2004, 12:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21