Introduction to Syntax and Semantics


Like ordinary language English, programming languages have syntax. The Syntax of a (programming) language is a set of rules that define what sequences of symbols are considered to be valid expression (programs) in the language.
The Syntax of a programming language is what the program looks like.
Syntax provides significant information needed for understanding a program and provides much needed information towards the translation of the source program into the object program.

A valid representation of syntax


Invalid representation maybe


2+3*4 text will be interpreted this expression as having value 14 and not 20. That is, expression is interpreted as if written (2+3)*4.

We can specify either interpretation, if we wish, by syntax and hence guide the translator into generating the correct operations for evaluating this expression.

In a Statement

X=2.82 + 3.68

syntax cannot tell the type of x on which result is depended.
If x is Real then output will be 6.50 and
if x is integer then output will be 6.

To completely describe the syntactic structure of programming language we need something else which can tell us the meaning of expression, statement and program units.

Semantics: Semantics is the meaning of an expression (program) in a programming language.

In C to declare a 10 elements vector V of Integer has declaration

int v:{10};

In Pascal
v:array[0......9] of integer
Although both creates similar data objects at run time , their syntax is very different. To Understand the meaning of declaration we need to know the semantics of both pascal and c for such array declaration.

Another example

While (< boolean_exp>)< statement>

The semantics of this statement form is that when the current value of the Boolean exp. Is true, the embedded statement is true.

Facebook Likes