Why the need for terminals? Is my solution sufficient enough?
Asked Answered
P

1

2

I'm trying to get my head around context free grammars and I think I'm close. What is baffling me is this one question (I'm doing practise questions as I have an exam in a month's time):

enter image description here

I've come up with this language but I believe it's wrong.

S --> aSb | A | B
A --> aA | Σ
B --> bB | Σ

Apparently this is the correct solution:

S --> aSb | aA | bB
A --> aA | Σ
B --> bB | Σ

What I don't quite understand is why we have S --> aSb | aA | bB and not just S --> aSb | A | B. What is the need for the terminals? Can't I just call A instead and grab my terminals that way?

Testing to see if I can generate the string: aaabbbb

S --> aSb --> aaSbb --> aaaSbbb --> aaaBbbb --> aaabbbb

I believe I generate the string correctly, but I'm not quite sure. I'm telling myself that the reason for S --> aSb | aA | bB is that if we start with aA and then replace A with a, we have two a's which gives us our correct string as they're not equal, this can be done with b as well. Any advice is greatly appreciated.

Into the Tuple (G-4-tuple)

V (None terminals) = {A, B}
Σ (Terminals) = {a, b}
P = { } // not quite sure how to express my solution in R? Would I have to use a test string to do so?
S = A

Patricio answered 13/4, 2013 at 15:5 Comment(2)
appreciate it, thanks!Patricio
ok read know updated with some standard terminology in theory..Decoy
D
4

First:

  1. Σ means language symbols. in your language Σ = {a, b}
  2. ^ means null symbols (it is theoretical, ^ is not member of any language symbol)
  3. ε means empty string (it is theoretical, ε can be a member of some language)

See ^ symbol means nothing but we use it just for theoretical purpose, like infinity symbol we uses in mathematics(really no number is but we use it to understand, to proof some theorems) similarly ^ is nothing but we use it.
this point not written in any book, I am writing it to explain/for understanding point of view. The subject more belongs to theoretical and math and I am from computer science.

As you says your grammar is L = {am bn | m != n}. Suppose if productions are as follows:
First:

S --> aSb | A | B
A --> aA | Σ
B --> bB | Σ

It means.(very rare book may use Σ in grammar rules)

S --> aSb | A | B
A --> aA | a | b
B --> bB | a | b

I replaced Σ by a | b (a, b language symbols).

This grammar can generates a string of equal numbers of symbols a and symbol b(an bn). How it can generate an bn? See below an example derivation:

S ---> aSb ---> aAb ---> aaAb ---> aabb  
   ^        ^        ^           ^
rule-1      S-->A    A--> aA     A --> b

But these kind of strings are not possible in language L because m != n.

Second:
For the same reason production rules S --> aSb | aA | bB is also not correct grammar if A --> aA | Σ or B --> bB | Σ are in grammar.

I think in second grammar you mean:

S --> aSb | aA | bB
A --> aA  | ^
B --> bB  | ^ 

Then this is correct grammar for language L = {am bn | m != n}. Because using:

S --> aSb  

you can only generate equal numbers of a' and b and by replacing S either by aA or by bB you make a sentential form in which unequal numbers of a and b symbols are present and that can't convert back to generate a string of type an bn. (since A doesn't generates b and B doesn't generates a).

Third:
But usually we write grammar rules like:

S --> aSb | A | B
A --> aA | a
B --> bB | b

Both forms are equivalent (generate same language L = {am bn | m != n}) because once you convert S into either A or B you have to generate at-least one a or b (or more) respectively and thus constraint m != n holds.

Remember proofing, whether two grammars are equivalent or not is undecidable problem. We can't prove it by algorithm (but logically possible, that works because we are human being having brain better then processor :P :) ).

Fourth:
At the end I would also like to add, Grammar:

S --> aSb | A | B
A --> aA | ^
B --> bB | ^

doesn't produces L = {am bn | m != n} because we can generate an bn for example:

S ---> aSb ---> aAb ---> ab
                     ^
                     A --> ^ 

Grammar in formal languages

