Is ε terminal in context-free? - automation

R: S ---> aSb
S ---> SS
S ---> ε
How should I write the grammar for these expressions?
Is it true to writing like this?
G = ({S}, {a, b}, {S ---> aSb, S ---> SS, S ---> ε}, {S})
or like this (adding epsilon to terminals):
G = ({S}, {a, b, ε}, {S ---> aSb, S ---> SS, S ---> ε}, {S})
which is the correct one?

ε is a way to make a zero- length sequence visible. It is not a grammar symbol.
The actual production is: S → — that is, S can produce nothing — but the invisibility of nothing makes it hard to read. So we usually write ε, which is more legible. You should read it as nothing, though.

Related

How can I derivate these Grammar

G = (V={S,X,Y}, T={0,1,2},P,S)
S -> 0X1
X ->S | 00S2 | Y | ε
Y ->X | 1
The Problem is I don´t know how to derivate numbers..
How can I derivate this here:
00111 ∈ L(G)
And here I have to give a derivation three:
0000121 ∈ L(G)
To do a derivation you start with the start symbol (in this case S) which is the fourth item in the grammar tuple). You then apply the production rules (P) in whatever order seems appropriate to you.
A production like:
X → S | 00S2 | Y | ε
means that you can replace an X with
S, or
00S2, or
Y, or
nothing.
In other words, you read production rules as follows:
→ means "can be replaced with".
| means "or"
ε means "nothing" (Replacing a symbol with nothing means deleting it from the current string.)
Everything else is just a possible symbol in the string. You keep doing replacements, one at a time, until you reach the string you are trying to derive.
Here's a quick example:
S
→ 0X1 (using S → 0X1)
→ 000S21 (using X → 00S2)
→ 0000X121 (using S → 0X1)
→ 0000121 (using X → ε)
That's it. Nothing complicated at all. Just a bunch of search and replace operations. (You don't need to replace the first occurrence, if there is more than one possibility. You can do the replacements in any order you like. But it's convenient to be systematic.)

Formal grammar and arity

I have the following grammar:
S --> LR .
L --> aL .
R --> bR .
This grammar generates the language a^n b^k, where n,k > 0.
I want a grammar that generates the language a^n b^n where n>0, so
my goal is to obtain a grammar in order to ensure that the number of a is always equal of b, but still keeping the non-terminals L and R.
Is there a way to do this?
In a.context free grammar, the derivations of L and R in S → L R are independent of each other. That is what "context free" means: the derivation of a non-terminal is not affected by the context in which the non-terminal occurs.
So if you want a grammar in which L and R must derive strings of equal length, it will have to be a context-sensitive grammar. No context-free grammar can do that.
Of course, there is a simple CFG for the language:
S →
S → a S b

Formal Languages - Grammar

I am taking a Formal Languages and Computability class and am having a little trouble understanding the concept of grammar. One of my assignment questions is this:
Take ∑ = {a,b}, and let na(w) and nb(w) denote the number of a's and b's in the string w, respectively. Then the grammar G with productions:
S -> SS
S -> λ
S -> aSb
S -> bSa
generates the language L = {w: na(w) = nb(w)}.
1) The language in the example contains an empty string. Modify the given grammar so that it generates L - {λ}.
I am thinking that I should modify the condition of L, something like:
L = {w: na(w) = nb(w), na, nb > 0}
That way, we indicate that the string is never empty.
2) Modify the grammar in the example so that it will generate L ∪ {anbn+1: n >= 0}.
I am not sure on how to do this one. Should that mean I make one more condition in the grammar, adding something like S -> aSbb?
Any explanation about these two questions would be greatly appreciated. I'm still trying to figure these grammar stuff out so I am not sure about my answers.
1) The question is about modifying the grammar to obtain a new language; so don't modify directly the language…
Your grammar generates the empty word because of the production:
S -> λ
So you could think of removing this production altogether. This yields the following grammar:
S -> SS
S -> aSb
S -> bSa
Unfortunately, this grammar doesn't generate a language (a bit like in induction, it misses an initial: there are no productions that only consist of terminals). To fix this, add the following productions:
S -> ab
S -> ba
2) Don't randomly try to add production rules in the hope that it's going to work. Here you want a's followed by b's. So the production rule
S -> bSa
must certainly disappear. Also, the rule
S -> SS
would produce, e.g., abab (try to see how this is obtained). So we'll have to remove it too. We're left with:
S -> λ
S -> aSb
Now this grammar generates:
λ
ab
aabb
aaabbb
etc. That's not bad at all! To get an extra trailing b, we could create a new non-terminal, say T, replace our current S by T, and add that trailing b in S:
T -> λ
T -> aTb
S -> Tb
I know that this is homework; I gave you the solutions to your homework: that's because, from the way you asked your question, it seems you're completely lost. I hope this answer will help you get on the right path!

