



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Programming Languages; Subject: Engineering Computer Science; University: University of California - Davis; Term: Winter 2006;
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Homework 2: Programming Project (C++) Due: 26 Jan 2006, 11:59 PM
The primary goal of this assignment is to help you develop a better understanding of parsing and compiling, and to enable some familiarity and practical programming experience with C and C++. The X programming language is similar to C, C++, Pascal, and Modula-2, but it differs syntactically and seman- tically, and is considerably simpler. You are to write a C++ program that translates X programs to their semantically equivalent C programs. For instance, as shown in Figure 1, the translator transforms a program, PX , written in X, into an equivalent program, PC , written in C.
Translator
PX
PC
Figure 1: X to C translator
To verify that your translator works correctly, you are to compile and execute the generated program.
The following grammar partially describes X’s syntax. In the EBNF below, non-terminal symbols start with capital letters whereas terminals start with small letters. Certain terminal symbols are quoted to avoid confusion with the meta-symbols.
Program ::= Block Block ::= {Declaration} {Statement} Declaration ::= VariableDeclaration | ConstantDeclaration VariableDeclaration ::= var Id {‘,’ Id} ConstantDeclaration ::= const Id ‘=’ Number Statement ::= Assignment | Print | GuardedIf | GuardedDo Assignment ::= Id ‘:=’ Expression Print ::= print Expression GuardedIf ::= if { do Expression ‘->’ Block end} end GuardedDo ::= loop {do Expression ‘->’ Block end} forever Expression ::= Simple [ Relop Simple ] Simple ::= Term { Addop Term } Term ::= Factor { Multop Factor } Factor ::= ‘(’ Expression ‘)’ | Id | Number Relop ::= ‘=’ | ’<’ | ’>’ | ‘/=’ | ‘<=’ | ‘>=’ Addop ::= ‘+’ | ‘-’ Multop ::= ‘*’ | ‘/’
The following rules complete the syntactic definition of X:
X’s semantics follow for the most part C’s semantics, but there are significant differences:
if do e1 -> s1 end do e2 -> s2 end do e3 -> s3 end : do en -> sn end end
This is a conditional statement. During the execution of an statement, expressions e1, e2, ... are evaluated sequentially until an expression ei is found to be true. For this expression, its associated statement si is evaluated next. After evaluating si, the control transfers to the next statement after the GuardedIf statement.
loop do e1 -> s1 end do e2 -> s2 end do e3 -> s3 end : do en -> sn end forever
This is a loop statement. It is repeated until all ei’s are false. At each step, expressions e1, e2, ... are evaluated sequentially until an expression ei is found to be true. For this expression, its associated statement si is evaluated next. After evaluating si, the control of execution is returned to the top and the loop is executed again.
if do 1 -> var N N := i +1; end end print N end
When parsing the inner block, the most recent symbol table entry for N indicates that N is a variable, whereas the older symbol table entry for N indicates that N is a constant. Thus, N’s most recent entry is the correct one to use. For a re-declared variable, give an appropriate error message, and then continue by ignoring the re-declaration. For an undeclared variable or an attempted assignment to a constant, give an appropriate error message, and then stop your program by using C’s exit(1) statement. Since the number of variables in the program is neither known in advance nor bounded, your program must use a dynamically allocated data structure.
Modify your parser so that it outputs appropriate C code. Feel free to use any C expression to represent X expressions into corresponding C expression. Print statement can similarly be implemented using printf functions. However, there is a constraint on the usage of statements of C: you can only use assignment, if-then-else, and while constructs of C to implement X statements. For instance, for the X program
int i i := 1 if do i/= 0 -> print 99 end # this is a comment end
your translator might produce a program that looks like the following:
main() {
int x_i = 4837; x_i = 1; if (x_i != 0) { printf("%d \n", 99); } }
Note that since the scanner discards white spaces and comments, the output is not as neatly formatted as the X program. In fact, the actual output from your translator will probably be less formatted than shown above; e.g., it is fine to output a single C token per line. Also note that the translator has prepended each X variable name with x in order to avoid conflicts with C reserved words. To test your translator, examine its C output (GC). Then, compile the GC and execute the resulting program. Verify that it does indeed execute as the source X program specifies.
#include <iostream.h> main() { cout << "hi there" << g() << endl; } int g() { return 3; }
Part Percentage 1 20 2 30 3 20 4 30 If your program does not fully work, hand in a listing of the last working part along with your attempt at the next part, and indicate clearly which is which. No credit will be given if the last working part is turned in. Points will be deducted for not following instructions, such as the above.