Created
April 19, 2020 02:28
-
-
Save awave1/6b79be811bbc925fdd494552f567d1d7 to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
| /** | |
| * Grammar for this parser | |
| * | |
| * S -> E | |
| * E -> E '+' F | F | |
| * F -> number | '(' E ')' | |
| * | |
| * Without immediate left recursion | |
| * S -> E | |
| * E' -> '+' F E | λ | |
| * E -> F E' | |
| * F -> number | '(' E ')' | |
| */ | |
| // Given | |
| int lex(); | |
| void unlex(); | |
| void peek(); | |
| void match(); | |
| // to implement | |
| int S(); | |
| int E(); | |
| int E_prime(); | |
| int F(); | |
| void error(); | |
| int main(int argc, char const *argv[]) { | |
| S(); | |
| return 0; | |
| } | |
| int S() { return E(); } | |
| int E() { | |
| F(); | |
| E_prime(); | |
| } | |
| int E_prime() { | |
| char token; | |
| switch (token = lex()) { | |
| case '+': | |
| F(); | |
| E_prime(); | |
| break; | |
| default: | |
| // 'lambda' | |
| break; | |
| } | |
| } | |
| int F() { | |
| char token; | |
| switch (token = lex()) { | |
| case number: | |
| break; | |
| case '(': | |
| E(); | |
| if (lex() != ')') { | |
| error(); | |
| } | |
| break; | |
| default: | |
| error(); | |
| break; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment