Created
May 13, 2013 19:23
-
-
Save AdamBrouwersHarries/5570761 to your computer and use it in GitHub Desktop.
Eliminating left recursion from example
This file contains 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
To begin with, the wikipedia left recursion algorithm is much eaiser to read: http://en.wikipedia.org/wiki/Left_recursion#Removing_indirect_left_recursion | |
So, we have the non-terminals A_1, and A_2: | |
A_1 -> A_2a | b | |
A_2 -> A_1c | ab | |
let's step through the algorithm: | |
i = 1 | |
j = 1 | |
j > i-1, break | |
do nothing - no direct left recursions | |
i = 2 | |
j = 1 | |
so, we know that A_j = 1, and A_i = 2 | |
at the moment: | |
A_1 -> A_2a | b | |
A_2 -> A_1c | ab | |
------------------------- | |
The algorithm wants to replace all instances where we have: | |
A_2 -> A_1$ (where $ is some string of terminals and non terminals) | |
with the direct version, ie, replacing A_2 -> A_1$ with the direct A_1. | |
This gives us: | |
A_2 -> A_2ac | bc | ab | |
To be extra clear (direct from A_1 bits in brackets): | |
A_2 -> (A_2a)c | (b)c | ab | |
j = 2 | |
j > i-1 break | |
We then need to eliminate the direct left recursions in A_2: | |
A_2 -> A_2ac | bc | ab | |
| | |
\_/ | |
A_2 -> bcA_3 | abA_3 | |
A_3 -> acA_3 | E | |
So, our completed grammar is: | |
A_1 -> A_2a | b | |
A_2 -> bcA_3 | abA_3 | |
A_3 -> acA_3 | E | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment