Dr. E. Owusu

• 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

• Increased ability to learn new languages. – Computer programming is still a relatively young discipline. and design methodologies. . and programming languages are still in a state of continuous evolution. software development tools.

• Once a thorough understanding of the fundamental concepts of languages is acquired. . especially for someone who is comfortable with only one or two languages and has never examined programming language concepts in general.• The process of learning a new programming language can be lengthy and difficult. it becomes far easier to see how these concepts are incorporated into the design of the language being learned.

statements. – Compile-time errors are syntax errors . and program units. 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.

what the programs do • Definition: The semantics of a programming language is the meaning of the expressions. statements.SEMANTICS • Semantics is all about meaning--what the statements do. – e. and program units.g. while statement in Java while (boolean_expr) statement – Logic errors are semantic errors ..

Implementation of the language (compilers and interpreters).PRAGMATICS • It is the description and examples of how the various features of the language are intended to be used. etc. .). Auxiliary tools (syntax checkers. debuggers.

• Other language designers. • Implementers. • Programmers (the users of the language) . – Users of a language definition.• Syntax and semantics provide a language’s definition.

C uses { and } • Pascal uses the keyword integer. C uses semicolons after statements • Pascal uses begin…end to group statements. modern languages are free-format • Pascal uses semicolons between statements. C uses int . Syntax examples • FORTRAN statements are one per line.

the meanings are identical. • Differences: parentheses around x > y. the word then. = or :=.• C programming: if (x > y) x = x-1. semicolon before else. . else y--. • Pascal programming: if x > y then x := x-1 else y := y-1. -- • These are syntactic differences.

if you don’t get it right. syntax is trivial. you fix it until it’s 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 . you learn it. your program won’t run • In a sense. The importance of syntax • Correct syntax is obviously important.

semicolons go between statements – Therefore. adding a statement to a block involves adding a semicolon to the previous line . Examples of poor syntax • In FORTRAN. every misspelling is a new variable • In Pascal. variables don’t have to be declared – Therefore.

while…end while. Examples of good syntax • In Ada. control statements have the form if…end if. case…end case. – 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 . etc.

Why syntax matters C: Ada: if (x < y) if x < y then temp = x. • The C programming version has a bug that almost never occurs in Ada . y := temp end if. x = y. y = temp. temp := x. x := y.

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 . Semantics • Semantics has to do with the meaning of constructs in a language.

– Prolog is a logic language. you describe objects and their behaviors. . High-level semantics • Semantics can affect things at a very high level: – C is a procedural language. – Java is an object-oriented language. you describe a set of procedures to follow. you describe facts and the logical relationships among them.

Pascal programming: repeat x := 2*x until x >= 100. Low level semantics • Semantics can affect things at a very low level: C programming: do { x = 2*x. Pascal when it becomes true. } while (x < 100). • Notice that the sense of the test is different: C exits the loop when the condition becomes false. .

Syntax supports semantics • A language cannot have semantics without syntax to support those semantics – C couldn’t have a for loop without syntax – Java couldn’t have objects without syntax for creating and using them • This doesn’t 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 .

or just nonterminal ) e. Java assignment statement can be given by <assign> → <var> = <expression> abstraction to definition of LHS be defined . which act like syntactic variables (also called nonterminal symbols..BNF Fundamentals A metalanguage is a language used to define other languages. A grammar is a metalanguage used to define the syntax of a language.g. BNF is a metalanguage for programming languages. • BNF uses abstractions for syntactic structures. Our interest : using grammars to define the syntax of a programming language.

and a right-hand side (RHS).g.• BNF rule (or production): It is a definition of an abstraction. which is a string of terminals and/or nonterminals • Terminals are lexemes or tokens • Nonterminals are often enclosed in angle brackets e. Its rule has a left-hand side (LHS).. <ident_list> . 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. which is a nonterminal.

Semantics is fundamental • Semantics affects the very way we think about programming • Someone once said. 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 .” – This is a poor way to program – You can use a language. “You can write a FORTRAN program in any language.

functional decomposition is one of the worst ways to write a program • In Java. Thinking in the language • In C. functional decomposition is the preferred way 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) .