



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The E programming language is similar to C, C++, Java, Pascal, and Modula-2, but it differs syntactically and semantically, and is considerably simpler. You are to write a Java program that translates E programs to their semantically equivalent C programs (not C++). That is, the input to your program—henceforth, the translator is an E program; the output from your program is a C program—henceforth, the generated code, or simply GC. To verify that your translator works correctly, you are to compile and execute the GC. This assignment should help you develop a better understanding of parsing and compiling, and some familiarity and practical programming experience with Java. The general approach to translation you will be using does all its work in one pass. That is, its three activities of syntactic analysis, semantic analysis, and code generation are intermixed. As your translator reads a token or a small group of tokens in the E program, it will determine if the token(s) is syntactically and semantically valid, and generate code for the token(s). The E language is designed to facilitate one-pass translation. In contrast, many real compilers perform multiple passes, e.g., one pass for each of the three activities.
The following grammar partially describes the E language. In the BNF below, nonterminals are in lowercase; terminal symbols are enclosed in single quotes to avoid potential confusion in the use of characters that are both meta-symbols in the grammar and terminals in E, e.g., “[”.
program ::= block block ::= declaration_list statement_list declaration_list ::= {declaration} statement_list ::= {statement}
declaration ::= ’@’ id { ’,’ id }
statement ::= assignment | print | do | if print ::= ’!’ expr assignment ::= ref_id ’=’ expr ref_id ::= [ ’˜’ [ number ] ] id do ::= ’<’ guarded_command ’>’ if ::= ’[’ guarded_command { ’|’ guarded_command } [ ’%’ block ] ’]’ guarded_command ::= expr ’:’ block
expr ::= term { addop term } term ::= factor { multop factor }
factor ::= ’(’ expr ’)’ | ref_id | number addop ::= ’+’ | ’-’ multop ::= ’*’ | ’/’
The following rules complete the syntactic definition of E:
E’s semantics follow C’s semantics for the most part, but there are significant differences. The impor- tant semantic points are:
˜0 x: the variable x that is declared in the current block. ˜1 x: the variable x that is declared in the immediately enclosing block (i.e., 1 level up). ˜2 x: the variable x that is declared in the enclosing block 2 levels up ...: etc. ˜x: the variable x that is declared at the global level.
˜N x is an error if N is greater than the current depth of nesting. Note that ˜x acts like C++’s scope resolution operator, i.e., ::x.
i = 1 <i-10: !i i=i+1>
is a loop that prints out the first 10 positive integers. (The value of i is 11 after the loop.)
An undeclared variable, then, is one that does not appear anywhere in the entire stack of lists. (Thus, the stack is used in an “impure” sense. It provides more than pop and push operations; it also provides search.) In a real compiler, the symbol table would be searched beginning with the newest block to locate the most recent symbol table entry with the given name; the symbol table entry would contain additional information such as the variable’s type. Given that E’s scoping rules are like C’s, then variables should generally be located in that fashion too. However, E (unlike C) provides the scoping operator ’˜’, whose use will affect how you represent levels and how you search in the symbol table. The numeric argument to ’˜’ specifies the exact scoping level in which to find the given variable; it is an error if the variable is not found within that level, even if the variable is currently in scope (i.e., appears at a different level in the symbol table). For a redeclared variable, give an appropriate error message, and then continue by ignoring the redec- laration. For an undeclared variable (including a bad ’˜’ reference), give an appropriate error message via “System.err.println” and then stop your program via “System.exit(1)”. Since the number of variables in the program is neither known in advance nor bounded, your program must use a dynamically allocated data structure for the symbol table. Break this part into subparts. First, get everything working without handling the scoping operator. Then, handle the scoping operator.
Modify your parser so that it outputs appropriate C code. For example, for the E program:
@i i = <i-10 :! i #this is a comment i=i+1>
your translator might produce something like:
main(){ int x_i; x_i = 1; while( (x_i-10) <= 0 ){ printf("%d\n",x_i); x_i = x_i+1; } }
Note that since the scanner discards whitespace and comments, the output is not as neatly formatted as the E program. In fact, the actual output from your translator will probably be less formatted than shown above; e.g., it is fine (and simpler) to output a single C token per line. Also note that the translator has prepended each E variable name with x to avoid conflicts with C reserved words. (Hint: You might want to further “munge” identifiers for implementing levels for the ’˜’ operator.) To test your translator, examine its C output (the GC). Then, compile the GC and execute the resulting program. Verify that it does indeed execute as the source E program specifies.
Add a definite iteration statement (e.g., like for in C or Pascal) to the language definition and your translator. This part has no one “correct” solution. Do, however, strive to be syntactically and semantically consistent with the other constructs in E. Give, in your README file:
To demonstrate that your implementation of the definite iteration is correct, include several well- commented E test program files and their “correct” output files. These tests should include tests that demon- strate error checking.
/usr/java/jdk1.6.0_06/bin/javac /usr/java/jdk1.6.0_06/bin/java
To do so for csh (or tcsh) users, carefully put
set path = (/usr/java/jdk1.6.0_06/bin $path)
in your .cshrc file. (You need to logout and login again to have this change take effect.) Test that you are using the correct Java by
which javac java
For users of other shells, make an analogous changes to the PATH environment variable.