Review: background information on compilers
 

  • Review:

    • Information = data + context

    • data = the binary number stored in a variable

    • context = the data type of a variable

  • Background information:

    • In order to translate statements that contains variables - such as:

         x = y + z;
      

      the compiler must know the data type of every variable in the expression

The 2-pass compilers

Two-pass compilers:

  • A 2-pass compiler reads the source code twice to compile (= translate) a program

  • In the 1st pass, the compiler will:

      • Gather the (type) definition of all variables and functions

  • In the 2nd pass, the compiler will:

      • Use the gathered (type) information on the variables and functions to translate the function calls

        (E.g., insert the necessary conversion instructions if the data types are mismatched)

A feature of a two-pass compiler: Java

A feature of a 2-pass compiler:

  • A 2-pass compiler allows variables to be defined after they were used

Example:   the 2-pass Java compiler can compile this code:

  public class demo
  {
     public static void main(String[] args)
     {
       System.out.println(x); // Use variable before its definition 
     }        // Allowed because javac read input file twice !

     static int x = 1234;     // Definition of variable x  
  }
   

DEMO:   /home/cs255001/demo/c/set1/java2pass.java (seen it before !)

The 1-pass compilers

One-pass compilers:

  • A 1-pass compiler reads the source code once to compile (= translate) the program

  • In the one-pass compiler, the compiler will:

      • Gather the definition of all variables and functions while it is translating the program !!!

Notable fact about 1 pass compilers:

  • When a 1-pass compiler processes a statement in a program, it cannot use the information that are given below that statement in the program !!!

A feature of a one-pass compiler: C

A feature of an 1-pass compiler:

  • An 1-pass compiler can not compile a program where variables are defined after they were used

Example 1: the 1-pass C compiler can compile this program:

  #include <stdio.h>

  int x = 1234;         // Definition of variable x  

  int main( )
  {
     printf("%d\n", x); // Use variable x 
  }
   

DEMO:   /home/cs255001/demo/c/set1/c-1pass.c (seen it before)

A feature of a one-pass compiler: C

A feature of an 1-pass compiler:

  • An 1-pass compiler can not compile a program where variables are defined after they were used

Example 2: the 1-pass C compiler cannot compile this program:

  #include <stdio.h>

  int main( )
  {
     printf("%d\n", x); // Use variable x before it was defined
  }

  int x = 1234;         // Definition of variable x  
   

DEMO:   /home/cs255001/demo/tmp/demo.c

How the C compiler solve the "missing type information" problem

  • The "missing type information" problem in C programs is solved using:

      • The variable declaration technique

  • Declaring a variable:

      • Declaring a variable = conveying (1) the name and (2) the data type of a variable to the compiler

  • Syntax to declare a variable in C:

     extern  data-type  variableName ;   

  • Example:

     extern  int  x ;  // Tells the C compiler that 
                       // the global var x has data type int 

Example variable declaration

  • This program cannot be compiled correctly:

      #include <stdio.h>
    
    
    
      int main( )
      {
         printf("%d\n", x); // Use variable x before it was defined
      }
    
      int x = 1234;         // Definition of variable x 
      

    because the data type of the variable x is not known when x was used in the printf( ) statement.

Example variable declaration

  • The declaration of the variable x will solve the problem:

      #include <stdio.h>
    
      extern int x;
    
      int main( )
      {
         printf("%d\n", x); // Statement can now be translated     
      }                     // correctly                      
    
      int x = 1234;         // Definition of variable x 
      

    Now the data type of the variable x is known (int) when x was used in the printf( ) statement.

DEMO with:   /home/cs255001/demo/c/set1/c-1pass.c (edit it)

Difference between a variable definition vs. a variable declaration

  • Defining a variable consists of the following actions taken by a compiler:

      • Record the name (identifier) of the variable
      • Record the data type of the variable
      • Allocate (= reserve) memory space to store the value of the variable

  • Declaring a variable consists of the following actions taken by a compiler:

      • Record the name (identifier) of the variable
      • Record the data type of the variable

  • Rule:

      • You can define a variable only once

      • You can declare a variable many times (as long as the declarations do not conflict with one another)

Final comments

  • Declarations can only be used with global variables, not for local variables !

      #include <stdio.h>
    
      // Declare global variable here <------------
    
      int main( )
      {
         printf("%d\n", x); // Use variable x before it was defined
      }
    
      int x = 1234;         // Global variable x  

  • Local variables are only accessible within the same function in which the local variables are defined and they do not need to be declared

  • There are other situations (a C program that consists of multiple files !) where declarations are crucial (we will cover them later !)