Interpreter

Program

A program keyword sets up the global namespace for the program. Program name is any valid identifier. Below is the syntax for defining a Program.

PROGRAM main; { PROGRAM <program_name>;}

Example

PROGRAM helloWorld;
BEGIN
    display('Hello World!');
END.

Block

There are two blocks in the Programming language. They are given below.

  1. Declaration Block
  2. Compound Statement Block

A detailed discussion on each block is given in the next section. Syntax for declaring both the blocks is shown here.

Syntax

PROGRAM block;

{ Declaration Block }
VAR
     name : STRING;
     { All the variables are declared here }

{ Compound Statement Block }
BEGIN
     name := 'Henry';
     { Other code statements comes here }
END.

Declaration Block

Variable declaration should be done at the top just after defining the program name. Variable Declaration starts with keyword VAR. Syntax for variable declaration is shown below. This block is optional i.e a program can be written without declaration block if there are no variables used.

VAR
    <variable_name_1> : <type_1>;
    <variable_name_2>, <variable_name_3> : <type_2>;

Example

PROGRAM varDecl;
VAR
     a : INTEGER;
     b : REAL;
BEGIN
     a := 2;
     b := 3.00;
     display('a:', a);
     display('b:', b)
END.

Compound Statement Block

Unlike declaration block compound statement block is not optional. There should be atleast one compound statement block within the program. A coumpound statement block starts with BEGIN keyword and ends with END keyword. A compound statement block can have nested compund statement blocks. Compound statement block can also be empty.

Compound statement block must be written after the declaration block.

Syntax

{ Compound Statement Block }
BEGIN
    { Code statements ... }
END.

Example 1

PROGRAM compoundStatementBlock;

{ Declaration Block is optional }

{ Compound Statement Block }
BEGIN
     { Compound statement block can be empty }
END.

Example 2

PROGRAM nestedCompoundStatementBlock;

{ Declaration Block is optional }

{ Compound Statement Block }
BEGIN
    BEIGN
       { ... }
    END;
END.

Data types

Pascal's Programming language supports for 4 data types.

  1. Integer
  2. Real
  3. String
  4. Boolean
  5. Array

1. Integer

An integer can be 0, positive or negative number without decimal point. Below is the example the shows the integer declaration.

number := 22;

2. Real

Any number with decimal point is a real number. Below is the example the shows the real number declaration.

pi := 3.14;

3. String

A string is a group of zero or many characters. String should be wrapped up in single quotes ('). Escape sequence (\) can be used to print single quote(') or backword-slash(\). Below is the example the shows the string declaration.

