context-free matching of characters

51 views Asked by At

Given the three symbols: "(" ")" and ";"

how can I create production rules of a context free grammar for S expressions, which meet the following criteria:

  • The whole expression is nested in brackets, which means it starts with "(" and ends with ")".

If the expression is read from left to right, then the amount of open brackets on any position of the expression (except for the last one) is greater than the amount of closed brackets. At the end of the expression the amount of open brackets should be equal to the closed brackets.

  • Brackets can be nested in any way.
  • Closing brackets must be separated from opening brackets with ";".
  • The most inner brackets can contain a ";" OR remain empty.
  • ";" must not occur consecutively.

Also upper case letters need to be used for any introduced Non-terminal letters.

Strings contained in the grammar:

()
(;)
(((;)))
((;);((;)))
(();(()))

Strings NOT contained in the grammar.

;
(;;)
(()())
());(()
();()
;()
ε

I have tried using the following grammar, but I get some faulty values from it, any input/correction is appreciated.

S --> (BAB)

A --> ; | ε | (A)

B --> B | A | A);(A
1

There are 1 answers

0
Hassan Abbasi On BEST ANSWER

An Answer:

S->(T)|(;)|()
T->(T)|T;T|()|(;)