Compiler

Aim:  We learn how a compiler work. We study theories behind the translation (compile) methods. We write a specification of a simple language (a grammar). We build a simple compiler from prefabricated parts (my compiler here). 

Theory of Grammar, especially Context-Free Grammar 
Parser, top down, LL(1)
Write a grammar
Compiler package  rz37ds.zip
S2 processor and its instruction set

Building a compiler

The first run, compile the whole compiler and test that it works.
1)  unzip the rz37ds.zip package.
2)  the compiler is in the directory /comp
3)  use your tools (C compiler) to compile it.  Let the compiler executable be "rz37".
4)  this compiler compiles an input file of Rz language

see what is Rz language here

and it produces an assembly language output (to be translate to machine codes and run on an appropriate processor).

Example  
Rz input file:

// a test file, Rz language

ax[10]

findmax(a)
  max = a[0]
  i = 1
  while( i < 10 )
    if( max < a[i] )
       max = a[i]
    i = i + 1
  return i

main()
  print(findmax(&ax))

// -------------------

run this command

c>test/rz37 max.txt

and you will get this output on the screen. It is the assembly language of S2 processor.  You can read about S2 processor and its instruction set here.

.symbol
 ...
 ax 5000
.code 100
...
 st r1 20
 jal rads main
 trap r0 #0
...
:findmax
st r1 @1 fp
...
pop sp r1
; (= #2 (vec #1 0 ))
mov r5 #0
ld r4 +r1 r5
mov r2 r4
; (= #3 1 )
mov r3 #1
; while ; (< #3 10 )
jmp L102
:L103
; if ; (< #2 (vec #1 #3 ))
ld r5 +r1 r3
lt r4 r2 r5
jf r4 L104
; (= #2 (vec #1 #3 ))
ld r4 +r1 r3
mov r2 r4
:L104
; (= #3 (+ #3 1 ))
add r3 r3 #1
:L102
lt r4 r3 #10
jt r4 L103
; end while
; (return #3 )
mov retval r3
...
ld r1 @1 fp
ret rads

; function
:main
st r1 @1 fp
add fp fp #2
st rads @0 fp
; (print (call findmax (& ax )))
mov r1 #ax
push sp r1
jal rads findmax
trap r28 #1
ld rads @0 fp
sub fp fp #2
ld r1 @1 fp
ret rads
.data 5000
.end

; ---------------------------

<to be continue>

last update 10 Aug 2013



  