Back

Syntax analysis

Syntax analysis
Once the source code has been passed through the lexical analysis program, the tokens generated are passed to the syntax analysis program for the next stage translation. This stage checks to see if the program makes sense - the program semantics.

Analysis of the token stream from lexical analysis
Following lexical analysis, the syntax of the program is checked. The first job is to take the long stream of tokens from the lexical analysis stage and to split them up into phrases. Each phrase can then be checked against the rules of the programming language (the syntax of the language). Sentences in English must be constructed according to rules. So for example,

"The computer helps me run my business." is okay but you cannot write,
"Computer business run the me helps my." because it breaks the syntax rules of English.

Here is another example of what this means during compilation. Consider the instruction:

IF|SizeOfEngine|>|2000|THEN|Payment|:=|Mileage|*|40          During lexical analysis it is converted to:

TOKEN/IF|VARIABLE|RELATION|CONSTANT|TOKEN/IF|VARIABLE|TOKEN/ASSIGN|VARIABLE|TOKEN/MATHS|CONSTANT

When this is checked against the allowable patterns in syntax analysis, it is found to be fine. Now consider the same instruction that has been written incorrectly, not following the syntax rules.

THEN |SizeOfEngine|>|2000| IF|PaymentMileage|*|40|:=

This would generate an unacceptable pattern of tokens during lexical analysis. During the syntax analysis, therefore, an error would be generated because the pattern of tokens did not match any in the compiler’s database of allowable patterns.

It is a relatively easy job for a compiler to keep a record of allowable patterns for a high-level language. One method for describing each pattern is known as Backus-Naur form, or BNF. This is dealt with later. When the syntax analysis stage is being carried out, each phrase of code is checked against the allowable patterns. This is known as parsing the code. If a match cannot be made, then the report generator generates an error.

Back