Grammars and language generated by grammars

On May 13th, 2012 by admin | No Comments | Posted in TOC

The general form of a grammar is G =(N,T,P,S).

N = finite set of non terminal

T = finite set of terminals

P = Production rules

S = Start state

The language generated by the grammar will be of the form

L(G) ={w | w ∈ T*, S*=>w}

If you have any doubts about the above mentioned things please go through the links below.

http://www.martinjacob.info/2012/05/12/grammar-in-a-language-context-free-phrase-structure-type-012-3/

http://www.martinjacob.info/2012/05/12/parsing-production-rules-derivation/

In this post we shall see how languages are derived from a given grammar. The point to be noted here is that all the terminal strings derivable from the grammar belong to the language.

Ex. 1

N={S} (the only non terminal is the start symbol)

T={a,b}(a and b are the terminal symbols)

P={1. S → aSb

2. S → ab} {1 and 2 are the set of production rules )

The above specified grammar is type 2 grammar because it has only one non terminal S on the LHS and has string on the RHS.

Now we shall derive the language generated .

Using the second production rule :

S => ab

Using the first rule first and then the second rule :

S => aSb

S => aabb

ie a^2 b^2

Using the first rule two times and then the second rule

S => aSb

S => aaSbb

S => aaabbb

ie a^3 b^3

The parse tree for a^3 b^3 is given below. To find the language generated we read the leafs of the tree from the left to the right.

 

 Ab,a^2 b^2 and a^3 b^3 here we can see a pattern being developed. When ever an ‘a’ is added on the LHS of the result a corresponding ‘b’ is added on the RHS. So the language generated will be of the form

L(G)= (a^nb^n | n>=1)

In this language the rule 1 will be applied n-1 times and rule 2 once.

Ex.2

Consider the grammar

N = {S}

T = {a,b,c}

P = { 1 .S → aSa

  1. S → bSb

  2. S → C }

Lets find out the language generated by this grammar

Using the third production rule

S => c

Therefore c belong to the language L(G).

Using the first production rule first and the third production rule second

S => aSa

=> aca

Therefore aca belongs to the language L(G)

Using the second production rule first and the third production rule second

S => bSb

=> bcb

Therefore bcb belongs to the language L(G)

Using the first production rule first, second production rule second and the third production rule last.

S => aSa

=> abSba

=> abcba

Therefore abcba belongs to the language L(G)

The parse tree is like this

In this language the pattern we can identify is that the string after the ‘c’ will be the reversed form of the string before c . ab c ba here string before c is ab and string after c is ba.

The general representation of the language is generated by this grammar is

L(G) = {w c w(r) | w ∈(a,b) }

Grammar in a language

On May 12th, 2012 by admin | No Comments | Posted in TOC

All of you have head about grammar in our school days. They are used to define a language. In the case of computer science they are used as language generating device. A grammar consist of

N – Finite set of non terminals

T – Finite set of terminals

P – Finite set of production rules

S – Start symbol

ie G= (N,T,P,S)

If you dont know what N,T,P,S are follow this link. http://www.martinjacob.info/2012/05/12/parsing-production-rules-derivation/

There are four types of grammar as defined by Chomsky. Type 0 , type 1, type 2 and type 3. Lets explain the grammars one by one.

Type 0 : This is the most general grammar and is also called phrase structured language. Here the rules are of the form

U → V {U ∈ V* N V* ie U should have atleast on non terminal

V∈ V*

V = N T }

Type 1 : This is also of the form

U → V {U ∈ V* N V* ie U should have atleast on non terminal

V∈ V* }

But the restriction here is that length of U should be greater than length of V ie |U|<= |V|. This grammar is also called the length increasing grammar. Since we have the restriction |U|<= |V| we cannot have the empty string є on the right hand side. To allow empty string we add the rule S → є and make sure that S does not occur in the right hand side of any rule.

There is another definition for this grammar – The context sensitive grammar. It definition is given as

α A β → α γ β {A ∈ N, α,β ∈ V*, γ∈ V+}

This means that A is rewritten as γ in the context of α and β.

Type 2 : This grammar is of the form

A → α {A ∈ N, α ∈ V* }

Here the LHS should be a non terminal but RHS can have string. This is also called Context free grammar .

Type 3 : This grammar is of the form

A → a β { A,β ∈ N, a∈ T}

A → b {b∈T∪{є}}

A language defined by a grammar G is denoted as L(G). It can be formaly represented as L(G)= {w | w ∈ T* ,S*=>W}. 

Theory of computation : Production rules, parsing and derivations

On May 12th, 2012 by admin | 1 Comment | Posted in TOC

TOC started in 1959 with  formal definition given by  N Chomsky. Chomsky tried to define a mathematical model for grammar. His real motivation was to study parsing in natural languages like English. He looked at the parse trees of the natural languages and tried to define what is a grammar.

