1

The problem is:

a. Write a directly recursive EBNF rule named mp that describes all symbols that have matching parentheses: (), ()()(), ()(()()), and ((())())(()(()))(). It should not recognize (, ())(, or (()() as legal.
b. Write a tabular proof and its derivation tree showing how ()(()()) is recognized as legal.

So far I've thought of one plausible solution. I am not sure if it is correct or if I am missing something.

<mp> ::= "" | ( <mp> "(" <mp> ")" ) 

Any suggestions?

pandorym
  • 475
  • 4
  • 14
Vlad
  • 2,739
  • 7
  • 49
  • 100

1 Answers1

1

But, before it closes, here's what I'd have:

mp := ( mp ) mp
mp := ''

Your second example, with n >= 0 and m >= 0 is not in BNF. Your first one should be acceptable however.

Here's my derivation tree for ()(()()):

mp
( mp ) mp
( '' ) mp
()( mp ) mp
()( mp ) ''
()(( mp ) mp )
()(( '' ) mp )
()(()( mp ) mp )
()(()( mp ) '' )
()(()( '' ))
()(()()) 
pandorym
  • 475
  • 4
  • 14
  • I would put both empty string and the rule in one row. – Vlad Mar 15 '16 at 11:42
  • 1
    I totally agree, I only separated the two for readability. When you're working with later compiler concepts, having each production rule on it's own line has value. You understand that they have the same semantic value though? Whether it is on one line or two? – pandorym Mar 15 '16 at 11:45
  • Well I though that each rule must be in one line, compact. But now I see I can keep both lines. Didn't know that. – Vlad Mar 15 '16 at 11:48
  • You can copy the solution for b, to your answer and I will accept it as correct. Will delete mine. – Vlad Mar 15 '16 at 11:53
  • Just saw your comment, I was filing in my derivation to `pt b` based on my rule. – pandorym Mar 15 '16 at 11:56