---------------------------------------------------------------------------------------

C:                                                         Stop1.for
      PROGRAM Stop1
C: The shortest program does nothing but terminate immediately
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         World.for
      PROGRAM World
C: Every language allows this one!
C: Note that strings lie between 'single quotes' and not "double quotes"
      PRINT *, 'Hello world'
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Case.for
      PROGram case
C: Treat UPPER and lower case as equivalent
      prINT *, 'Hello world'
      stop
      end

---------------------------------------------------------------------------------------

C:                                                         Endline.for
      PROGRAM EndLine
C: Use EOLN where Java programmers would use \n
      PRINT *, 'This is on line 1', EOLN
      PRINT *, 'This is on line 2 '
      PRINT *, 'This is also on line 2', EOLN
      PRINT *, 'This is on line 3', EOLN
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Stop2.for
      PROGRAM Stop2
C: Execution of a program ceases when one reaches a STOP statement
      PRINT *, 'Goodbye cruel world '
      STOP
C: So one would never know that this might not really be suicide after all
      PRINT *, 'I am off to join the circus'
      END

---------------------------------------------------------------------------------------

C:                                                         Stop3.for
      PROGRAM Stop3
C: A program must terminate by reaching a STOP statement, so this
C: example is incorrect and should raise a runtime error
C: Hint: Introduce a new opcode PVM.trap to do this and generate it automatically
C: when END is reached
      PRINT *, 'Hello world'
      END

---------------------------------------------------------------------------------------

C:                                                         Stop4.for
      PROGRAM Stop4
C: A program terminates by reaching a STOP statement, but this does
C: not have to come at the end of the program block
      INTEGER I
      I = 0
10    IF (I .LT. 10) GOTO 20
        STOP
20      PRINT *, I
        I = I + 1
      GOTO 10
      END

---------------------------------------------------------------------------------------
C:                                                         Continue.for
      PROGRAM ContinueDemo
C: Continue statements really do nothing
      CONTINUE
C: Continue statements can be labelled
20    CONTINUE
      STOP
C: Stop statements can also be labelled (of course, this program would never reach
C: the statement labelled 30 at run time)
30    STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Greeting.for
      PROGRAM Greeting
C: Be friendly to our extinguished guest!
      INTEGER Year, Born
      Year = 2004
      PRINT *, 'When were you born? '
      READ  *, Born
      PRINT *, 'That means you are ', Year - Born, ' years old!', EOLN
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Simple1.for
      PROGRAM Simple1
C: Some very simple arithmetic
      INTEGER I, J, K
      I = 10
      J = 12
      K = I * J
      PRINT *, I, J, K
      PRINT *, J - I
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Simple2.for
      PROGRAM Simple2
C: Some very simple Boolean arithmetic
      INTEGER I, J, K
      LOGICAL L1, L2, L3
      I = 10
      J = 12
      L1 = I .LT. J
      L2 = .NOT. L1
      PRINT *, L1, L2
      PRINT *, J .GT. I, L1 .EQ. L2 .OR. .FALSE
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Wrong1.for
      PROGRAM Wrong1
C: This program should not compile
      INTEGER I
C: Variables may not be initialized as they are declared
      LOGICAL Okay = .FALSE.
C: Character constants are not recognized as they might be in Java
      I = 'a'
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Arrays1.for
      PROGRAM Arrays1
C: Arrays are declared (and automatically allocated) along with other variables
C: The maximum index value appears as an integer constant (for example 20)
C: between parentheses
      INTEGER I, LIST(20)
C: You could go "out of bounds" if you were not careful - try reading a negative number
      READ *, I
      LIST(I) = I
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Quotes.for
      PROGRAM Quotes
C: Two quotes in succesion within a string represent a single printable quote
      PRINT *, 'Another of Pat''s diabolical exams!'
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Parrot.for
      PROGRAM Parrot
C: You would rapidly get annoyed if you tried to run this program!
10    PRINT *, 'Pretty Polly '
      GOTO 10
      END

---------------------------------------------------------------------------------------

C:                                                         GoTo1.for
      PROGRAM GoTo1
C: Very simple use of GOTO statements
      INTEGER I
      READ *, I
      GOTO 10
      PRINT *, 'You should not see this string'
      GOTO 20
10    PRINT *, 'You read a value for I = ', I
      GOTO 30
20    CONTINUE
30    STOP
      END

---------------------------------------------------------------------------------------

