2110316  Programming Languages
      Principles 3(3-0-6)
    1st semester  2013
    alternative site http://db.tt/sVSI4R54
    
    Prabhas Chongstitvatana
    official course description
    
    Principal lecturer: Twittee.  This course is divided into three equal
    parts.  Three parts are taught by three different lecturers.
    
    1) Programming Language concepts, Twittee 
    2) Non-imperative programming language, Vishnu   
    3) Language and implementation, Prabhas
    
    The goal of this course is to make you understand the languages you use. To
    make you appreciate diversity of ideas in programming and prepare you for
    new programming methods and paradigms. Theoretical foundations will be
    presented. You will know properties of language, not just syntax. Moreover,
    you will recognize the cost of presenting an abstract view of machine and
    understand trade-offs in programming language design.  
    
    Each part will be taught separately and independently. It is logical that
    the assessment will also be arranged according to this structure. There is
    no midterm exam (besides whatever assess by the lecturer at that time) and
    the final exam will contain all materials taught in the course.
    Assessment
    each part  20%  by 3  = 60%
    final
    exam            
             40%
    
    the lecture schedule:
    section 1: T, V, P  
    section 2: V, P, T
    section 3: P, T, V
    Language and implementation
    This part concerns a compiler for a programming language. There are two
    aspects of learning this part: theory and practice.  The theory will be
    given in the lectures.  The practice is carried on as homework and
    classwork. To teach effectively and to enrich your C programming skill, I
    choose to design a toy language and implement its compiler in C. You will be
    studying an actual compiler and modify it.
    
    old lecture  2009  2010 
      2011  2012
    Announcement
    4 June 2013   Add alternative site
    for this webpage. 
    Please visit the site and bookmark it.  Presently, the departmental
    site is not stable.
    18 June 2013  Add reference to Surreal Numbers
    25 June 2013  Section 3: project announced. deadline is 11 July. 
    I promise to up old exam questions soon.
    9 July 2013     Quiz for Section 3 is here. 
    deadline is 18 July, 4pm. Hand in to Khun Su, floor 17, admin. office.
    15 Aug 2013   Quiz
      for current section is here  It will be a take-home. The deadline
    to hand-in the answer is 19 Aug, 4pm. Hand in to Khun Su, floor 17, admin.
    office.
    19 Sept 2013   Quiz take home 
    Please hand-in to Khun Su at the office before 4pm Monday 23 Sept.
    Study Plan
    plan for 4 weeks, with one week to spare
    each week has 2 sessions of 1 1/2 hrs. each.
    a homework will be handed out each week.
    one project will be issued on week 3.
    
    workload
    
    one small project
    one in-class exam
    weekly homework, running the code
    Lecture sessions
    1  structure of a compiler      Overview
      of the course   (from Stanford slide)    Compiler (ppt)
       High Level Language  to  Low Level Language 
    to  Processor architecture 
       Demonstrate the actual compiler of this course RZ.  
       Recursive programming with
      List  
    2  lexical analyser      Scanner
    (ppt) 
    --------------------------
    3 
    grammar              
    Context Free Grammar  (ppt)
    4  parsing               
      Parsing (ppt)   
    top-down parsing   How to compute First
      and Follow set  (by Prof. Kamin at UIUC)
                                   
    LL parser at Wiki
    ----------------------------
    5  actual parser        
    project announcement         
    6  code generator        Code
      Generation (updated)  
      Som v2.0 virtual machine   S-code
        recursive evaluator    here is the source
    code in C for an interpreter of Rz parse tree   
      eval3.c
    ---------------------------
    8  actual code generator  How to do code
    generation     Zero Assember
      
       exam
    -----------------
    9-10 spare  additional topics   
    Assessment for this part
    homework        5% 
    one project      
    5%         
    exam       
          10%   
    total              
    20%
    Homework and project 
    
    Section 1
    
    Classwork
    Section 2
    ...
    
    Homework
    Section 2
    1.  Learn how to write in Rz by reading  Quick
      Start Rz. 
    2.  Download and compiler the compiler used in this class (rz33-1.zip). 
    Use whatever compiler for C that you are familiar with and compile it. Try
    it out to compile some simple program.
    3.  Write a "recursive" program to copy a complex list. 
    4.  Figuring out what this regex means  /* ([^*]*[^/]*)* */
    Project
    Design a Domain Specific Programming Language (DSL) (look it up in wiki).
    Write a short report of your study on the topic. Your work must include the
    following:
    1)  Motivation, what is your language good for?  why you did it
    this way?
    2)  Description of the language, in an informal way. That should
    include,
    3)  Examples of the use of your language.
    4)  Write the Grammar for your language.
    bonus:  If you can compare your language with existing language (which
    is related) you will get additional "like" :-)
    5)  I expect around 4 pages of report. But it should take as many pages
    as needed to explain your design. 
    6)   Hand in by 22 Aug 2013, 4pm. at the box in front of my
    office. I will not accept any submission after this time.
    
    Hint:  Make your language small, (or you can show just a beautiful
    subset of the language).  It is a big job to design a full and complete
    language.  For example, the inventors of C language wrote a whole book
    (although not very thick) to describe it including how to program in
    C.  I think C grammar took about 10 pages long.
    
    This will take about 10 hours of your time.  The report can be in
    either Thai or English. The length of the report is about 4-5 pages.  I
    am interested in the "quality" of the report not the "quantity".  You
    should try to explain your idea in your own word. 
    
    Reference Text
    -- Aho, Sethi, Ullman, Compilers: Principles, Techniques, and Tools.
    Addison-Wesley, latest edition.
    -- Louden, K.C., Compiler Construction: Principles and Practice. PWS
    Publishing Co., 1997.
    
    The first one is the standard text book on compiler. It has been used in
    more than 100 universities in North America.  It is a bit difficult to
    read as it contains a lot of theory.   The second one (Louden) is
    much easier to read.  I will update some chapter from my textbook from
    time to time as necessary.
    Extra reading
    
    
    Tools
    See
      my Rz language homepage
    the compiler  package with code generator for s-code 
      rz33-1.zip  
    Zero Assembler: source, example and executable
    (including som v2 vm) source:  zas.c  
    How to use the compiler
    Use rz33-1.zip  to compile and run your programs.   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!
    
    Prabhas Chongstitvatana
    contact address:   prabhas at chula dot ac dot
    th     
    office   room 18-13  Engineering Building 4, floor 18. 
    tel 02-2186982
    research lab:  Intelligent Systems,  floor 20.
    
    Last update  19 Sept 2013