0

I'm currently having problems solving this kind of a conflict in a grammar:

A -> (A)A'
A -> 0A'
A -> 1A'
A'-> NAND A A'
A'-> eps

The problem is that FIRST of A' is NAND - as well as a part of its FOLLOW set. And since there's the A' -> eps rule, that creates a conflict. Is there any way to resolve this conflict? Substitution or factorization don't yield any results - so I guess that I'm missing something.

prkist
  • 431
  • 5
  • 13
  • How did you conclude NAND was in FOLLOW(A')? – Scott Hunter May 11 '15 at 19:56
  • A'->NAND A A'; A' -> eps; what follows after this rule is FIRST(A'), which is NAND. – prkist May 11 '15 at 20:06
  • What partial derivation results in ... A' NAND ... ? – Scott Hunter May 11 '15 at 21:05
  • I can't actually find the partial derivation, although I've been following these rules: http://www.jambe.co.nz/UNI/FirstAndFollowSets.html ("If there is a production A → aB, then everything in FOLLOW(A) is in FOLLOW(B)". A can be followed by a NAND (NAND A A' -> NAND A NAND A'). As NAND can follow an A, it can follow B as there's a rule in this form - e.g. A -> 0A' – prkist May 11 '15 at 21:20
  • And talking about that, there's the partial derivation: NAND A A' -> NAND 0 A' A' -> NAND 0 A' NAND A A' – prkist May 11 '15 at 21:37

1 Answers1

2

The problem is that your grammar is ambiguous. For example 0 NAND 0 NAND 0 has at least two leftmost derivations:

A => 0 A' => 0 NAND A A' => 0 NAND 0 A' A' => 0 NAND 0 NAND A A' A' =>
  => 0 NAND 0 NAND 0 A' A' A' =>* 0 NAND 0 NAND 0

A => 0 A' => 0 NAND A A' => 0 NAND 0 A' A' => 0 NAND 0 A' => 
  => 0 NAND 0 NAND A A' => 0 NAND 0 NAND 0 A' A' =>* 0 NAND 0 NAND 0

rewriting it with ELL syntax it's easier (for me) to see that there are two possible recursions, with A in NAND A, or with the star (A' in the original grammar).

A -> ( '(' A ')' | 0 | 1 ) ( NAND A )*

you could solve the ambiguity making the star the only choice to add NANDs, and using '(' A ')' | 0 | 1 as its operands:

A -> X ( NAND X )*
X -> '(' A ')' | 0 | 1
1010
  • 1,779
  • 17
  • 27