[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

285.0. "Context-Free Grammar Problem" by BEING::POSTPISCHIL () Tue May 21 1985 10:19

		                                                     m n
Problem:	Construct a context-free grammar which has language b c , where
		2/5 <= m/n <= 3/5.  It is allowed for m and n to be both zero.

		 m n
Note:		b c  is a string of m b's followed by n c's.


For those who have not been fortunate enough to be exposed to context-free
grammars, a short introduction follows the form-feed.


				-- edp

A context-free grammar consists of four things:

A set of terminal symbols.
A set of non-terminal symbols.
A start symbol.
A set of rules of the form x -> y, where x is a non-terminal symbol and y is
some string of non-terminal and terminal symbols.

A given string of non-terminal symbols is in the language iff it is possible
to derive it:

	The first line of the derivation is the start symbol.

	One line in the derivation may follow another if it is possible to
	obtain the second from the first by replacing some symbol in the first
	with the right side of a rule, where the left side of the rule is the
	symbol being replaced.

For example, a context-free grammar which has a language which contains all
strings of a's, b's, and c's, where there are an even number of b's, is:

	Terminal symbols:	{ a, b, c }
	Non-terminal symbols:	{ S, O, E }
	Start symbol:		S

	Rules:		      { S -> E,
				E -> lambda,
				E -> aE,
				E -> cE,
				E -> bO,
				O -> aO,
				O -> cO,
				O -> bE
			      }

Lambda represents the empty string.  These rules can be abbreviated:

      { S -> E,
	E -> lambda | aE | cE | bO,
	O -> aO | cO | bE
      }

A derivation of abcb is:

	S	(start symbol),
	E	(S is replaced by E through the rule S -> E),
	aE	(E replaced by aE),
	abO	(E replaced by bO),
	abcO	(O replaced by cO),
	abcbE	(O replaced by bE),
	abcb	(E replaced by the empty string).
T.RTitleUserPersonal
Name
DateLines
285.1METOO::YARBROUGHTue May 21 1985 12:354
I have a suggestion: instead of "lambda", why not write "empty"? It's shorter
and a great deal clearer. Why be obscure when there is no reason to do so?

Lynn Yarbrough
285.2ALIEN::POSTPISCHILWed May 22 1985 11:5521
Re .1:

The reason I wrote lambda is clear if you know the evolution:

	The actual Greek letter lambda is used when writing by hand or when the
	symbol is available.

	Since I was typing, but I was used to lambda, I typed "lambda".

I agree, "empty" would be more clear, but how about """"""?  By this, I do
not mean six quotes, but just two; the quoted quotes are doubled.  So the
rule S -> lambda would be written:

	S -> ""

Of course, it might be even better to write:

	S ->


				-- edp
285.3R2ME2::GILBERTSun May 26 1985 15:3049
Solution:
                                        m n
The following grammar has the language b c , where 2n/5 <= m <= 3n/5.

	S -> E
   	E -> bbEccccc
	E -> bbbEccccc
	E -> bEcc
	E -> lambda

Proof:

It's a simple matter to show by induction that the language contains
              m n
only strings b c , with 2n/5 <= m <= 3n/5, since, if (m,n) satistfies
the inequality, then so must (m+2,n+5), (m+3,n+5) and (m+1,n+2).

We must show that all strings satisfying the inequality are produced
by the grammar.  Given a string that satisfies the inequality, it can
be produced by applying the productions the following number of times:

	S -> E			1
   	E -> bbEccccc		(3n-5m - (3n mod 5))/5
	E -> bbbEccccc		(5m-2n - (3n mod 5))/5
	E -> bEcc		3n mod 5
	E -> lambda		1

It is a simple matter to show that this yields the correct number of
b's and c's, but we must still show that the expressions yield non-
negative integers.  Clearly, they yield integers, since 3n - (3n mod 5)
and 2n + (3n mod 5) are always multiples of 5.

From 3n >= 5m, we have 3n - (3n mod 5) >= 5m, and so 3n-5m - (3n mod 5)
is always >= 0.  Recalling that 2n + (3n mod 5) is always a multiple of 5,
and that 5m >= 2n, we have 5m >= 2n + (3n mod 5), so 5m-2n - (3n mod 5)
is always >=0, too.

Thus, the productions can be applied the described number of times to
                  m n
yield any string b c  that satisfies 2n/5 <= m <= 3n/5.  And since the
grammar generates only strings satisfying 2n/5 <= m <= 3n/5, it is a
solution to the problem.

[ Note that from this grammar, it's a simple matter to produce one
which doesn't also admit the production of lambda, by removing the
production E -> lambda, and including the productions: E -> bbccccc,
E -> bbbccccc, and E -> bcc. ]

					- Gilbert
285.4BEING::POSTPISCHILFri May 31 1985 14:5322
That is the answer I found.  I like it because it seemed rather difficult when
I started doing the problem, but the answer was really quite simple.


Generalization:		Construct a context-free grammar than generates all
			                     m n
			strings of the form b c , where p/q <= m/n <= r/s, where
			p and r are non-negative integers, and q and s are
			positive integers.

Conjecture:		The rules:

			    { S -> lambda,

			            u  v
			      S -> b Sc  | p/q <= u/v <= r/s and v <= max(q,s) }

			form such a grammar (with non-terminal { S }, terminals
			{ b, c}, and starting symbol S).


				-- edp