递归下降解析器中错误的成功解析

问题描述

我有以下代码。这是一个带有回溯的递归下降解析器。

/*\              /*\
|*|--------------|*|
|*| Grammar:     |*|
|*| A -> ( A ) B |*|
|*|   | [ A ] B  |*|
|*|   | n B      |*|
|*|   | B        |*|
|*|              |*|
|*| B -> + C B   |*|
|*|   | e        |*|
|*|              |*|
|*| C -> ( C ) D |*|
|*|   | [ C ] D  |*|
|*|   | n D      |*|
|*|   | D        |*|
|*|              |*|
|*| D -> * E D   |*|
|*|   | e        |*|
|*|              |*|
|*| E -> n       |*|
|*|              |*|
|*| e represents |*|
|*| epsilon      |*|
|*|--------------|*|
\*/            /*\*/

#include <stdbool.h>
#include <stdio.h>

char *next;

int terminal(char token)
{
    return (*next++ == token);
}

int A();
int B();
int C();
int D();
int E()
{
    printf("In E()\n");
    char *temp = next;
    return (next = temp,terminal('n'));
}

int A1()
{
    printf("In A1()\n");
    return terminal('(') && A() && terminal(')') && B();
}

int A2()
{
    printf("In A2()\n");
    return terminal('[') && A() && terminal(']') && B();
}

int A3()
{
    printf("In A3()\n");
    return terminal('n') && B();
}

int A4()
{
    printf("In A4()\n");
    return B();
}

int A()
{
    printf("In A()\n");
    char *temp = next;
    return  (next = temp,A1()) || 
            (next = temp,A2()) || 
            (next = temp,A3()) || 
            (next = temp,A4());
}

int B1()
{
    printf("In B1()\n");
    return terminal('+') && C() && B();
}

int B2()
{
    printf("In B2()\n");
    return true;
}

int B()
{
    printf("In B()\n");
    char *temp = next;
    return  (next = temp,B1()) || 
            (next = temp,B2());
}

int C1()
{
    printf("In C1()\n");
    return terminal('(') && C() && terminal(')') && D();
}

int C2()
{
    printf("In C2()\n");
    return terminal('[') && C() && terminal(']') && D();
}

int C3()
{
    printf("In C3()\n");
    return terminal('n') && D();
}

int C4()
{
    printf("In C4()\n");
    return D();
}

int C()
{
    printf("In C()\n");
    char *temp = next;
    return  (next = temp,C1()) || 
            (next = temp,C2()) || 
            (next = temp,C3()) || 
            (next = temp,C4());
}

int D1()
{
    printf("In D1()\n");
    return terminal('*') && E() && D();
}

// D2 and B2 are the same functions

int D()
{
    printf("In D()\n");
    char *temp = next;
    return  (next = temp,D1()) || 
            (next = temp,B2());
}

int main(void)
{
    char str[1000];
    printf("INPUT a string to be parsed : ");
    scanf("%s",&str);
    next = &str[0];
    (*next == '\0' || A() == 1) ? printf("Parsed Successfully\n") : printf("Parsed Unsuccessfully\n");
    //(A() == 1) ? printf("Parsed Successfully\n") : printf("Parsed Unsuccessfully\n");
    printf("\n");
}

字符串 3 = 3 被程序成功解析。该程序在控制台输出的 epsilon 转换基础上跳闸。我该如何解决

这是控制台输出

INPUT a string to be parsed : 3 = 3
In A()
In A1()
In A2()
In A3()
In A4()
In B()
In B1()
In B2()
Parsed Successfully


--------------------------------
Process exited after 2.888 seconds with return value 0
Press any key to continue . . .

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)