Programming Languages
(04/20/2008)


Machine Code/Assembler/High Level Languages/Natural Languages: Computer languages exist on a spectrum from machine oriented languages (machine code) to human oriented languages (high-level languages)

    More Information


Code Translation: Programs in languages other than machine code must be translated into machine code. Translations can be done one of two ways:  Interpretation is the translation technique where each individual statement in a program is first translated, then executed (Python and the original BASIC use interpretation). With compilation the entire program is first translated into machine code then executed (most other languages use this). With compilation, execution is faster since the "price" of translation is paid "up-front". However interpreted languages are more “flexible”. There are also hybrid translation systems where the source code it translated into an intermediate code which is later interpreted (e.g. Java).

·         Assemblers

o   one to one translation: each assembler statement generates one machine code instruction (since assembler is mnemonic code for machine code)

o   translation is done by table look up where symbolic names are cross-listed with their numeric values

o   translating assembler into machine code usually requires two passes through the code.

§  during pass one, a user-defined symbol table is constructed consisting of the numeric addresses for all symbols used by the programmer

§  during pass two, table look-up is used to translate the assembler code into machine code  

·         High-Level Language Compilers & Interpreters

o   many to one translation

o   there are four stages to high-level language translation

§  lexical analysis - identification of syntactic elements ("tokens")

§  parsing - checking that the program is syntactically correct

§  code generation - translating  syntactically correct high-level code into machine code

§  optimization - refining the machine code to make it more efficient (compilers only)

 


"The structure of language defines the boundaries of thought" : Sapir-Whorf hypothesis

High-Level Language Paradigms (The Tower of Babel) - Over time many high-level computer languages were created. Some languages were created to "fix" certain perceived problems in early languages. Others were created to enhance a particular way to solve a problem. Some languages were created to specifically teach programming. Others were created to force the programming to write robust code (i.e. code that would execute correctly). Some languages were "small"; others were "large". Sufficient to say, there is no "perfect" language; every language has its good points and its bad points.
 

       More Information


Examples of Imperative, Functional and Logical Programs

Fortran-I Code to detect small primes < 100 (imperative)

Note XMODF(N,M) is the Fortran I modulo function returning n % m

      READ 1, N

 1    FORMAT (I3)

           IF (N - 2) 10, 100, 10

10    IF (XMODF(N, 2)) 20, 50, 20

     20    IF (N - 3) 30, 100, 30

30    IF (XMODF(N, 3)) 40, 100, 40

     40    IF (N - 5) 50 ,100, 50     

50    IF (XMODF(N, 5)) 60, 100, 60

     60    IF (N - 7) 70, 100, 70

70    IF (XMODF(N, 7)) 80, 100 80

80    PRINT 2, N

 2    FORMAT (I3,9H IS PRIME)

      STOP

   100     PRINT 3, N

3     FORMAT(I3, 13H IS NOT PRIME)

      STOP   

Fortran I program to compute n factorial (imperative)

     READ 1, N

1    FORMAT (I3)

     NFACT = 1

     DO 100 I=1, N

     NFACT = NFACT * I

100  CONTINUE

     PRINT 2, NFACT

2    FORMAT (I6)

     STOP   

BASIC Program to compute n factorial (imperative)

    10 INPUT "Enter an integer ", N

    20 F = 1

    30 FOR I = 1 TO N

    40 F = F * I

    50 NEXT I

    60 PRINT F

    70 END  

Scheme Program to compute n factorial (functional)

 (define (factorial n)

    ( if ( = n 0)

             1

         ( * n (factorial ( - n 1)))

        )

 )

 

Scheme Code to detect a small prime < 100 (functional)

( define (smallprime n)

    (cond

            (( = 2 n) #t)
            (( = 3 n) #t)
            (( = 5 n) #t)
            (( = 7 n) #t)

        (( = (remainder n 2) 0) #f)

        (( = (remainder n 3) 0) #f)

        (( = (remainder n 5) 0) #f)

        (( = (remainder n 7) 0) #f)

        (else #t)

    )

)

Prolog Program to detect Who is Mortal (logic oriented)

predicates

   man(symbol)

        mortal(symbol)

clauses

        man(socrates).

   man(plato).

   man(aristotle).

        mortal(X) if man(X).

 Prolog Code to Detect Small Primes < 100 (logic oriented)

    predicates

       smallprime(integer)

            prime(integer)

    clauses

            smallprime(2).

       smallprime(3).

            smallprime(5).

       smallprime(7).

       prime(X) :- smallprime(X) or

                   (X mod 2 <> 0) and

                   (X mod 3 <> 0) and

                   (X mod 5 <> 0) and

                   (X mod 7 <> 0).



Special Purpose Languages: The languages listed above are all general purpose in that each is capable of solving a wide range of problems. However there is a class of special purpose languages that are more narrowly aimed at solving specific problems.


A Brief History of Some Important High-Level Procedural Languages
 


Return to Comp 150 HomePage