Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular vs Context Free Grammars

I'm studying for my computing languages test, and there's one idea I'm having problems wrapping my head around.

I understood that regular grammars are simpler and cannot contain ambiguity, but can't do a lot of tasks that are required for programming languages. I also understood that context-free grammars allow ambiguity, but allow for some things necessary for programming languages (like palindromes).

What I'm having trouble with is understanding how I can derive all of the above by knowing that regular grammar nonterminals can map to a terminal or a nonterminal followed by a terminal or that a context-free nonterminal maps to any combination of terminals and nonterminals.

Can someone help me put all of this together?

like image 452
Jason Baker Avatar asked Feb 18 '09 03:02

Jason Baker


People also ask

Why is context free grammar more powerful than regular grammar?

Because context-free grammars allow a wider range of rules than regular grammars, they can generate a wider range of structures than regular grammars.

Is a context-free language regular?

All regular languages are context-free languages, but not all context-free languages are regular. Most arithmetic expressions are generated by context-free grammars, and are therefore, context-free languages.

Are context-free grammars more powerful than regular expressions?

Context-free grammars are strictly more powerful than regular expressions. Any language that can be generated using regular expressions can be generated by a context-free grammar. There are languages that can be generated by a context-free grammar that cannot be generated by any regular expression.


2 Answers

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.

like image 84
Sujoy Avatar answered Oct 20 '22 00:10

Sujoy


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.

like image 22
Charlie Martin Avatar answered Oct 20 '22 00:10

Charlie Martin