it is based on regular expression programming so before I go over the important details, here is my eliminated left recursion grammar rules -
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')'
Okay so, when I typed my inputs as such as 'a', 'ab', 'abc', etc. my program won't be able to read more than one letter. Do you know what's up with this?
#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;
}
Aucun commentaire:
Enregistrer un commentaire