C:                                                         GoTo2.for
      PROGRAM GoTo2
C: Each label should be defined exactly once, so this program should not compile
      INTEGER I
      READ *, I
      GOTO 10
      PRINT *, 'You should not see this string'
      GOTO 200
10    PRINT *, 'You read a value for I = ', I
      GOTO 30
10    CONTINUE
30    STOP
      END

---------------------------------------------------------------------------------------

C:                                                         ArithIf1.for
      PROGRAM ArithIf1
C: You probably think that the "arithmetic if" was a very funny statement
      INTEGER I
10    READ *, I
      IF (I) 12, 13, 14
12    PRINT *, 'Negative '
      GOTO 15
13    PRINT *, 'Zero'
      GOTO 10
14    PRINT *, 'Positive '
      STOP
15    PRINT *, 'That is quite enough for now!'
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         ArithIf2.for
      PROGRAM ArithIf2
C: Another "arithmetic if" example
      INTEGER I, J
90    READ *, I, J
      IF (I - J) 20, 500, 500
20    PRINT *, 'I is less than J'
      GOTO 30
500   PRINT *, 'I is greater than or equal to J'
30    STOP
      END

---------------------------------------------------------------------------------------

C:                                                         LogIf1.for
      PROGRAM LogIf1
C: The simple Logical IF worked like this
C: (and allowed you to write lots of spaghetti code)
      INTEGER I
10    READ *, I
      IF (I .LT. 0) GOTO 12
      IF (I .EQ. 0) GOTO 13
      GOTO 14
15    PRINT *, 'That is quite enough for now!'
      GOTO 16
12    PRINT *, 'Negative'
      GOTO 15
13    PRINT *, 'Zero'
      GOTO 10
14    PRINT *, 'Positive'
16    STOP
      END

---------------------------------------------------------------------------------------

C:                                                         LogIf2.for
      PROGRAM LogIf2
C: Here are some more examples of the Logical IF
      INTEGER I
      LOGICAL FLAG
      READ *, I
      IF (I .LT. 0) PRINT *, 'Negative'
      IF (I .EQ. 0) PRINT *, 'Zero'
      READ *, FLAG
      IF (FLAG) PRINT *, 'True, true'
      IF (I.GT.0.AND.FLAG) PRINT *, I
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Arrays2.for
      PROGRAM Arrays2
C: Logical IF statements can be used to set up loops
      INTEGER I, LIST(20)
      LIST(4) = 10
C: A loop for setting up part of an array
      I = 1
10    LIST(I) = I
      I = I + 1
      IF (I .LT. 4) GOTO 10
C: Another loop for displaying some of that array "backwards"
      I = 6
20    PRINT *, I, LIST(I), EOLN
      I = I - 1
      IF (I .GE. 1) GOTO 20
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         While1.for
      PROGRAM While1
C: Here are some simple WHILE loops
C: ENDWHILE statements may optionally be labelled
C: The first example goes "up"
      INTEGER I
      I = 0
      WHILE (I .LT. 10)
        PRINT *, I
        I = I + 1
20    ENDWHILE
C: The second example goes "down"
      WHILE (I .GT. 0)
        PRINT *, I, 2 * I
        I = I - 1
      ENDWHILE
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Wrong2.for
      PROGRAM Wrong2
C: The WHILE statement does not follow the condition with a statement on the same line
C: so this program should not compile
      INTEGER I
      I = 0
      WHILE (I .LT. 10) I = I + 1
      ENDWHILE
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         While2.for
      PROGRAM While2
C: WHILE loops can be nested
      INTEGER I, J
      I = 4
      WHILE (I .GT. 0)
        PRINT *, I, 2 * I
        J = I
        WHILE (J .GT. 0)
          PRINT *, J
          J = J - 1
        ENDWHILE
        I = I - 1
      ENDWHILE
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         BadWhile.for
      PROGRAM BadWhile
C: While loops can easily be wrongly coded
      INTEGER I
      I = 0
C: You should not have an ENDWHILE outside a loop
      ENDWHILE
C: A loop that counts up
      WHILE (I .LT. 10)
        PRINT *, I
        I = I + 1
20    ENDWHILE
C: A loop that counts down
      WHILE (I .GT. 0)
        PRINT *, I, 2 * I
        I = I - 1
C: However, this loop does not have an ENDWHILE and so is unacceptable
      STOP
      END

---------------------------------------------------------------------------------------

