views:

338

answers:

3

My teacher has given me two bnf grammars:

A ::= 'd' | A 'e' A | A 'f' A
B ::= 'd' | B B 'e' | B B 'f'

and four strings to match with them:

  • dffd
  • dddefddfe
  • dedf
  • deded

I've figured out two of them, but the other two have me stumped. I don't want anyone to tell me the answers, but if someone could give me some hints as to where I'm going wrong it would be much appreciated.

A: 

My advice would be to draw a finite automata or state diagram for yourself before you write any code. Do it out by hand with a pencil and paper first.

duffymo
+1  A: 

Hmmm...

By induction, all matches must have an odd number of characters. So neither of the 4 character strings can be a hit...


Oh wait. I just noticed the 'Y' in the first rule. Do we know what that is? It could break my argument right open...

dmckee
Thanks, I was mostly focusing on those as I though they would be easier; once I skipped them I got the other two. I guess I'll just ask the teacher about those.
That was a typo.
+2  A: 

This is a Context-Free grammar, so you should be looking to draw a parse tree. You can then see which non-terminal symbol leads to which yielded string. These grammars are fairly simple, so drawing a parse tree should be fairly easy to do by hand.

sykora