3.3.2 Language syntax required for software solutions

In this section, we examine the syntax of programming languages. The syntax of programming languages is described using metalanguages. 

In the Preliminary course, we examined EBNF and railroad diagrams. We will use these metalanguages to describe the syntax of commands used to define and use 

  • multi-dimensional arrays, 
  • arrays of records, 
  • files and 
  • random number generators.

Metalanguages

The rules of a programming language must be precise. Computers cannot follow vague instructions, so programs have to be written in a very precise way. The computer does not understand the text of a program. You must not use words of symbols that are not expected, and each instruction must adhere to a given structure or set of rules.

Each program instruction written in a programming language is known as a sentence.

The rules that tell us how the sentence can be constructed is called the syntax. Ever programming language has its own syntax or rules. When we use a language we must obey its syntactic rules The rules can be described in a number of ways.

  • By example
  • By using a text description
  • By using a diagram

Sometimes the syntax of a language is given using all of these methods. A text description explains what the sentence is about.

Metalanguages provide the best way of defining the structures common to all programming languages. These are formal standard structures understood by all professional developers and programmers. Metalanguages can be divided into text based descriptions of syntax provided by BNF (Backus Normal Format) and EBNF (extended Backus Naur Format), and the graphical0based descriptions of syntax used by syntax structure diagrams or railroad diagrams.


BNF and EBNF

BNF/EBNF descriptions of language syntax have these major components:

  • Terminals or symbols which stand for themselves. Terminals can be characters, strings or operators and they are fixed for the sequence.
  • Non terminals which are symbols that represent another structured part of the language. To understand them you need to refer to other structure or structures
  • Rules (called production rules) that patterns or sequences of symbols that may be used in a particular language. Each production rule has a non-terminal symbol on the left hand side and a set of symbol on the right had side that can be terminals or non-teminals. The symbol ::= or = is used to separate the two sides and stands for ‘is assigned to’ for example:

            <digit> ::= 1

        can be written in English as "a 1 is assigned as the meaning of the non terminal symbol called digit".


Railroad Diagrams Introduction