Classwork and Homework

Classwork

Section 3
1) Recursive programming.  Write a program to find sum of all elements in an array, without using loop (recursion only).  You can use the size of the array as a parameter or the "last" element as 0 (sentinel).
2) Write a grammar for a love letter.  Define your own style of the letter.

Homework

Section 3
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.   For recommended free C compiler, see Tools section below.
3.  Modify the compiler to run only as scanner then modify the scanner to accept a new word "chaiyo" in the source. 

Here is the steps to modify the compiler to run only as "scanner".
3.1    Go "main()" in "compile.c"  add the following line to call "lex()" (a scanner function) repeatedly.

readinfile(source);
prolog();
testlex();
exit(0);

3.2    in the same file  delete comments (open up)  "testlex()"  (around line 48).

void testlex(void){
    mylex();
    while( tok != tkEOF ){
        printf(" ");
        prtoken(tok);
        mylex();
    }
}

3.3    You also need to open up the "prtoken()" (in "prtoken.c") by deleting "PRIVATE" keyword (line 16).

// print token
void prtoken(int tk){
. . .
        case tkIDEN: printf("%s", tokstring); break;
        case tkNUMBER: printf("%s", tokstring); break;
        case tkSTRING: printf("%c%s%c",34,tokstring,34); break;
        case tkEOF: printf("eof"); break;
        }
    }
}

by now, you should have a "scanner" working properly.  Here is what the session looks like (assuming your compiler is "rz33.exe").  Run it with "console" (called "cmd" in Windows).  It is a black box that accept command lines (you interact with computers through "command" from the keyboard instead of using mouse and click).

C:\rz33-1\comp\lcc>rz33 fac.txt
 ( n ) { if ( n == 0 ) return 1 ; else return n * fac ( n - 1 ) ; } main ( ) { p
rint ( fac ( 6 ) ) ; }


Notice that the first word has not been displayed properly.  (it should show "fac"). Do you know why?

4.  With your LOVE grammar, write a program to generate letters from it.