Survey of Programming Languages
COSC306
Lesson 3
Program Analysis and Design
- Syntax (Various PLs)
Outline
Review of Class Exercises – Lessons 1 & 2
PL Rules, Naming, Binding, Scope
Program Analysis/Design - Syntax
Program Sample Codes/Examples
Important Dates and Activities to note (very crucial)
Assignment for Next Class – Mathematical Expressions
Computational Mathematics
Sentence Representation
Computer Format
2 COSC306
Programming Languages Concepts - Lesson 2
The focus will be on syntax and semantic concepts:
Study Syntax – learn to specify grammars, parsers
Transforming 3 + 4 or (+ 3 4) or +(3, 4) to “application
of plus operator to constants three and four”.
Syntax reads input strings of the language and verify the input
belonging to the language. Compilers and interpreters
recognize syntax and convert it into machine understandable
form.
Study Semantic - How to define a language precisely?
– English is a poor metalanguage – what does that mean?
– What do programs mean (do/compute/produce/represent)?
Aspects of meaning: equivalence, termination, determinism,
type, denotation and axiomatic semantics . . .
3 COSC306
Review – PL Rules
Requires lots of patience, organization and interest (enjoy it)
Sound knowledge of programming principles (policies and rules) which
apply to many PLs
Understand the preliminaries for producing and maintaining high-
quality software systems
Must have skills in Analysis/Design, Coding/Debugging, Testing,
Maintenance/Refactoring, Documenting, etc.
It’s a life long process; need lots of experience to gain a deep
understanding of the concepts of programming languages
Good programmers never stop learning (the more you put in the
more you get out)
Programming is about ‘not writing code’ because the more you can
reuse code, the less you have to write
Most important always use the best tool for the job assigned
4 COSC306
Review – PL Naming
An identifier is the name of a user-defined object, a variable, a
function, class, or a label.
Identifier naming rules:
Names may contain letters (A to Z, a to z), numbers (0 to 9), or
underscores ().
The first character must be a letter or an underscore.
Names cannot contain any symbols, such as ~ ! @ # $ % ^ & * ( ) –
“+ = \| ‟, nor can they have any spaces.
Keywords cannot be used as variable names.
Identifiers may be any length, but only 1,024 characters are
significant.
Variable Name –
A data type is the type of data container.
A variable is the container’s name.
Each piece of program data needs both.
The data that is placed in the variable is referred to as the variable’s value.
5 COSC306
Review – PL Naming (contd)
A variable declaration statement must have a data type and a
variable name.The basic format is:
data_type variable_name;
Or a specifier of the desired data type and a variable identifier
type (specifier) variable_identifier;
Most programs are case sensitive.
“total”, “Total”, and “TOTAL” are separate names.
Also most programs are written inside functions
A function has a specific format, an entrance point,
and one (or many) exit points.
All functions have a name, a means to pass data into it and obtain
data from it.
Functions do program tasks and for most the starting point is the
main function
6 COSC306
Review – PL Binding
Most programs allow the developer to be flexible in writing declaration
and assignment statements.
type identifier = initial value ;
OR
type identifier (initial_value) ;
All of these are correct (True or False):
- int a (0);
- float ave;
- int num = 5;
- ave = 35.29;
- double money, speed;
- money = speed = 0.0;
*The ‘=’ symbol (equal sign) is called the assignment operator
7 COSC306
Review – PL Scope
This access is known as the scope of a variable.
8 COSC306
Program Analysis - Syntax
The ordering of and relationship between the words and other
structural elements in phrases and sentences. An exposition of or
set of rules for producing grammatical structures according to the
syntax of a language
The rules governing which statements and combinations of
statements in a programming language will be acceptable to a
compiler for that language.
The set of rules that defines the combinations of symbols that are
considered to be a correctly structured document or fragment in
that language.
This applies both to programming languages where the document
represents source code and markup languages, where the
document represents data; also documents that are syntactically
invalid are said to have syntax error.
9 COSC306
Syntax in various PLs – 1
Simple codes – explain the following:
a = b; a == b; a = !b; !a = !b;
a → b; a ⇒ b; a++; ++b;
(x * 2 or x.2 or x:2 or x2 or 2x) are all the same?
a/y or x//y (division operator) are the same?
– a is the same as !a?
a**b is an exponent and the same as a^b?
a = b = c = d = 15; (sets of integer variables)
ab(c), abc(4), a + b(2); are all absolute values?
(x++) is the same as (x = x*2)?
10 COSC306
Syntax in various PLs – 2
Declaring variables in different PLs:
PHP, Perl – $ (global variables for Ruby, ‘my’ for Perl)
Javascript – var (single variables for Python, class for PHP)
Python, Ruby – on the fly (its dynamic but needs care)
Ruby – @ for instance variable, @@ for class variables
Perl - @ for entire array, % for Hash array (list of elements)
Python, PHP – global key was for global variables
Most HLL – datatype variablename (C, C++, Java, C#, …)
Other forms of declarations
Python, Perl – myParams (database connectivity)
Class creation is the use of the keyword class in most PLs
11 COSC306
Syntax in various PLs – 3
Including libraries in various PLs:
Perl – use or require (use for CSS/JS in PHP)
Ruby – require (for routing in PHP)
Java, Python , D (DPL) – import
C/C++ – #include (include for PHP)
C# – using (extern or package in Javascript)
Comments in various PLs:
Javascript , Python, PHP – // or <!--…--> for HTML inline
Ruby, Python, Perl – #
Most HLL – /*…*/ or // (C, C++, Java, C#, D, …)
Functions in PLs:
Javascript , Python, PHP – function keyword (sub for Perl)
Returntype function-name for most HLLs (def fname for Ruby)
12 COSC306
Syntax in various PLs – 4
Common to most PLs:
\n – newline, \t – tab
\r – carriage return
[ ] – arrays, ; – terminator
+, –, *, /, % (modulus) – Arithmetic Operators
==, =, !=, <, >, <=, >= – Comparison Operators
and, or, not – Logical Operators
**, ^ – Exponentiation
sin, cos, tan – Trigometrical operators
Min, max, round, sqrt, pow, pi, random, exp – for most PLs
⟺, #, !, |, ~, {}, ∑, ∞, ∫, ∈, ∉, ∋, ∂, ∏, ∀, ∓, π - symbols
13 COSC306
Take Note!!!
Please, pay attention to the following information:
-----------------------------------------------------
Lesson 4 (Parse Tree) –Wednesday, February 15, 2017
(today – continuation of class)
Test (one and only) –Wednesday, February 22, 2017
Mid-Semester Exam – Friday, February 24, 2017 ???
Lesson 5 (Semantics) –Wednesday, March 8, 2017
Final Exam-Oral – March 15-17, 22-24, 2017 (Wed/Fri)
Exam Layout and CA –Wednesday, March 29, 2017
Final Exam Written – (as scheduled on Exam Timetable)
14 COSC306