Search code examples
recursioncontext-free-grammarleft-recursion

how to make a context free grammar with no left recursion to include a left recursion (without changing the language of the grammar)?


assume we have a context free grammar , if it is in LL[1] , then it has only right Associative ! but let's say i want to make this context free grammar have a left Associative , then it will not stay in LL[1] , (which is okay) i figured that in order to make a context free grammar have a left Associative i should make it have left recursion . is there is a way to include left recursion to a context free grammar without changing the language of the grammar ? for example if we have this context free grammar :

1: S -> sum ( ELIST )
2: ELIST -> E , ELIST
3: ELIST -> E
4: E -> num
5: E -> id
6: E -> S

how to make this include a left recursion so the operator "," will now be left Associative ?


Solution

  • Change your second production to:

    2: ELIST -> ELIST , E

    This does not change the language, only the way the language is parsed.

    This modification should work with any expression grammar written in cascading precedence style. But it is not a transformation which can be applied to any grammar whatsoever.