Syntax, Semantics, Pragmatics

Syntax, Semantics, Pragmatics
Three aspects of language
• Syntax is the required grammar and
punctuation of the language
– Compile-time errors are syntax errors
• Semantics is all about meaning--what the
statements do, what the programs do
– Logic errors are semantic errors
• Pragmatics has to do with what’s “good”
and “bad” about a language or program
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 begin…end to group statements;
C uses { and }
• Pascal uses the keyword integer; C uses int
Another syntax example
• C:
if (x > y) x = x-1; else y--;
Pascal: 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
don’t get it right, your program won’t run
• In a sense, syntax is trivial; you learn it, you
fix it until it’s right, end of story
• 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 don’t 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
An example of good syntax
• 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
• Syntax is usually more important for
reading and understanding programs than
for writing them
Why syntax matters
C:
if (x < y)
temp = x;
x = y;
y = temp;
Ada:
if x < y then
temp := x;
x := y;
y := temp
end if;
• The C 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:
do { x = 2*x; } while (x < 100);
Pascal: 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 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
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
Goals of this course
• A primary goal of this course is to introduce
you to different ways of thinking about
programming
• It is essential to understand the “genius” of a
language, and to work with it, not against it
• You have to learn the syntax of a language
well enough that you can concentrate on the
semantics
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
What is in this course?
• Syntax, because you can’t do anything
without it -- but it’s just an obstacle
• Semantics, because this is what programming
languages are all about, and where the real
advances are made
• Pragmatics, because you need some idea of
the strengths and weaknesses of languages
The End