LARA

Compilers in Action

gcc Compilers for C

Example program in C programming language

#include <stdio.h>

int main(void) {
  int i = 0;
  int j = 0;
  while (i < 10) {
    printf("%d\n", j);
    i = i + 1;
    j = j + 2*i+1;
  }
}

We will pay attention to line:

j = j + 2*i + 1

Example command line invoking GNU C Compiler:

gcc test.c -S

x86 Assembly view of the resulting output:

test.s

from this the compiler could generate executable machine code (e.g. Linux Executable and Linkable Format (ELF) or EXE format for MS platforms)

javac Compiler from Java to JVM

class Test {
public static void main(String[] args) {
  int i = 0;
  int j = 0;
  while (i < 10) {
    System.out.println(j);
    i = i + 1;
    j = j + 2*i+1;
  }
}
}

To compile above Test.java into Test.class :

javac Test.java

To view Test.class in human-readable way:

javap -c Test

test.jvm

In this class, you will be building a compiler