-
-
Save caigen/9391623 to your computer and use it in GitHub Desktop.
LL(2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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