Solution 1:

Regular grammar is either right or left linear, whereas context free grammar is basically any combination of terminals and non-terminals. Hence you can see that regular grammar is a subset of context-free grammar.

So for a palindrome for instance, is of the form,

S->ABA
A->something
B->something

You can clearly see that palindromes cannot be expressed in regular grammar since it needs to be either right or left linear and as such cannot have a non-terminal on both side.

Since regular grammars are non-ambiguous, there is only one production rule for a given non-terminal, whereas there can be more than one in the case of a context-free grammar.

Solution 2:

I think what you want to think about are the various pumping lemmata. A regular language can be recognized by a finite automaton. A context-free language requires a stack, and a context sensitive language requires two stacks (which is equivalent to saying it requires a full Turing machine.)

So, if we think about the pumping lemma for regular languages, what it says, essentially, is that any regular language can be broken down into three pieces, x, y, and z, where all instances of the language are in xy*z (where * is Kleene repetition, ie, 0 or more copies of y.) You basically have one "nonterminal" that can be expanded.

Now, what about context-free languages? There's an analogous pumping lemma for context-free languages that breaks the strings in the language into five parts, uvxyz, and where all instances of the language are in uvixyiz, for i ≥ 0. Now, you have two "nonterminals" that can be replicated, or pumped, as long as you have the same number.

Solution 3:

The difference between regular and context free grammar: (N, Σ, P, S) : terminals, nonterminals, productions, starting state Terminal symbols

● elementary symbols of the language defined by a formal grammar

● abc

Nonterminal symbols (or syntactic variables)

● replaced by groups of terminal symbols according to the production rules

● ABC

regular grammar: right or left regular grammar right regular grammar, all rules obey the forms

  1. B → a where B is a nonterminal in N and a is a terminal in Σ
  2. B → aC where B and C are in N and a is in Σ
  3. B → ε where B is in N and ε denotes the empty string, i.e. the string of length 0

left regular grammar, all rules obey the forms

  1. A → a where A is a nonterminal in N and a is a terminal in Σ
  2. A → Ba where A and B are in N and a is in Σ
  3. A → ε where A is in N and ε is the empty string

context free grammar (CFG)

○ formal grammar in which every production rule is of the form V → w

○ V is a single nonterminal symbol

○ w is a string of terminals and/or nonterminals (w can be empty)

Solution 4:

Regular grammar:- grammar containing production as follows is RG:

V->TV or VT
V->T

where V=variable and T=terminal

RG may be Left Linear Grammar or Right Liner Grammar, but not Middle linear Grammar.

As we know all RG are Linear Grammar but only Left Linear or Right Linear Grammar are RG.

A regular grammar can be ambiguous.

S->aA|aB
A->a
B->a

Ambiguous Grammar:- for a string x their exist more than one LMD or More than RMD or More than one Parse tree or One LMD and One RMD but both Produce different Parse tree.

                S                   S

              /   \               /   \
             a     A             a     B
                    \                   \
                     a                   a

this Grammar is ambiguous Grammar because two parse tree.

CFG:- A grammar said to be CFG if its Production is in form:

   V->@   where @ belongs to (V+T)*

DCFL:- as we know all DCFL are LL(1) Grammar and all LL(1) is LR(1) so it is Never be ambiguous. so DCFG is Never be ambiguous.

We also know all RL are DCFL so RL never be ambiguous. Note that RG may be ambiguous but RL not.

CFL: CFl May or may not ambiguous.

Note: RL never be Inherently ambiguous.

Solution 5:

Regular Expressions

  • Basis of lexical analysis
  • Represent regular languages

Context Free Grammars

  • Basis of parsing
  • Represent language constructs

enter image description here