Skip to content

Instantly share code, notes, and snippets.

@caigen
Created March 6, 2014 15:02
Show Gist options
  • Select an option

  • Save caigen/9391623 to your computer and use it in GitHub Desktop.

Select an option

Save caigen/9391623 to your computer and use it in GitHub Desktop.
LL(2)
#include <iostream>
using namespace std;
/*
Grammar:
E -> \0 | F;
F -> d\0 | d+F;
*/
char look_first;
char look_second;
char s[100];
int cur = -1;
void init() {
cur = -1;
};
char next() {
cur++;
return s[cur];
}
bool f_rule();
bool e_rule() {
look_first = next();
if (look_first == '\0') {
return true;
}
/* back one step */
cur--;
return f_rule();
}
bool f_rule() {
/* LL(2) */
look_first = next();
look_second = next();
if (look_first == 'd') {
if (look_second == '\0') {
return true;
}
else if (look_second == '+') {
return f_rule();
}
else {
return false;
}
}
return false;
}
int main()
{
strcpy(s, "");
init();
cout << s << ":" << e_rule() << endl;
strcpy(s, "d");
init();
cout << s << ":" << e_rule() << endl;
strcpy(s, "+d");
init();
cout << s << ":" << e_rule() << endl;
strcpy(s, "d+");
init();
cout << s << ":" << e_rule() << endl;
strcpy(s, "d+d");
init();
cout << s << ":" << e_rule() << endl;
strcpy(s, "d+d+d+d");
init();
cout << s << ":" << e_rule() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment