为什么我的程序无法正确读取消除的左递归语法规则?C ++

CR9191

它基于正则表达式编程,因此在详细介绍之前,这是我消除的左递归语法规则-

RE -> S RE2
RE2 -> S RE2
     | EMPTY

S -> E S2
S2 -> '|' E S2
    | EMPTY

E -> F E2
E2 -> '*' E2
    | EMPTY

F -> a
   | b
   | c
   | d
   | '('RE')'

好了,所以,当我输入我的输入,如aababca|cab*等我的程序将不能超过一个字母阅读更多。你知道这是怎么回事吗?

#include <iostream>
#include <string>

using namespace std;

string input;
int index;

int nextChar();
void consume();
void match();
void RE();
void RE2();
void S();
void S2();
void E();
void E2();
void F();

int nextChar()
{
    return input[index];
}

void consume()
{
    index++;
}

void match(int c)
{
    if (c == nextChar())
        consume();
    else
        throw new exception("no");
}

void RE()
{
    S();
    RE2();
}

void RE2()
{
    if (nextChar() == 'a' || nextChar() == 'b' || nextChar() == 'c' || nextChar() == 'd' || nextChar() == '|' || nextChar() == '*' || nextChar() == '(' || nextChar() == ')')
    {
        S();
        RE2();
    }
    else
        ;
}

void S()
{
    E();
    S2();
}

void S2()
{
    if (nextChar() == 'a' || nextChar() == 'b' || nextChar() == 'c' || nextChar() == 'd' || nextChar() == '|' || nextChar() == '*' || nextChar() == '(' || nextChar() == ')')
    {
        match('|');
        E();
        S2();
    }
    else
        ;
}

void E()
{
    F();
    E2();
}

void E2()
{
    if (nextChar() == 'a' || nextChar() == 'b' || nextChar() == 'c' || nextChar() == 'd' || nextChar() == '|' || nextChar() == '*' || nextChar() == '(' || nextChar() == ')')
    {
        match('*');
        E2();
    }
    else
        ;
}

void F()
{
    if (nextChar() == 'a')
    {
        match('a');
    }
    else if (nextChar() == 'b')
    {
        match('b');
    }
    else if (nextChar() == 'c')
    {
        match('c');
    }
    else if (nextChar() == 'd')
    {
        match('d');
    }
    else if (nextChar() == ('(' && ')'))
    {
        match('(');
        RE();
        match(')');
    }
}

int main()
{
    cout << "Please enter a regular expression: ";
    getline(cin, input);

    input = input + "$";
    index = 0;

    try
    {
        RE();
        match('$');

        cout << endl;
        cout << "** Yes, this input is a valid regular expression. **";
        cout << endl << endl;
    }
    catch (...)
    {
        cout << endl;
        cout << "** Sorry, this input isn't a valid regular expession. **";
        cout << endl << endl;
    }

    return 0;
}
黑暗

我强烈建议学习如何使用调试器。然后,您可以逐行浏览并查看您的程序在做什么,甚至可以在throw调用上放置一个断点并查看堆栈跟踪。

在这种情况下,您if在E2中进行测试会检查很多字符,如果不是,则会引发错误*

if (nextChar() == 'a' || nextChar() == 'b' || nextChar() == 'c' || nextChar() == 'd' || nextChar() == '|' || nextChar() == '*' || nextChar() == '(' || nextChar() == ')')
{
    match('*');

这应该是

if (nextChar() == '*')
{
    match('*');

您的代码中多次出现此问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

数学表达式的语法规则(无左递归)

来自分类Dev

为什么antrl4无法将标记识别为语法规则的一部分?

来自分类Dev

什么是语法规则(在解析中)?

来自分类Dev

为什么我的BufferedReader无法正确读取我的文件?

来自分类常见问题

(JavaScript)语法规则

来自分类Dev

(JavaScript)语法规则

来自分类Dev

路径语法规则

来自分类Dev

语法规则实际上是正确的还是可能的?

来自分类Dev

此Haskell代码的语法规则是什么?

来自分类Dev

为什么我的程序无法读取用户输入?

来自分类Dev

为什么我的程序无法从文件中读取数据

来自分类Dev

我如何离开因子并消除左递归?

来自分类Dev

为什么我无法取消NOHUP进程?

来自分类Dev

ANTLR4 - 消除间接相互左递归的规则集

来自分类Dev

为什么此C程序无法正确运行?

来自分类Dev

为什么此C程序无法正确运行?

来自分类Dev

为什么我的C程序的输出不正确?

来自分类Dev

为什么DataInputStream无法正确读取char?

来自分类Dev

为什么自上而下的解析器无法处理左递归?

来自分类Dev

为什么我在 Ruby 中使用递归时无法得到正确答案?

来自分类Dev

为什么我的if块无法在我的C程序中执行?

来自分类Dev

消除左递归

来自分类Dev

消除左递归

来自分类Dev

为什么我的C程序无法检测到特殊字符?

来自分类Dev

c程序无法正确读取文件

来自分类Dev

为什么scanf无法读取我的输入?

来自分类Dev

为什么我无法读取只读文件?

来自分类Dev

为什么无法读取我的索引?

来自分类Dev

为什么我的char *无法正确通过?