"subst" where indices to be equated also use subst

I'm stuck on the following. I have the derivation of a pi calculus transition that takes place in some context Γ, plus a proof that Γ ≡ Γ′. I would like to coerce the derivation into a transition in Γ′, using subst. The details of the setting are mostly unimportant, as usual.
module Temp where
open import Data.Nat as Nat using (_+_) renaming (ℕ to Cxt)
open import Function
open import Relation.Binary.PropositionalEquality
data _⟿ (Γ : Cxt) : Set where
bound : Γ ⟿
nonBound : Γ ⟿
target : ∀ {Γ} → Γ ⟿ → Cxt
target {Γ} bound = Γ + 1
target {Γ} nonBound = Γ
data Proc (Γ : Cxt) : Set where
data _—[_]→_ {Γ} : Proc Γ → (a : Γ ⟿) → Proc (target a) → Set where
-- Use a proof that Γ ≡ Γ′ to coerce a transition in Γ to one in Γ'.
coerce : ∀ {Γ Γ′} {P : Proc Γ} {a R} → P —[ a ]→ R → (q : Γ ≡ Γ′) →
subst Proc q P —[ subst _⟿ q a ]→ subst (Proc ∘ target) {!!} R
coerce E refl = {!!}
It's easy enough to coerce both the source P of the transition, and the action a which appears as a label on the transition. The problem is the target R of the transition, whose type depends on a. In the coerced transition, I use subst to coerce a from Γ ⟿ to Γ' ⟿. Naively, I would like to then also use subst to change the type of R from Proc (target a) to Proc (target (subst _⟿ q a) by showing that the Proc indices are equal. However, by its very nature subst _⟿ q a has a distinct type from a, so I'm unsure how to do this. Maybe I need to use the proof of Γ ≡ Γ′ again or somehow "undo" the inner subst to unify the types.
Is what I'm trying to do reasonable? If so, how do I coerce R given the heterogeneity?
Generally, when you want to compare two things of different types (that can become equal given suitable reduction), you would want to use heterogeneous equality.
The proof that subst doesn't actually change the value cannot be done with usual (propositional) equality, because a and subst P q a have different types. However, once you know that q = refl, you can reduce those expressions enough so that their types now match. This can be expressed using heterogeneous equality:
data _≅_ {ℓ} {A : Set ℓ} (x : A) : {B : Set ℓ} → B → Set ℓ where
refl : x ≅ x
This allows you to even express the fact that a ≅ subst P q a. When you then pattern match on q, the goal reduces to simple a ≅ a, which can then be proven by reflexivity:
≡-subst-removable : ∀ {a p} {A : Set a}
(P : A → Set p) {x y} (eq : x ≡ y) z →
P.subst P eq z ≅ z
≡-subst-removable P refl z = refl
So, the first instinct is to change the last subst to heterogeneous subst (from Relation.Binary.HeterogeneousEquality) and use the proof ≡-subst-removable. This almost works; the problem is that this subst cannot capture the change from a : Γ ⟿ to Γ′ ⟿.
As far as I know, the standard library doesn't provide any alternate substs that capture this interaction. The simple solution is to write the combinator ourselves:
subst′ : ∀ {Γ Γ′} {a} (q : Γ ≡ Γ′) (R : Proc (target a)) →
Proc (target (subst _⟿ q a))
subst′ refl R = R
As you noted in the comments, this can be further generalized. However, unless you expect to do a lot of proofs of this kind, this generalization is not very useful since the problem is fairly rare and for the simpler cases, heterogeneous equality usually does the work.

Specifying language for a given grammar

DFA problem : Write a complete grammar for L, including the quadruple and the production rules
L ={x: ∃y ∈ {a, b}* : x = ay}
Answer:
G={{S, A}, {a, b}, S, P}
P: S => aA
A => aA | bA | λ
My question is :
Why there is λ for A, but there is no λ for S?
From the language definition, it is any string that begins with an a and contains only a's and b's , but why in the answer A => bA. Does not it mean that the string starts with b if it is A => bA?
Thank you so much
1. Why there is λ for A, but there is no λ for S?
λ nul can be derived from A to convert a sentimental from into sentence. Additionally according to language statement prefix sub-string y ∈ {a, b}* can be nul (a empty string) e.g. "a" is a string belongs to the language. If y contain any symbol then length of language will be more than one.
S doesn't derive λ nul because empty (or say nul string) is not in language. The smallest string in language is single "a".
2. From the language definition, it is any string that begins with an a and contains only a's and b's , but why in the answer A => bA. Does not it mean that the string starts with b if it is A => bA?
Note only strings those can derived from start variable S are included in language of grammar. You can't start derivation from A (that is not start variable). And if you start a derivation from S your string will always start with a symbol.
I suggest you to read: "Why the need for terminals? Is my solution sufficient enough?" Where I written about basic definition of formal grammar.