So I have been assigned this project and have been giving it the old college try but I am a little lost on how to go about it. The idea is you’re given a txt file with a grammar in the form:
- V: S, A, B, C
- T: a, b
- S: S
- P:
- S -> aAaA|aABBA
- A -> AaaA|$
- B -> bB|bbC
- C -> B
These productions rules won’t be the only ones tested but that’s just an example.
So the first step is to read in the program.
The next step is to remove Lambda ($) productions.
and the final step is to remove unit productions.
I am … the way I’m removing lambda productions is not the best way I think.
Here’s how I’m doing it.
So first, use getline to read in the file.
Next use some loops to go through a file.
Now in an array, I’ve stored the non terminals that correspond to a rule with a lambda production, so keep that in mind.
So, while going through each character in each production, check if that character is the same as one in the array of the non terminals that denote lambda productions (excluding the index 0 because that’s the start of the production)
if you find a match, mark that index you’re on
so say your going through S.
S -> a (no problem)
s -> aA (Ok a bit of a problem)
instead of writing the A, don’t. skip over it and then using another loop, print out the remaining chunk of that production rule (that is, print out until you hit a bar)
so we get
S -> aaA
now draw a bar
S -> aaA|
now return the index to the first character in this chunk, here being a. From there, rewrite the character up through where you first hit the non terminal.
S -> aaA|aA
now continue looping through to find the next non terminal that’s a lambda
S -> aaA | aAaA (here we are)
draw a bar
S -> aaA | aAa |
move back to beginning, continue through
S -> aaA | aAa | aAaA
continue reading through the grammar and outputing each characters and repeat this process to get
S -> aaA | aAa | aAaA | aABB | aBBA | aABBA
at the end of all the code, I have two loops that check for the original bars in the grammar (i replace them with !’s when I did it) and then output at the end the form that has all lambda productions as lambdas giving
S -> aaA | aAa | aAaA | aa | aABB | aBBA | aABBA | aBB
I’m going to include the code here but, be warned,… it’s rough. It’s rough enough that I can’t actually get it to fall nicely under the codeing blocks in here so i’m going link it.
I appreciate any ideas on how you would undertake this project or if i’m doing something blatantly wrong.
The code currently throws out some errors (which i’m pretty sure are the cause of me reading some empty characters somewhere) but if you ignore them it’ll spit out to console the right thing… mostly.
Appreciate any help, thank you for taking the time to read all this mess.
Here is the architecture I would recommend:
Example meta grammar: