You are on page 1of 11

Study Session 4: Syntax, Semantics and Pragmatics

Introduction
In the last study session , you have been introduced to the various approaches to problem
solving, Algorithm, Pseudocode and Flowchart, the various ways of representing an algorithm
and also its properties, differences between Pseudocode and Flowchart, the symbols used in
designing Flowcharts and the advantages and disadvantages of using a Flowchart.

In this study session, you will be introduced to syntax, semantics and pragmatics of selected
programming language. The importance of syntax and its properties will be discussed while the
theories and levels of semantics will also be discussed.

Learning Outcomes for Study Session 4


When you have studied this session Four, you should be able to:

4.1: Explain the meaning, importance and properties of Syntax.

4.2: Enumerate the importance and levels of Semantics

4.3: Highlight the importance of Pragmatics.


4.1 Introduction to Syntax, Semantics and Pragmatics.
A good program must be syntactically, semantically and pragmatically correct. In syntax,
because you can’t do anything without it then it is just an obstacle and you must understand how
well syntax is used in the program so as to communicate well with your programming language.

In pragmatics, it deals with the some idea of the strengths and weaknesses of languages rules so
as to understand the levels of the language of your program. In semantics, this is what your
programming languages are all about, and where the real advances are made when dealing with
you programming.

Syntax: The syntax


of a language
Pragmatics:
describes the
relates the basic Semantics: refers
possible
concepts of to the meaning of
combinations of
programming language
symbols that form
language.
a syntactically
correct program

Figure 4.1: Illustrations of Syntax, Semantics and pragmatics

4.1.1 Syntax
The syntax of a Programming language determines which character strings constitute well-
formed programs in the language and which do not so as to know how the syntactic of your
program is well defined.
Programming language's surface form is known as its syntax. Most programming languages are
purely textual; they use sequences of text including words, numbers, and punctuation, much like
written natural languages. On the other hand, there are some programming languages which are
more graphical in nature, using visual relationships between symbols to specify a program.
The syntax of a language describes the possible combinations of symbols that form a
syntactically correct program and the meaning given to a combination of symbols is handled by
semantics (either formal or hard-coded in a reference implementation). Since most languages are
textual, this article study session introduced you to a textual syntax.

Therefore, Programming language syntax is usually defined using a combination of regular


expressions (for lexical structure) and Backus–Naur Form (for grammatical structure). The table
below shows a simple grammar of a language.

Box 4.1: Syntax

The syntax of a Programming language determines which character strings constitute well-
formed programs in the language and which do not so as to know how the syntactic of your
program is well defined.

Example 4.1:
1. Expression:= atom | list
2. Atom: = number | symbol
3. Number: = [+-]? ['0'-'9']+
4. Symbol: = ['A'-'Z''a'-'z'].*
5. List: = '(' expression* ')'

The grammar above specifies the following:

i. an expression is either an atom or a list;


ii. an atom is either a number or a symbol;
iii. a number is an unbroken sequence of one or more decimal digits, optionally preceded
by a plus or minus sign;
iv. a symbol is a letter followed by zero or more of any characters (excluding
whitespace); and
v. A list is a matched pair of parentheses, with zero or more expressions inside it.
Not all syntactically correct programs are semantically correct. Many syntactically correct
programs are nonetheless ill-formed, per the language's rules; and may (depend on the language
specification and the soundness of the implementation) result in an error on translation or
execution.

Box 4.2: Syntax, Semantics and Pragmatics

Syntax simply describes the possible combinations of symbols that form a syntactically correct
program; Semantics describes the meaning of a language while pragmatics relates the concepts
of language.

Example 4.2

Some examples of selected programming languages are enumerated below:


i. FORTRAN statements are one per line; modern languages are free-format
ii. Pascal uses semicolons between statements; C uses semicolons after statements
iii. Pascal uses begin…end to group statements; C uses { and }
iv. Pascal uses the keyword integer; C uses int.

4.1.2 The Importance of a Syntax


Correct syntax is obviously important; if you don’t get it right, your program won’t run correctly.
In a sense, syntax is trivial; you learn it, you fix it until its right, then you stop.
The syntax of a language determines the following:

1. How easy it is to write programs


2. How easy it is to read and understand programs
3. How easy it is to make hard-to-see syntax errors
Example 4.3

Some examples of poor syntax are the following:

In Pascal, semi-colon go between statements. Therefore, adding a statement to a block involves


adding a semicolon to the previous line

In FORTRAN, variables don’t have to be declared therefore, every misspelling is a new variable

Example 4.4

Some examples of a good syntax are the following:

In Ada, control statements have the form if…end if, while…end while, case…end case, etc. This
helps avoid the confusion (in C) resulting from large groups of anonymous closing braces
Therefore, Syntax is usually more important for reading and understanding programs than for
writing them.

Pilot Question 4.1


1. What do you understand by the Syntax of a Language?
2. Enumerate the importance of Syntax as discussed in this Study Session.

4.2 Properties of a Syntax


Some of the properties can be listed below:
 The syntactic rules of a language belong to distinct levels according to their meaning:
