Survey of Programming Language Lecture 1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

SURVEY OF PROGRAMMING

LANGUAGE

Dr. E. Owusu
CONCEPTS OF PROGRAMMING
LANGUAGES
Increased capacity to express ideas.

It is widely believed that the depth at which


people can think is influenced by the
expressive power of the language in which
they communicate their thoughts.
It is difficult for people to conceptualize
structures they cannot describe, verbally or in
writing.
Increased ability to learn new languages.

Computer programming is still a relatively


young discipline, and design methodologies,
software development tools, and
programming languages are still in a state of
continuous evolution.
The process of learning a new programming
language can be lengthy and difficult, especially
for someone who is comfortable with only one or
two languages and has never examined
programming language concepts in general.

Once a thorough understanding of the


fundamental concepts of languages is acquired, it
becomes far easier to see how these concepts are
incorporated into the design of the language being
learned.
Constituents of programming language
definition
SYNTAX
Syntax is the required grammar and
punctuation of the language
Definition: The syntax of a programming
language is the form or structure of the
expressions, statements, and program
units.
Compile-time errors are syntax errors
SEMANTICS

Semantics is all about meaning--what the


statements do, what the programs do
Definition: The semantics of a
programming language is the meaning of
the expressions, statements, and
program units.
e.g., while statement in Java
while (boolean_expr) statement
Logic errors are semantic errors
PRAGMATICS

It is the description and examples of how


the various features of the language are
intended to be used. Implementation of
the language (compilers and interpreters).
Auxiliary tools (syntax checkers,
debuggers, etc.).
Syntax and semantics provide a
languages definition.

Users of a language definition.


Other language designers.
Implementers.
Programmers (the users of the language)
Syntax examples
FORTRAN statements are one per line;
modern languages are free-format
Pascal uses semicolons between statements;
C uses semicolons after statements
Pascal uses beginend to group statements;
C uses { and }
Pascal uses the keyword integer; C uses int
C programming: if (x > y) x = x-1; else y--;
Pascal programming: if x > y then x := x-1
else y := y-1;

Differences: parentheses around x > y, the


word then, = or :=, semicolon before else, --
These are syntactic differences; the
meanings are identical.
The importance of syntax
Correct syntax is obviously important; if you
dont get it right, your program wont run
In a sense, syntax is trivial; you learn it, you fix
it until its right
But the syntax of a language greatly affects:
How easy it is to write programs
How easy it is to read and understand programs
How easy it is to make hard-to-see syntax errors
Examples of poor syntax
In FORTRAN, variables dont have to be
declared
Therefore, every misspelling is a new variable

In Pascal, semicolons go between


statements
Therefore, adding a statement to a block
involves adding a semicolon to the previous
line
Examples of good syntax
In Ada, control statements have the form
ifend if, whileend while, caseend
case, etc.
This helps avoid the confusion (in C) resulting
from large groups of anonymous closing
braces
Syntax is usually more important for
reading and understanding programs than
for writing them
Why syntax matters
C: Ada:
if (x < y) if x < y then
temp = x; temp := x;
x = y; x := y;
y = temp; y := temp
end if;

The C programming version has a bug


that almost never occurs in Ada
Semantics
Semantics has to do with the meaning of
constructs in a language, and the
meanings of programs written in that
language
Semantics is fundamental to everything
you do in a language
Syntax is just the code you use to
describe the semantics
High-level semantics
Semantics can affect things at a very high
level:
C is a procedural language; you describe a
set of procedures to follow.
Java is an object-oriented language; you
describe objects and their behaviors.
Prolog is a logic language; you describe facts
and the logical relationships among them.
Low level semantics
Semantics can affect things at a very low level:

C programming:

do { x = 2*x; }
while (x < 100);

Pascal programming:

repeat x := 2*x
until x >= 100;

Notice that the sense of the test is different: C exits the loop when
the condition becomes false, Pascal when it becomes true.
Syntax supports semantics
A language cannot have semantics
without syntax to support those semantics
C couldnt have a for loop without syntax
Java couldnt have objects without syntax for
creating and using them

This doesnt mean that for loops and


objects are syntactic constructs!
Syntax is typographical
Syntax describes the way we write programs
as a sequence of characters
Syntax can be precisely and formally
defined by BNF (Backus-Naur Form)
A language in the usual sense is a sequence
of characters (or sounds) and requires
syntax
BUT you can do many language-like things
with a GUI and no real syntax
Backus-Naur Form (1959) or Backus Normal Form
(BNF)
Invented by John Backus to describe Algol 58
BNF is equivalent to context-free grammars
BNF Fundamentals
A metalanguage is a language used to define other languages. BNF is a
metalanguage for programming languages.
A grammar is a metalanguage used to define the syntax of a language.
Our interest : using grammars to define the syntax of a programming
language.

BNF uses abstractions for syntactic structures, which act like


syntactic variables (also called nonterminal symbols, or just
nonterminal )
e.g., Java assignment statement can be given by

<assign> <var> = <expression>

abstraction to definition of LHS


be defined
BNF rule (or production): It is a definition of an abstraction. Its rule
has a left-hand side (LHS), which is a nonterminal, and a right-hand
side (RHS), which is a string of terminals and/or nonterminals
Terminals are lexemes or tokens
Nonterminals are often enclosed in angle brackets
e.g., BNF rules:
<if_stmt> if( <logic_expr> ) <stmt>
<if_stmt> if( <logic_expr> ) <stmt> else <stmt>
or with the rule
<if_stmt> if( <logic_expr> ) <stmt>
| if( <logic_expr> ) <stmt> else <stmt>

Describing lists:
<ident_list> identifier | identifier, <ident_list>
Semantics is fundamental
Semantics affects the very way we think
about programming
Someone once said, You can write a
FORTRAN program in any language.
This is a poor way to program
You can use a language, or you can fight with it
If you are fighting with a language, you are either
using the wrong language, or
using the language wrong
Thinking in the language
In C, functional decomposition is the
preferred way to write a program
In Java, functional decomposition is one of
the worst ways to write a program
In Java, you need to:
Choose your set of objects carefully
Decide the behaviors of each kind of object
Decide how objects communicate and interact
Pragmatics
Pragmatics has to do with how well the
language connects to the real world
Semantics supports pragmatics: some
kinds of languages are better for some
kinds of problems
The choice of a language should depend
on pragmatic considerations
Examples of pragmatics
C is fast because it does so little error
checking
Java programs are less buggy because
they spend so much time on error checks
Perl is good for CGI scripts because it has
powerful tools for string processing
Java is a better choice for me than C++
because I know Java better
Assignment 1

Find the differences between high


level and low level languages

Write a three pages document about


the Backus-Naur Form (BNF)

You might also like