Top-Down Parser (Part 3 of 7)


Date assigned: 2/28/07
Date due: 3/9/07
Points: 50

You are to write the top-down portion of the parser using the recursive decent method for the grammar listed below.

1) The input to your parser will be a program written in our subset of C based on the grammar below.

2) Output the name (one per line) of each function as it is entered. Further, make the names of your functions exactly the same as the names of their respective nonterminals in the grammar below. I would build in a machanism that is easy to turn on and off the printing of each function as it is entered. You will find this a useful debug feature as you build your compiler.

3) When you run into EXPRESSION, call a function to eat tokens until any of the following are found: semicolon, comma, right paren, or right brace.

C Subset Grammar
program      -> externaldefs mainprogram
externaldefs -> int externaldef | e
externaldef  -> id typepart externaldefs | * id vardecl ; externaldefs
typepart     -> ( optparamlist ) functionbody | vardecl ;
vardecl      -> [ constant ] optinit moreinitdecls | optinit moreinitdecls
optparamlist -> id moreparams | e
moreparams   -> , id moreparams | e
functionbody -> typedecllist functionstmt
typedecllist -> int idorptr optarraydecl moredecls ; typedecllist | e
idorptr      -> id | * id
optarraydecl -> [ ] | e
moredecls    -> , idorptr optarraydecl moredecls | e
functionstmt -> { optdecllist stmtlist }
optdecllist  -> int idorptr vardecl ; optdecllist | e
optinit      -> = initializer | e
initializer  -> EXPRESSION | { initlist }
initlist     -> EXPRESSION moreinit
moreinit     -> , EXPRESSION moreinit | e
moreinitdecls-> , idorptr vardecl | e
stmtlist     -> statement stmtlist | e
statement    -> if ( EXPRESSION ) statement optelse |
                for ( EXPRESSION ; EXPRESSION ; EXPRESSION ) statement |
                return optexp ; |
                ; |
                { stmtlist } |
                EXPRESSION ; |
                input ( arglist ); |
                output ( arglist );
optexp       -> EXPRESSION | e
optelse      -> else statement | e
mainprogram  -> main ( ) functionstmt
arglist      -> EXPRESSION morearglist
morearglist  -> , arglist | e

Note1: Remember, your compiler is to be in a directory called yourlastname. In that directory there is to be a makefile that I simply use to compile your project creating the executable pcc. Continue creating subdirectories within yourlastname directory for each of the modules needed as we go through the compiler creation process; thus, for this third assignment, you will need to create at least a subdirectory called td where all of the top-down parser routines will go. Also, continue to use subversion as we build this project.

Note2: Use the submit script to submit your solution: zeus$ submit cs480s07 user.tar.gz

Note3: On the day the program is due, turn in a colored hard copy of the source code in this order:

1. makefile
2. main .h/.c
3. td.h / td.c
4. any new .h/.c source file combinations
5. all older .h/.c source file combinations


Douglas J. Ryan / ryandj@pacificu.edu