C:                                                         Sieve1.for
C: Sieve of Eratosthenes for finding primes 2 <= n <= 400 (Toy Fortran version)
C: This version uses GOTO statements to construct loops
      program sieve1
      logical uncrossed(400)
C: counters
      integer max, i, n, k, it, iterations, primes
      max = 400
      primes = 0
      print *, 'How many iterations?'
      read *, iterations
      print *, 'Supply largest number to be tested '
      read *, n
      if (n .le. max) goto 10
        print *, 'n too large, sorry'
        stop
10    it = 1
20    if (it .gt. iterations) goto 100
        primes = 0
        print *, 'Prime numbers between 2 and ' , n , eoln
        print *, '-----------------------------------' , eoln
        i = 2
C: clear sieve
30      if (i .gt. n) goto 40
          uncrossed(i) = .true.
          i = i + 1
        goto 30
40      i = 2
C: the passes over the sieve
50      if (i .gt. n) goto 80
          if (.not. uncrossed(i)) goto 70
            primes = primes + 1
            print *, i
            if (primes .eq. (primes / 8) * 8) print *, eoln
C: now cross out multiples of i
            k = i
60          uncrossed(k) = .false.
            k = k + i
            if (k .le. n) goto 60
70        i = i + 1
        goto 50
80      it = it + 1
90    goto 20
100   print *, eoln, primes, ' primes', eoln
      stop
      end

---------------------------------------------------------------------------------------

C:                                                         Sieve2.for
C: Sieve of Eratosthenes for finding primes 2 <= n <= 400 (Toy Fortran version)
C: This version uses WHILE statements to construct loops
      program sieve2
      logical uncrossed(400)
C: counters
      integer max, i, n, k, it, iterations, primes
      max = 400
      primes = 0
      print *, 'How many iterations?'
      read *, iterations
      print *, 'Supply largest number to be tested '
      read *, n
      if (n .le. max) goto 10
        print *, 'n too large, sorry'
        stop
10    it = 1
      while (it .le. iterations)
        primes = 0
        print *, 'Prime numbers between 2 and ' , n , eoln
        print *, '-----------------------------------' , eoln
        i = 2
C: clear sieve
        while (i .le. n)
          uncrossed(i) = .true.
          i = i + 1
        endwhile
        i = 2
C: the passes over the sieve
        while (i .le. n)
          if (.not. uncrossed(i)) goto 20
            primes = primes + 1
            print *, i
            if (primes .eq. (primes / 8) * 8) print *, eoln
C: now cross out multiples of i
            k = i
60          uncrossed(k) = .false.
            k = k + i
            if (k .le. n) goto 60
20        i = i + 1
        endwhile
        it = it + 1
      endwhile
      print *, eoln, primes, ' primes', eoln
      stop
      end

---------------------------------------------------------------------------------------

C:                                                         While3.for
      PROGRAM While3
C: By the end of this exercise you may be desperate for help
      WHILE (.TRUE.)
        PRINT *, 'help'
      ENDWHILE
      STOP
      END


Summary of useful library classes

  class SymSet { // simple set handling routines
    public SymSet()
    public SymSet(int[] members)
    public boolean equals(Symset s)
    public void incl(int i)
    public void excl(int i)
    public boolean contains(int i)
    public boolean isEmpty()
    public int members()
    public SymSet union(SymSet s)
    public SymSet intersection(SymSet s)
    public SymSet difference(SymSet s)
    public SymSet symDiff(SymSet s)
    public void write()
    public String toString()
  } // SymSet

  public class OutFile { // text file output
    public static OutFile StdOut
    public static OutFile StdErr
    public OutFile()
    public OutFile(String fileName)
    public boolean openError()
    public void write(String s)
    public void write(Object o)
    public void write(int o)
    public void write(long o)
    public void write(boolean o)
    public void write(float o)
    public void write(double o)
    public void write(char o)
    public void writeLine()
    public void writeLine(String s)
    public void writeLine(Object o)
    public void writeLine(int o)
    public void writeLine(long o)
    public void writeLine(boolean o)
    public void writeLine(float o)
    public void writeLine(double o)
    public void writeLine(char o)
    public void write(String o,  int width)
    public void write(Object o,  int width)
    public void write(int o,     int width)
    public void write(long o,    int width)
    public void write(boolean o, int width)
    public void write(float o,   int width)
    public void write(double o,  int width)
    public void write(char o,    int width)
    public void writeLine(String o,  int width)
    public void writeLine(Object o,  int width)
    public void writeLine(int o,     int width)
    public void writeLine(long o,    int width)
    public void writeLine(boolean o, int width)
    public void writeLine(float o,   int width)
    public void writeLine(double o,  int width)
    public void writeLine(char o,    int width)
    public void close()
  } // OutFile

  public class InFile {   // text file input
    public static InFile StdIn
    public InFile()
    public InFile(String fileName)
    public boolean openError()
    public int errorCount()
    public static boolean done()
    public void showErrors()
    public void hideErrors()
    public boolean eof()
    public boolean eol()
    public boolean error()
    public boolean noMoreData()
    public char readChar()
    public void readAgain()
    public void skipSpaces()
    public void readLn()
    public String readString()
    public String readString(int max)
    public String readLine()
    public String readWord()
    public int readInt()
    public long readLong()
    public int readShort()
    public float readFloat()
    public double readDouble()
    public boolean readBool()
    public void close()
  } // InFile


