hytriutucx
hytriutucx

Reputation: 1634

Suggestions for writing a Parser that reads a CFG and removes left recursion

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) :)

Upvotes: 1

Views: 341

Answers (1)

Ira Baxter
Ira Baxter

Reputation: 95324

  1. You need to define a grammar P that describes grammars. This should be pretty easy.
  2. 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
  3. 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.
  4. 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.
  5. Finally you need to print out the rules; they should reappear in the syntax you chose for P.

Upvotes: 1

Related Questions