Any class of formal languages can be represented by a formal Grammar consisting of the four-tuple (S, V, Σ, P). (note a Grammar or an automata both are finite representation weather language is finite or infinite: Check figures one & two).

  1. Σ: Finite set of language symbols.
    In grammar we commonly call it finite set of terminals (in contrast of variables V). Language symbols or terminals are thing, using which language strings (sentences) are constructed. In your example set of terminals Σ is {a, b}. In natural language you can correlate terminals with vocabulary or dictionary words.
    Natural language means what we speak Hindi, English

  2. V: Finite set of Non-terminals.
    Non-terminal or say 'variable', should always participate in grammar production rules. (otherwise the variable counts in useless variables, that is a variable that doesn't derives terminals or nothing).
    See: 'ultimate aim of grammar is to produce language's strings in correct form hence every variable should be useful in some way.
    In natural language you can correlate variable set with Noun/Verbs/Tens that defined a specific semantical property of an language (like Verb means eating/sleeping, Noun means he/she/Xiy etc).
    Note: One can find in some books V ∩ Σ = ∅ that means variables are not terminals.

  3. S: Start Variable. (S ∈ V)
    S is a special variable symbol, that is called 'Start Symbol'. We can only consider a string in language of grammar L(G) if it can be derived from Start variable S. If a string can not be derived from S (even if its consist of language symbols Σ) then string will not be consider in the language of grammar( actually that string belongs to 'complement language' of L(G), we writes complement language L' = Σ* - L(G) , Check: "the complement language in case of regular language")

  4. P: Finite set of Production Rules.
    Production Rules defines replacement rules in the from α --> β, that means during the derivation of a string from S, from grammar rules at any time α (lhs) can be replaced by β (rhs).(this is similar to Noun can be replace by he,she or Xiy, and Verb can be replace by eating, sleeping etc in natural language.
    Production rules defines formation rules of language sentences. Formal language are similar to Natural language having a pattern that is certain thing can occurs in certain form--that we call syntax in programming language. And because of this ability of grammar, grammar use for syntax checking called parse).
    Note: In α --> β, α and β both are consists of language symbols and terminals (V U Σ)* with a constraint that in α their must be at-least one variable. (as we can replace only a string contain variable by rhs of rule. a terminal can't replace by other terminal or we can say a sentence can't be replaced by other sentence)
    Remember: There is two form Sentential Form and Sentence of a string:

    • Sentence: if all symbols are terminals (sentence can be either in L(G) or in complement language L' = Σ* - L)
    • Sentential: if any symbol is variable (not a language string but derivation string)

From @MAV (Thanks!!):
To represent grammar of above language L = {am bn | m != n}, 4-tuple are :

V = {S, A, B}
Σ = {a, b}
P = {S --> aSb | A | B, A --> aA | a, B --> bB | a}
S = S

note: Generally I use P for Production rules, your book may use R for rules

Terminology uses in theory of formal languages and automate

  • Capital letters are uses for variables e.g. S, A, B in grammar construction.
  • Small letter from start uses for terminals(language symbols) for example a, b. (some time numbers like 0, 1 uses. Also ^ is null symbol).
  • Small letters form last uses for string of terminals z, y, w, x (for example you can find these notations in pumping lemma, symbols use for language string or sub strings).
  • α, β, γ for Sentential forms.
  • Σ for language symbols.
  • Γ for input or output tap symbol, other then language symbols.
  • ^ for null symbol, # or Symbol for blank symbol in Turing machine and PDA (^, #, are other then language symbols.
  • ε uses for empty string (can be a part of language string for example { } is empty body in C language, you can write while(1); or while(1){ } both are valid see here I have defined a valid program with empty sentences).
  • means empty set in set theory.
  • Φ, Ψ uses for substring in Sentential forms.

Note: means set is empty, ε means string is empty, ^ means none symbol (don't mix in theory, all are different in semantic)

There is no rules I know about symbol notation, but these are commonly used terminology once can find in most standard books I observed during study.

Next post: Tips for writing Context free grammar

Decoy answered 13/4, 2013 at 16:21 Comment(11)
Wow. I can't tell you how much this has helped me out. Thank you so much!Patricio
The only thing I ask is, how do we represent this in a as a 4-tuple G = (V; E; R; S)?Patricio
sure, really appreciate all the help!Patricio
@Xiy V is your nonterminals = {S, A, B}. E is your terminals = {a, b}. R is all your rules = {S --> aSb | A | B, A --> aA | a, B --> bB | a} and S is your start nonterminal = S. Hope this makes sense. :)Aerobatics
@GrijeshChauhan Okay. I just though my edit had been rejected for some reason and wanted to give Xiy an answer to his question. :) I like your answer.Aerobatics
You guys are awesome, it's all clicked now! Lastly (sorry!) what happens if we have to construct a language with constrains that are based on numbers like so: d.pr/i/jr77 is it just the string we check for 3 and 2 amounts?Patricio
@Xiy you always generates three a for two b . like S --> aaaSbb .....wait I am writing.Decoy
I see, brilliant, thank you both for all your help. I'm really grateful.Patricio
@Aerobatics you seems to be online now read the answer I updated you may find interesting....Decoy
@GrijeshChauhan I have read it and I think it is a good answer. There is more than enough information to answer the question and help whoever sees this question in the future. :)Aerobatics
@Aerobatics Thanks :) ..yes I hope!.. Actually I am getting e-mail from students that they like my answers and so I always add information from fundamental..Decoy

© 2022 - 2024 — McMap. All rights reserved.