-1

I have a little problem with this exercise.

Given this grammar:

S -> aX | X
X -> aXb | b | eps

a) shows that it is ambiguous with a string

b) say what language captures the grammar

c) change the grammar and build a descendant parser

Solution by me:

a) I shows the ambiguous with the string 'ab':

- S -> aX -> ab
- S -> X -> aXb -> ab

b) the grammar captures this language:

L = {a^n b^n: n >= 0} U {a^n b^m: n=m+1, n,m >= 0} U {a^n b^m: m=n+1, n,m >= 0} 

c) my problem is change the grammar to build the parser. I try different grammars but they are ambiguous also. For example this:

 - G -> X$
   X -> aX' | b | eps
   X' -> XB | eps 

Can you help me to find a correct grammar for the exercise or give me an input?

Ely
  • 10,860
  • 4
  • 43
  • 64
Federico
  • 143
  • 1
  • 1
  • 10

1 Answers1

-1

As you.undoubtedly know, the language

S -> X | a S b

can be described as "an instance of X surrounded by an equal number of as and bs". X here is the base of the recursion.

As you have established, the target language has either an equal number of each letter, or one more of one letter. So we might ask, "What simple definition of X could produce this language?"

rici
  • 234,347
  • 28
  • 237
  • 341
  • A production could be X -> a | b. But with this production the grammar generates conflicts in parsing table for a top-down parsing. Do you think another production? – Federico Nov 03 '16 at 18:49
  • @federico: actually, it would have to be `X -> a | b | ε`; otherwise, balanced strings wouldn't be possible. That's unambiguous but not LL(k). (It's not LR(k) either.) An LL(1) grammar is trickier, but possible; think about deconstructing the language into a prefix and a maximum (finite) length suffix. – rici Nov 03 '16 at 20:59