We will look at a example “The man ate the fruit”. The parse tree for the sentence is given below.

Parse tree

The start state is S. From there we divide into many sub division and reach the final nodes which form the sentence. This process of deriving the sentence from the start state S is called derivation. The rules for derivation of the above example is given below. → Means rewritten as.

<S> → <Non Phrase 1> ,<Verb Phrase>

<Non Phrase 1> → <article>, <Noun 1>

<article> → the

<Noun 1> → man

<Verb Phrase> → <Verb> , <Non Phrase 2>

<Verb> → ate

<Non phrase 2> → <article> ,<Noun 2>

<article> → the

<Noun 2> → fruit

The rules are also called productions.

Now we can see how these rules/ productions can be used to derive the sentence “The man ate the fruit”. The derivation is given below. => Means directly derives.

<S> => <Non Phrase 1> ,<Verb Phrase>

      => <article>, <Noun 1> ,<Verb Phrase>

     => the <Noun 1> ,<Verb Phrase>

     => the man <Verb Phrase>

     => the man <Verb> , <Non Phrase 2>

     => the man ate , <Non Phrase 2>

     => the, man ate <article> ,<Noun 2>

     => the, man ate the ,<Noun 2>

     => the, man ate the fruit

 A => B means we get B from A by the application of a single rule. A *=> B means that B is derived from A in zero or more steps. In our example S*=> The man ate the fruit.

Non terminals (N) : The non phrase 1, verb etc in the parse tree are called non terminals. They are called non terminals because they are rewritten as some thing else

 Terminals (T) : The words of the sentence are called terminals . They are the leaves of the tree. They are called terminals because they cant be rewritten into some thing else.

 V is the set of alphabets we use V= N u T .

 S: Is the start symbol. S belongs to the set of non terminals.

Karnaugh map Explained

On March 3rd, 2012 by admin | No Comments | Posted in Computer organisation and architecture

Universal gates -NAND and NOR

On March 3rd, 2012 by admin | No Comments | Posted in Computer organisation and architecture

NAND and NOR are called universal gates because any gate can be constructed using these two gates

 

Universal property of NAND gate

 

Universal property of NOR gate

 

OR gate

On March 1st, 2012 by admin | No Comments | Posted in Computer organisation and architecture

OR gate gives a high out if any of its input is high.  Q=A+B . Q is high if A or B is true or both of them is true.

The symbol and boolean expression for OR gate is given below

The truth table for a 3 input Boolean expression is given below.

 

Examples of 2 input OR gates are given along with their corresponding boolean expression.

 

AND gate

On March 1st, 2012 by admin | No Comments | Posted in Computer organisation and architecture

And gate gives a high output only if all the inputs are high. The symbol for  a 3 input AND gate is given below

Output Q= X.Y.Z (Q is high only if x,y,z are high)

The truth table of a 3 input AND gate is given below

 

 

Example of a 2 input AND gate is given below

NOT gate

On March 1st, 2012 by admin | No Comments | Posted in Computer organisation and architecture

Not gate is also called the Inverter. It inverts what ever input it gets. ie if the input to a not gate is true its output will be false and if the input to a not gate is false its output will be true.


The truth table for a NOT gate is given below

Voltage levels in logic gates

On March 1st, 2012 by admin | No Comments | Posted in Computer organisation and architecture

AND, OR and NOT gates are the most basic gates. The input and output of these gates are in the form of logical 1 and 0. The I/p and o/p can also be represented in the form of HI and LO.

 To implement logic gates in digital circuits we need to find a way of representing 0 and 1 in the form of voltages. In digital circuits we use a band of voltages to represent 0 and another band of voltages to represent one.

 Band of voltages for output:If the output of gate lies in between 2.4 and 5.0 volt it is assumed to be a 1. And if the output is between 0 and 0.4 it is assumed to be a 0. Below we have a pictorial representation of voltage bands of output. 

Band of voltages for input: If the input to a logic gate lies in between 5.0v to 2.0 v the input is assumed to be 1 and if the input lies in between 0v and .8v the input is assumed to be 0. Below we have a pictorial representation of voltage bands of input.

The specification of input allows a narrower gap between 0 and 1 comparing to that of output. This implies that the output of a gate can be corrupted by a noise of magnitude upto 0.4v and can still be accepted as input by another gate. This guarantees noise immunity of 0.4v.

Positive logic: The convention of using HI voltage level to represent a 1 and a LO voltage level to represent a 0 is called a positive logic.

 Negative logic: The convention of using HI voltage level to represent a 0 and a LO voltage level to represent a 1 is called a negative logic.

 Mixed logic: In this HI is taken as a 1 in input and 0 at the output. 

Canonical forms for Boolean expressions Canonical forms for Boolean expressions Canonical forms for Boolean expressions

On February 26th, 2012 by admin | No Comments | Posted in Computer organisation and architecture, digital computer design

Canonical forms for Boolean expressions

Older Posts »