Classwork and Homework for Section 3
Classwork
Section 3
1. Write a recursive program to find a max value from an array
of integer. Write it in Rz.
2. What is the meaning of this regular expression?
Draw the state diagram and try to explain what it does.
/*
([^*]*[^/]*)* */
3. Design a domain specific language. Here is the FB language.
4. Write s-code from the following Rz programs:
5.1 a,b,c are local
variables:
if (a < 2) b = c; else c = 11;
5.2
len(e) {
if( e == 0 ) return 0;
else return 1 + len(tail(e));
}
assume that tail(.) is an already defined function.
Homework
Section 3
1. Familiarize with the compiler tool. Use rz33-1.zip to compile and run
your find-max recursive in the classwork1.
Here is what a session looks like:
Go to rz33/test directory (that you
unzip the package to). There are two executable
files: rz33.exe and somv2x.exe
Try to compile "fac.txt". It is shown here:
// factorial
fac(n){
if( n == 0 ) return
1;
else return n *
fac(n-1);
}
main(){
print(fac(6));
}
Here is the command line and the output at the
screen:
D:\prabhas\bag\rz\rz33\test>rz33 fac.txt
fac
main
(fun main (print (call fac 6
)))
(fun fac (else (== #1 0
)(return 1 )(return (* #1 (call fac (- #1 1 ))))))
:main
fun.1
lit.6
call.fac
sys.1
ret.1
:fac
fun.1
get.1
lit.0
. . .
You will get the file "fac.obj" as an
output file. It is an object file that is executable under Som
VM (a kind of virtual machine similar to JVM). You can "run" it
under somv2x.
D:\prabhas\bag\rz\rz33\test>somv2x fac.obj
720
That's it. Enjoy!
2. (Do not have to hand in anything) Download
rz33-1 and try to compile the source. Use the
executable rz33.exe and somv2x.exe to compile and run your
program. Learn how to write in Rz by reading Quick
Start Rz.
3. Write a parser for FB language.
4. Read "How to
compute First and Follow set"
5. Given Rz grammar
here, find its First and Follow sets. Restrict the
grammar to be the left column only. To reduce the size of the
grammar, all non-terminal symbols that appear at the right column
are considered to be terminal symbols.
Project
Write a short report of your study on the topic of "The
relationship of scripting languages and
compilers/interpreters". The report must include:
1) A short description of your chosen language
(scripting language: javascript, python, php, etc.)
2) Ask yourself how this language is
compiled? Or what it is virtual machine?
3) Collect information and digest it then write in
your own language.
4) Expect around 4 pages of report.
5) Hand in by 13 July 2012, 4pm. I will not
accept any submission after this time.
This will consume about 6-9 hours of your time. The report can
be in either Thai or English.
End