Jump to content

Theory of Programming Languages/General Syntactic Structure

From Wikiversity

This is a lesson in in the course, Theory of Programming Languages, which is a part of The School of Computer Science

Objective

[edit | edit source]

In this lesson students will learn about syntax and semantics, and consider the logical structure of a basic mathematical statement.

Students will be able to answer:

  • What is the difference between Syntax and Semantics?
  • Within the field of computer science, what is a Statement?

General Syntactic structure

[edit | edit source]
By understanding the distinction between syntax and semantics, computer scientists have been able to develop hundreds of well used computer languages.

Within the realm of computer science, "Syntax" means the way that logical ideas are written, this is in contrast with "Semantics" which is the meaning of the ideas themselves. To understand the difference between Semantics and Syntax, consider this example:

The date "March 4th, 1990" can be written in many formats. The european format of writing dates is DD/MM/YYYY (where D denotes the day, M the month, and Y the year). The American format is MM/DD/YYYY. Therefore:

4/3/1990 - European format
3/4/1990 - American format

These examples have the same meaning, which is "March 4th, 1990". This means they have the same Semantics. Now, to consider another case:

3/4/1990 - European format
3/4/1990 - American format

These examples have different meanings, the European format is "April 3rd, 1990" while the American format is "March 4th, 1990". Their Syntax is the same - they are written the same way. But their Semantics are different - the meanings are not the same.

The distinction between Semantics and Syntax will become more significant as this course continues. For now, remember that Syntax is structure and Semantics is meaning

Like English, letters are used to write words in Programming Languages - but these words are connected together to form "Statements" rather than sentences. The syntax of a mathematical statement may vary between programming languages, but the semantics will remain the same. Consider this example:

The statement A + B can be written in many different syntaxes while retaining the same semantics: 

1] A + B
2] (+ A B)
3] AB+

Example 1 uses infix notation.
Example 2 uses prefix notation.
Example 3 uses postfix notation.

In the examples above, different syntax is used while the semantics remain the same. Often semantics are represented using a simple tree structure. The semantic tree for all of the examples is:

   +
  / \
 a   b

Whole programs can be reduced to a semantic tree - this is because programs are composed of logical statements, and as we have learned above we can represent statements as semantic structures.


Assignments

[edit | edit source]
  1. Write several complicated mathematical statements, write them in Postfix, Prefix, and Infix notation.
  2. Draw the syntactic tree for these statements.
  3. Read the sections about Semantics and Syntax from your textbook.

Completion status: this resource is just getting off the ground. Please feel welcome to help!