Strings and Characters in Java

The following rather meaningless program illustrates various of the string and character manipulation methods that are available in Java and which will be found to be useful in developing translators.

  import java.util.*;

  class demo {
    public static void main(String[] args) {
      char c, c1, c2;
      boolean b, b1, b2;
      String s, s1, s2;
      int i, i1, i2;

      b = Character.isLetter(c);              // true if letter
      b = Character.isDigit(c);               // true if digit
      b = Character.isLetterOrDigit(c);       // true if letter or digit
      b = Character.isWhitespace(c);          // true if white space
      b = Character.isLowerCase(c);           // true if lowercase
      b = Character.isUpperCase(c);           // true if uppercase
      c = Character.toLowerCase(c);           // equivalent lowercase
      c = Character.toUpperCase(c);           // equivalent uppercase
      s = Character.toString(c);              // convert to string
      i = s.length();                         // length of string
      b = s.equals(s1);                       // true if s == s1
      b = s.equalsIgnoreCase(s1);             // true if s == s1, case irrelevant
      i = s1.compareTo(s2);                   // i = -1, 0, 1 if s1 < = > s2
      s = s.trim();                           // remove leading/trailing whitespace
      s = s.toUpperCase();                    // equivalent uppercase string
      s = s.toLowerCase();                    // equivalent lowercase string
      char[] ca = s.toCharArray();            // create character array
      s = s1.concat(s2);                      // s1 + s2
      s = s.substring(i1);                    // substring starting at s[i1]
      s = s.substring(i1, i2);                // substring s[i1 ... i2]
      s = s.replace(c1, c2);                  // replace all c1 by c2
      c = s.charAt(i);                        // extract i-th character of s
  //    s[i] = c;                             // not allowed
      i = s.indexOf(c);                       // position of c in s[0 ...
      i = s.indexOf(c, i1);                   // position of c in s[i1 ...
      i = s.indexOf(s1);                      // position of s1 in s[0 ...
      i = s.indexOf(s1, i1);                  // position of s1 in s[i1 ...
      i = s.lastIndexOf(c);                   // last position of c in s
      i = s.lastIndexOf(c, i1);               // last position of c in s, <= i1
      i = s.lastIndexOf(s1);                  // last position of s1 in s
      i = s.lastIndexOf(s1, i1);              // last position of s1 in s, <= i1
      i = Integer.parseInt(s);                // convert string to integer
      i = Integer.parseInt(s, i1);            // convert string to integer, base i1
      s = Integer.toString(i);                // convert integer to string

      StringBuffer                            // build strings
        sb = new StringBuffer(),              //
        sb1 = new StringBuffer("original");   //
      sb.append(c);                           // append c to end of sb
      sb.append(s);                           // append s to end of sb
      sb.insert(i, c);                        // insert c in position i
      sb.insert(i, s);                        // insert s in position i
      b = sb.equals(sb1);                     // true if sb == sb1
      i = sb.length();                        // length of sb
      i = sb.indexOf(s1);                     // position of s1 in sb
      sb.delete(i1, i2);                      // remove sb[i1 .. i2]
      sb.replace(i1, i2, s1);                 // replace sb[i1 .. i2] by s1
      s = sb.toString();                      // convert sb to real string
      c = sb.charAt(i);                       // extract sb[i]
      sb.setCharAt(i, c);                     // sb[i] = c
    }
  }


Home  © P.D. Terry