# Programming Language Grammar

Every language has its own grammar. Nepali, Hindi, Mathili, Newari, English, French, German etc are natural languages and each language has its own grammar. The richness of language depends on its stock of vocabulary and grammar rules. The grammar is the discipline of expression. It is hard and fast rule of reading, writing and speaking.

The oldest grammar of the world is a grammar of Tamil language, called Tolkaapiyam written by Tolkappiyar in 200 BC. The foundation stone of modern grammar was laid by Panini by Sanskrit grammar in 5th century BC. It contains 3,959 rules of Snaskrit. In Greece, the dialogue crytylus was first grammar written by Plato. Every computer languages have its own grammar and compiler is designed to follow these grammars.

• The comment writing techniques are different for each language. // and /* */ are used in C, C++, C# and Java. This technique is quite different in PASCAL. In PASCAL, {} or (**) is used commonly for comment.
• In PASCAL, C, C+, C#, Java statements are terminated by semi-colon. If we avoid semi-colon, compiler displays error messages.
• In PASCAL, block is started by begin and terminated by end, but in the case of C, C++, C#, Java the curly braces are used for block({}).
• Branching and looping syntax are different for different programming languages.
• The file handling techniques are different for each programming languages.

The program written for one language cannot be executed on the compiler of other languages because every language has its own syntax, semantics and procedures. The novice students have to design simple mathematical compilers because simple mathematical functions processors can be easily constructed.

Example:

This program is written in C++ language and only C++ compiler supports this program for machine code generation. When this program is tried to execute in other language compiler, error message are displayed.

// Program written in C++ Language

/* Hello>cpp */

#include<iostream.h>

Void main()

{

Cout<<”Hello Friends”;

}

Example:

This program is written in PASCAL programming language and it is supported and executed by PASCAL comiler only.

(*Program written in PASCAL language*)

Program Hello(input, output);

Begin

WriteLn(‘Hello Friend’);

end.

### Comparisons between C++ and PASCAL

When we compare simple program written in C++ and PASCAL language, the concept of different types of compilers needed for different types of computer languages can be clarified to us. The programming languages grammars for both programs are different, so different types of compilers are needed to generate machine codes.

 Issues C++ PASCAL Comments The comments are started by /* and ended by */. We can use // to write single line comments. Comments are closed within ( and ). Header File Header file is needed because it provides objects and functions to execute program. It does not support any header file like as C or C++. Block The curlu braces are used to start and close blocks of statements. The blocks of statements are started by being and closed by end. Display Technique In C++, object cout is ued to display string. In PASCAL, function WritLn() is used to display strings. Conditional Statements If(condition) { Statement(s); } Else { Statement(s); } Ifthen Being Statement(s) End; Else Being Statement(s) End;

The rich knowledge of programming language grammar is first requisite for compiler designers. The success of compiler depends upon its capability of error checking, message displaying, and promptly execution of source codes. The concept of terminal symbols, non-terminal, production, reduction, syntax tree etc are basic key points to understand complex structure of compiler.

### Terminal symbol

The individual alphabets or number is called terminal symbol.

Example:

{a, b . . . Z, 1. . . .9}

### Non-terminals (NT)

The sequence of character is called non-terminals. It may be word and sentences. Generally capital letters are used to represent non-terminals and small are used to denote terminals symbols.

Example:

<institution>, <legal>, <publisher> etc

• String: String is the finite sequence of symbols (characters). The Uppercase is used to represent it, but some authors suggest $\alpha \, \beta \, \gamma$ etc for string representation. The concatenation of string is also possible. For instances, if A=red and B=hat, then AB becomes redhat.
• $x^n$ is the string where x is repeated n times.

Example:

X1 = x

X2=xx

X3=xxx

—————

x=xxxx……n

• Sentence and word: The sentence and word are synonyms of string. The size of string is also easily denoted by | and |. For instance he size A is |A|. if A={redrose} the size of A becomes 7.
• Empty string: The string with size zero is called empty string.
• Language: A set of string is termed as language. The notation of language is same as string. It also supports concatenation. For instance, A and B are two languages, so AB={XY | x is in A and y is in B}

Example:

The following example illustrate concept of concatenation of two languages.

A={red, blue}

B= {doll, kite}

AB={reddoll, bluedoll, redkite, bluelkite}

Example:

The above example can help to understand following example.

A={01, 10}

B={11,110}

AB={0111, 1011, 01110, 10110}

### Productions

The expression which is the part of grammar called productions. It is an expression made from terminals and non-terminal symbols.

NT: =NT and Terminal symbols

Example:

income ::= salary + rent

<income>, <salary>, <rent> are non terminals.

### Derivation

Derivation is the replacement according to production.

### Reduction

It is a replacement of string according to non-terminals (NTs). The following tracks are applied at the time of reduction:

<sentence> ::=<noun phrase><verb phrase>

<noun phrase> ::=<article><noun>

<verb phrase> ::=<verb><noun phrase>

Example:

0: the lion takes flesh

1: <article> Lion takes flesh

2: <article><noun> takes flesh

3: <article><noun><verb><noun>

4: <noun phrase><verb><noun>

5: <noun phrase><verb phrase>

6:<sentence>

### Syntax Tree

The syntax tree is the representation of steps of reductions.

Syntax Tree

### Classification of Grammar

The programming language grammar is classified into four categories:

• Type-0 Grammar or Phrase structure Grammar: $\alpha : : = \beta$ where $\alpha$ and $\beta$ are terminal and non-terminals.
• Type-1 Grammar or Context sensitive Grammar: The concept of context sensitive grammar was introduced by Noam Chomsky in 1950s as a way to describe the syntax of natural language.
• Type-2 Grammar or Context free Grammar (CFG): The formal grammar is termed as Context free grammar. It is more suitable for programming language specification.
• Type-3 Grammar or Linear or Regular Grammar
• Operator Grammar

Related posts:

1. Programming Language Language is a medium of communication. There are several languages...
2. Procedural oriented programming Procedural oriented programming (pop):- A program in a procedural language...
3. Compiler Construction The compiler is a system program used to translate source...
4. Programming Concepts Step of Programming   The programming is the coding instruction...
5. Programming Techniques Software designing is very anesthetic phase of software development cycle....