简体   繁体   English

C中的递归下降解析器-回溯时出错

[英]Recursive Descent Parser In C - Error while backtracking

Implementing a recursive descent parser for the given grammar with the sample input '(a)'. 使用样本输入“(a)”为给定的语法实现递归下降解析器。
The output of this program is to check if the input read by the user is parsed successfully or not. 该程序的输出是检查用户读取的输入是否已成功解析。 As of how the flow of the program should go for this particular input... 至于该特定输入的程序流应如何...

main() calls the start symbol E() -> T() -> T1() [ which evaluates to false ] hence tracing back to its parent function T() which now calls T2() (which also evaluates to false), hence T() -> T3() -> E() -> E1() -> T() -> T1() (evaluates to true). main()调用开始符号E() -> T() -> T1() [其结果为false],从而追溯到其父函数T() ,该父函数现在调用T2() (其结果也为false),因此T() -> T3() -> E() -> E1() -> T() -> T1() (评估为true)。

Eventually E() should also be true and the last condition termial(')') in the statement return termial('(') && E() && termial(')'); 最终E()也应该为true,并且语句中的最后一个条件termial(')') return termial('(') && E() && termial(')'); from the function T3() should be performed with ')' as the value for token . 来自函数T3()应使用')'作为token的值。 However, it turns out that token == '(' still and hence the output of the program results in 但是,事实证明token == '('仍然存在,因此程序的输出结果为

Parsed Unsuccessfully 解析失败

Not really sure why token is not equal to ')' 不太确定为什么token不等于')'

 //Grammar :
 //E -> T | T + E
 //T -> a | a * T | (E)
 //
 //Input :
 //(a)

#include <stdio.h>

char *next;

int E();
int E1();
int E2();
int T();
int T1();
int T2();
int T3();

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

int E1(){
    return T();
}

int E2(){
    return T() && termial('+') && E();
}

int E(){
    char *temp = next;
    return (next = temp, E1()) || (next = temp, E2());
}

int T1(){
    return termial('a');
}

int T2(){
    return termial('a') && termial('*') && T();
}

int T3(){
    return termial('(') && E() && termial(')');
}

int T(){
    char *temp = next;
    return (next = temp, T1()) || (next = temp, T2()), (next = temp, T3());
}


int main(int argc, const char * argv[]) {
    char str[10];
    int result;
    printf("INPUT a string to be parsed : ");
    scanf("%s", str);
    next = &str[0];
    result = E();
    result == 1 ? printf("Parsed Successfully") : printf("Parsed Unsuccessfully");
    printf("\n");
    return 0;
}

In T() : T()

int T(){
  char *temp = next;
  return (next = temp, T1()) || (next = temp, T2()), (next = temp, T3());
}

(next = temp, T3()) is always called (there's no short-circuit at the comma before it), and will return false in most of your cases, causing all sorts of failures when T1() or T2() succeed and should have ended the function with success. (next = temp, T3()) 始终被调用(逗号之前没有短路),并且在大多数情况下都将返回false ,从而在T1()T2()成功执行并导致各种故障时应该以成功结束了功能。

Change that line to: 将该行更改为:

return (next = temp, T1()) || (next = temp, T2()) || (next = temp, T3());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM