I need some advice about writing a parser (using C and Lex) that accepts a CFG and removes left recursion. Since the parser, needs to accept any string and a grammer, I am clueless about how to start. Although I am familiar with the algorithm to remove left recursoin (as mentioned here, I am clueless about how to start, and what will be the data structures involved. What is the best way to store the grammar, and the string. And how can I efficiently apply the algorithm. Please suggest. (Since this is homework, please do not provide me the code, instead any other help/pseudocode shall do) :)
Asked
Active
Viewed 334 times
1
-
This is homework assigned by a professor? As a homework problem it seems fine, but you shouldn't be handed such a problem unless your coursework has prepared to answer the question. The prof didn't cover parsing/tree building in your class, or didn't require it a pre-requisite? – Ira Baxter Feb 06 '12 at 05:00
-
Though parse tree building was taught (using the Dragon book) it didn't cover the advance examples. – hytriutucx Feb 09 '12 at 01:32
1 Answers
1
- You need to define a grammar P that describes grammars. This should be pretty easy.
- You need to build a parser for P. It needn't be complicated, because P won't be complicated. Trust me. You can use this method to hand-code a recursive-descent parser: https://stackoverflow.com/a/2336769/120163
- As P parses, it build up a data structure representing the grammar. Such a data structure needs to record, for each rule, its left side, its length, and the tokens the comprise the rule body.
- At this point, you've collected the rules in the data structure, and now you need to apply your left recursion algorithm by adjusting the rules.
- Finally you need to print out the rules; they should reappear in the syntax you chose for P.

Community
- 1
- 1

Ira Baxter
- 93,541
- 22
- 172
- 341