1

I have the following production rule

E -> (EX)|Y

X -> vE|^E|>E

Y -> -E|p|q|r

and I have the following code

int i;
int j;
int count;
char l;

void match(char t);
void E();
void X();
void Y();

void match(char t) 
{ 
    if (l == t) { 
        count += 1;
    } 
    else{
        printf("Error"); 
    }
} 

void E()
{
    if(l == '('){
        match('(');
        E();
        X();
        if(l == ')'){
            match(')');
            return;
        }
    }
    else{
        Y();
    }
    return;
}

void Y()
{
    if(l == '-'){
        match('-');
        E();
    }
    else if(l == 'p'){
        match('p');
        return;
    }
    else if(l == 'q'){
        match('q');
        return;
    }
    else if(l == 'r'){
        match('r');
        return;
    }
}

void X()
{
    if(l == 'v'){
        match('v');
        E();
    }
    else if(l == '^'){
        match('^');
        E();
    }
    else if(l == '>'){
        match('>');
        E();
    }
    return;
}

int parse(char* g){
  count = 0;
  //int k = strlen(g);
  l = g[count];
  //sprintf("%c",l);
  E();
  if(l == '$'){
    printf("Parsing successful");
    return 1;
  }
  else{
    printf("Parsing unsuccessful");
    return 0;
  }
} 

which doesnt seem to work. The program crashes without printing anything. What is the problem in the code? The code runs inititally but nothing happens and then the program just exits. I dont think there is a segmentation fault but could someone please advise and help

1
  • 1
    It looks weird that l is not updated during the recursion. Commented Oct 30, 2020 at 14:59

1 Answer 1

1

Your code never updates l. If I understand your code correctly, you’d want to update l in match at the very least:

void match(char t) 
{ 
    if (l == t) { 
        count += 1;
        l = g[count];
    } 
    else {
        printf("Error"); 
    }
} 

For this to work you need to make g global` (or, ideally, you’d remove all global variables and encapsulate the state of your parser into a structure that you pass through to your parsing functions).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.