That is, in syntax, the lowest level contains the 'spelling rules' for basic symbols, which
describe the construction of keywords, identifiers and special symbols. These rules
determine, for example, whether keywords have the form of identifiers (begin) or are
written with special delimiters ('BEGIN' .BEGIN), whether lower case letters are
permitted in addition to upper case And which spellings (<=, .LE., 'NOT' 'GREATER')
are permitted for symbols such as that cannot be reproduced on all I/O devices.

 The second level of properties consists of the rules that governing representation and
interpretation of constants.
That is rules about the specification of exponents in floating point numbers or the allowed
forms of integers (decimal, hexadecimal, etc.).

 The third level of syntactic properties is termed as the concrete syntax.


That is Concrete syntax rules describe the composition of language constructs such as
expressions and statements from basic symbols. Figure 4.2a shows the parse tree (a
graphical representation of the application of concrete syntax rules) of the Pascal
statement 'if a or b and c then : : : else : : : '.

Figure 4.2: Parse tree

4.2.1 The Semantics


The term Semantics refers to the meaning of languages, which has to do with the meaning of
constructs in a language, and the meaning of programs written in that language is known as
Semantics. Semantics is fundamental to everything you do in a language while Syntax is just the
“code” you use to describe the semantics in a language.

Semantics focuses on the relation between signifiers, like words, phrases, signs, and symbols,
and what they stand for, which is their denotation. Linguistic semantics is the study of meaning
that is used for understanding human expression through language. Other forms of semantics
include the semantics of programming languages, formal logics, and semiotics.

Box 4.3: Semantics

The term Semantics refers to the meaning of languages which has to do with the meaning of
constructs in a language and the meaning of programs written in that

4.2.2 Theories in semantics


In Semantics, we have different theories that explain the concept semantically in a language and
it can be listed below: Model theoretic semantics:

Models theoretical is highly formalized theory of natural language semantics in which


expressions are assigned denotations (meanings) such as individuals, truth values, or functions
from one of these to another. The truth of a sentence, and more interestingly, its logical relation
to other sentences, is then evaluated relative to a model.

While Formal (or truth-conditional) semantics : is a semantics that aims aims to associate you
with each natural language sentence with a meta-language description of the conditions under
which it is true, for example when the Snow is white' is true if and only if snow is white. The box
below will expose you to some challenges you likely to see in semantics manner.

4.2.3 Levels of Semantics


In Semantics, there are two levels you need to know for better understanding of semantics. These
are: High level Semantics and Low level Semantics.
Figure 4.4: Levels of Semantics

High level Semantics: in high level semantics, semantics can affect things at a very high level.

Example 4.4

Some examples of High level Semantics are:


1. Pascal is a procedural language; you describe a set of procedures to follow
2. Java is an object-oriented language; you describe objects and their behaviours
3. Prolog is a logic language; you describe facts and the logical relationships among them

Low level Semantics: This is a semantics that can affect things at a very low level. The box
below shows the examples that relate with the meaning of a low level semantics.

4.2.4. Importance of Semantics


In semantics, there are three ways in which semantics is important to a language. These are:

1. Syntax supports Semantics


A language cannot have semantics without syntax to support those semantics as Pascal cannot
have a for loop without syntax. Java cannot have objects without syntax for creating and using
them, and it does not mean that for loops and objects are syntactic constructs.
2. Semantics is Fundamental
In every language of a program, semantics must be understandable since it denotes the meaning
of a language in a program Semantic.

Pilot Question 4.2


1. Enumerate the properties of semantics
2. Highlight the importance of semantics
3. Itemize the levels of semantics

4.3. Pragmatics
Pragmatics is a subfield of linguistics and semiotics which studies the ways in which context
contributes to meaning. Pragmatics encompasses speech act theory, conversational, talk in
interaction and other approaches to language behaviour in philosophy, sociology, linguistics and
anthropology. Unlike semantics, which examines meaning that is conventional or “coded” in a
given language?

Box 4.5: Pragmatics

Pragmatics is a subfield of linguistics and semiotics which studies the ways in which context
contributes to meaning.

4.3.1. Importance of Pragmatics


Some importance of pragmatics are enumerated below:

a. Pragmatics has to do with how well the language connects to the “real world”
b. Semantics supports pragmatics: some kinds of languages are better for some kinds of
problems
c. The choice of a language should depend on pragmatic considerations
Pilot Question 4.3
1. What do you understand by the word “Pragmatics”
2. Enumerate the importance of Pragmatics.

Summary of Study Session 4

In Study Session 4, you have learnt that:

1. The syntax of a Programming language determines which character strings constitute well-
formed programs in the language and which do not so as to know how the syntactic of your
program is well defined
2. The three importance of syntax which are: very easy to write program, easy to write and
understand and easy to make hard in syntax error.
3. Three importance of Syntax which are: the first level is syntactic rules of a language, the
second level properties consist of rules that governing representation of constants and the
third level of syntactic is termed as the concrete syntax.
4. The term semantics refers to the meaning of languages, which has to do with the meaning of
constructs in a language, and the meanings of programs written in that language is known as
Semantics.
5. In Semantics, we have different theories which are: Models theoretical and formal (or truth)
semantics.
6. The two levels of Semantics which are High level and low level semantics.
7. Pragmatics is a subfield of linguistics and semiotics which studies the ways in which context
contributes to meaning

You might also like