name := 'R V College'; 
student := 'Student\'s Name'; { Student's Name }
slash := '\\'; { \ }

4. Boolean

Boolean can be True or Fasle. Below is the example the shows the Boolean declaration.

isValid := True;
isNew := False;

5. Array

Array is collection of values, values can be of any type. Values can be of array type even. Array starts with "[" and ends with "]" and each value in array is seperated by ",". Below is the code that showa the syntax of array declaration.

arr := [1, 'string', 2.00, True, [1,2,3]];

Example

Below is the code example that shows the variable declaration and usage for all the data-types

{ Data types }
PROGRAM dataTypes;
VAR
     name : STRING;
     age : INTEGER;
     salary : REAL;
     isAdult : BOOLEAN;
     arr : ARRAY;
BEGIN
     name := 'Henry';
     age := 24;
     salary := 100000.00;
     isAdult := True;
     arr := [name, age, salary, isAdult];
     display(arr);
END.

Operators

There are 5 types of Operators defined in the language syntax

  1. Assignment Operator
  2. Arithmentic Operators
  3. Logical Operators
  4. Comparision Operators
  5. Concatenation Operator

1. Assignment Operator

Assignment operator is used to assign a value to a variable. Below code bloack shows the syntax and usage of assignment operator.

name := 'Henry' { 'Henry' string value is assigned to variable 'name'}

2. Arithmentic Operators

Arithmentic Operator Includes:

  1. Unary Operator
  2. Addition ( + )
  3. Subtraction ( - )
  4. Multiplication ( * )
  5. Integer Division ( DIV )
  6. Float Division ( / )

Example

{Arithmetic Operators}

PROGRAM arithmeticOperator;
VAR
     a,b,c,d,e : INTEGER;
     x, y, z : REAL;
BEGIN
     a := 1;
     b := 2 + a; 	                 { ADDITION }
     c := b - a;  	                 { SUBTRACTION }
     d := b * 5;  	                 { MULTIPLICATION }
     e := ---++c;	                 { UNARY OPERATOR }
     x := d DIV b;                  { INTEGER DIVISION }
     y := x / c;	                 { REAL DIVISION }

     z := (a + b) * c -- d DIV x /3; { COMBINATION }

     display('a:', a, 'b:', b);
     display('c:', c, 'd:', d);
     display('x:', x, 'y:', y);
     display('z:', z);
END.

3. Logical Operators

Logical operators returns Boolean. There are 2 logical operators in the language, they are given below:

  1. Logical AND Operator
  2. Logical OR Operator

3.1 Logical AND operator

It returns True if both of the operands are of Truthy value. Below is the truth table diagram for the AND logical operator.

T/F

FALSE

TRUE

FALSE

FASLE

FALSE

TRUE

FALSE

TRUE

3.2 Logical OR operator

It returns True if any of the operands are of Truthy value. Below is the truth table diagram for the OR logical operator.

T/F

FALSE

TRUE

FALSE

FALSE

TRUE

TRUE

TRUE

TRUE

4. Comparision Operator

The result or the final value returned by the comparison operator is a boolean. It can be True or False. Comparision operator Includes:

  1. Less than Operator ( <)
  2. Less than or equal Operator( <= )
  3. Greater than Operator( > )
  4. Greater than or equal Operator( >= )
  5. Equal Operator( = )
  6. Not Equal Operator( != )

Example

{Comparison Operators}

PROGRAM comparisonOperator;
VAR
     a,b,c,d,e: INTEGER;
     x,y,z : BOOLEAN;
BEGIN
     a := 1;
     b := 5;
     c := 2;
     d := 10;

     x := a > 2 and 2 < 3 or b > 4;
     y := b = 5;
     z := a >= 4 and c <= 3;

     display('a:', a, 'b:', b);
     display('c:', c, 'd:', d);
     display('x:', x);
     display('y:', y);
     display('z:', z);
END.

5. Concatenation Operator

Concatenation Operator is used to concatenate two strings. $ symbol is used as concatenation operator. Below code example shows the usage of concatenation operator

Example

{ Concatenation Operator}

PROGRAM concatenationOperator;
VAR
     firstname, lastname : string;

BEGIN
     firstname := 'RV';
     lastname := 'COLLEGE';

     display(firstname $ ' ' $ lastname);
END.

Operator Precedence

To evaluate the expressions there is a rule of precedence in language. These precende rules guide the order in which these operations are to be carried out.

The operator precedence in language is listed below in the following table. It is in the descending order (upper group has higher precedence than the lower ones).

Operator
Meaning
( )
Parentheses
$
Concatenation Operator
+X, -X
Unary Plus, Unary Minus
*, DIV, /
Multiplication, Integer Division, Real Division
+, -
Addition, Subtraction
=, !=, <, >, <=, >=
Comparison Operators
AND
Logical AND
OR
Logical OR

If Statements

If statements are used when there is requirement of condition based evaluation. Here first the condtion is checked and then, only its associated code block is executed.

There are 4 varieties of If statements

  1. if statement
  2. if else statement
  3. if elseif elsif ... elseif statement
  4. if elseif elsif... else statement

Note that all the if statement varieties should be terminated with ENDIF keyword;

Syntax

IF <condition-1> THEN
     {statements to execute ...}
ELSEIF <condition-2> THEN
     {statements to execute ...}
ELSEIF <condition-3> THEN
     {statements to execute ...}
  .
  .
  .
ELSE
     {statements to execute ...}
ENDIF

Example

{ VOTER }

PROGRAM voting;
VAR
     age : INTEGER;
     hasCriminalRecord : BOOLEAN;

BEGIN
     age := 19;
     hasCriminalRecord := False;

     IF age >= 18 and hasCriminalRecord = False THEN
         display('Eligible for Voting');
     ELSE
         display('Not Eligible');
     ENDIF
END.

Loop

Loop is used to repeatedly execute a group of statements as long as the required condtion is met. Once the condtion fails. If there is else satatement present, then the else block will be executed once. Otherwise the statements after loop will be executed.

Note that else block will be executed even if the loop is terminated with break statement.

Below is the code example for using loop in program.

Syntax

LOOP IF <condition>
     {statements to execute ...}
ELSE
     {statements to execute ...}
ENDLOOP

Example

PROGRAM loopExample;
VAR
     a : INTEGER;
     b : INTEGER;
BEGIN
     a := 0;
     LOOP IF (a < 5)
         a := a + 1;
         display('Current value of a:', a);
     ELSE
         b := 1;
         display();
         display('Out of loop');
         display('Value of b:', b);
     ENDLOOP
END.

Example Codes

Below are some of the example codes written in the current programming language.

Example 1: Print Numbers from 1 - 20.

{ Displaying numbers from 1 to 20 }
PROGRAM main;
VAR
     range : INTEGER;
     count : INTEGER;
BEGIN
     range := 20;
     count := 0;

     LOOP IF (count < range)
          count := count + 1;
          display('Number is :', count);
     ENDLOOP
END.

Example 2: Factorial of 10

{ Displaying numbers from 1 to 20 }
PROGRAM factorial;
VAR
     counter, fact : INTEGER;

BEGIN
     counter := 1;
     fact := 1;

     LOOP IF (counter < 10)
          fact := fact * counter;
          counter := counter + 1;
     ENDLOOP

     display('factorial of 10 :',fact)
END.

Example 3: Simple Interest

{ Simple Interest }
PROGRAM simpleInterest ;
VAR
     principle : REAL;
     rate : REAL;
     year : INTEGER;
     amount : REAL;
     si : REAL;

BEGIN
     principle := 10000;
     rate := 5;
     year := 1;

     si := principle * year * rate / 100;

     amount := principle + si;

     display('Principle :', principle);
     display('Rate :', rate);
     display('Time :', year);

     display('Simple interest :', si);
     display('Amount :', amount)
END.

Example 4: Fibonacci Series

{ Fibonacci Series }
PROGRAM fibonacciSeries ;
VAR
     terms, count : INTEGER;
     prevResult, prevPrevResult : INTEGER;
     currentResult : INTEGER;

BEGIN
     terms := 10;
     count := 2;
     display('Fibonacci Series:', terms, 'terms');

     IF (terms = 1) THEN
           display('0');
     ELSEIF (terms > 1) THEN
           display('0');
           display('1');
           prevResult := 1;
           prevPrevResult := 0;

           LOOP IF (count < terms)
                 currentResult := prevResult + prevPrevResult;
                 prevPrevResult := prevResult;
                 prevResult := currentResult;
                 count := count + 1;
                 display(currentResult);
           ENDLOOP;
      ENDIF
END.