






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!







In this assignment we will build a compiler that translates Pascal-like programs into JVM bytecode for execution by the JVM.
We first need to write a lexical analyzer (scanner) in C. The code is similar to the lexan() function in lexer.c shown on page 74 in the textbook 1st edition and copied below
/**** lexer.c ****/
#include "global.h"
char lexbuf[BSIZE]; int lineno = 1; int tokenval = NONE;
int lexan() { int t; while (1) { t = getchar(); if (t== ’ ’ || t == ’\t’) ; else if (t == ’\n’) lineno = lineno + 1; else if (isdigit(t)) { ungetc(t, stdin); scanf("%d", &tokenval); return NUM; } else if (isalpha(t)) { int p, b = 0; while (isalnum(t)) { lexbuf[b] = t; t = getchar(); b = b + 1; if (b >= BSIZE) error("compiler error");
lexbuf[b] = EOS; if (t != EOF) ungetc(t, stdin); p = lookup(lexbuf); if (p == 0) { printf("calling insert\n"); p = insert(lexbuf, ID); } tokenval = p; return symtable[p].token; } else if (t == EOF) return DONE; else { tokenval = NONE; return t; } } }
/**** error.c ****/
#include "global.h"
error(m) char *m; { fprintf(stderr, "line %d: %s\n", lineno, m); exit(1); }
The lexical analyzer is a routine lexan() that is called by the parser to find tokens. The routine reads the input one character at a time and returns to the parser the token it found. The value of the attribute associated with the token is assigned to a global variable tokenval. The lexical analyzer uses the symbol- table routine lookup to determine whether an identifier lexeme has been previously seen and the routine insert to store a new lexeme into the symbol table. It also increments a global variable lineno every time it sees a newline character.
The scanner that we need to implement should recognize operators, keywords, non-negative integers, and identifiers as tokens listed in Table 1. The first column in the table lists the terminals used by the grammar (the grammar will be defined in the next section) where the terminals int8, int16, and int32 represent signed 8-, 16-, and 32-bit integers whose values are stored in the int tokenval variable. The terminal id is an identifier starting with a letter (upper or lower case) followed by letters and digits. The second column lists the operator tokens you need for your program. For example, the operator := has a token ASSIGN with code 261.
Create a global.h file with your global declarations of the tokens (see also textbook 1st edition page 73) and copied below:
/**** global.h ****/
In global.h the ASSIGN token should be defined like this:
#define ASSIGN (261)
The keywords begin, div, do, end, if, mod, output, then, and while are all to be stored in the symbol table at initialization of the table (see symbol.c and init.c page 77 of the textbook 1st edition) and copied below:
/**** symbol.c ****/
#include "global.h"
#define STRMAX 999 #define SYMMAX 100
char lexemes[STRMAX]; int lastchar = -1; struct entry symtable[SYMMAX]; int lastentry = 0;
int lookup(s) char s[]; { int p; for (p = lastentry; p > 0; p = p - 1) if (strcmp(symtable[p].lexptr, s) == 0) return p; return 0; }
int insert(s, tok) char s[]; int tok; { int len; len = strlen(s); if (lastentry + 1 >= SYMMAX) error("symbol table full"); if (lastchar + len + 1 >= STRMAX) error("lexemes array full"); lastentry = lastentry + 1; symtable[lastentry].token = tok; symtable[lastentry].lexptr = &lexemes[lastchar + 1]; lastchar = lastchar + len + 1; strcpy(symtable[lastentry].lexptr, s); return lastentry; }
/**** init.c ****/
#include "global.h"
struct entry keywords[] = { "div", DIV, "mod", MOD, 0, 0 };
init() { struct entry *p; for (p = keywords; p->token; p++) insert(p->lexptr, p->token); }
The tokenval of an identifier id is the index in the symbol table + 3. So the first identifier’s tokenval is 3, the next identifier’s tokenval is 4, and so on. The end-of-file token is DONE.
Test your scanner on standard input. To do so, write a main() function in a new file lextest.c that uses your lexan() scanner to print the <token,tokenval> pairs scanned from standard input. For example:
./lextest < mytest
with mytest a file containing:
begin n := 2; m := 1000*n+$0; end
The program should print:
Write a Makefile to build lextest. If you are not familiar with make or your experience with make is rusty, visit
http://www.gnu.org/software/make/manual/html_chapter/make_toc.html
Remember that tabs are significant in a Makefile (so copy-pasting the contents of Makefiles is problematic). An example Makefile is included in the software that you will have to download for the second part of the assignment in the next section.
To recap, for this first part of the assignment, you should implement the source code files global.h, lexer.c, init.c, error.c, symbol.c, lextest.c, and a Makefile and test your scanner. Before you proceed with the next section, make sure your scanner works fine. You don’t need to submit the lextest code for grading. The lexical analyzer will be tested and graded together with the rest of the project code after you completed the next part.
Download the Pr1.zip file from
In the generated code for expressions, command-line arguments that are provided to the compiled program are pushed on the operand stack with the following instructions
aload_ bipush nnn iaload
where nnn is the index. Local temporary variables are always available in the frame (as long as max local is large enough). A local variable’s value (r-value) is pushed on the stack with
iload nnn
where nnn is the index of the variable (note that variables 0 to 2 are already occupied). A local variable is assigned with the popped value on the stack using
istore nnn
where nnn is the index of the variable. Constant integers are pushed with bipush, sipush, and ldc, where the last operation requires storing the 32-bit integer value in the constant pool.
This is the complete grammar with semantic actions for this assignment:
stmt → begin opt stmts end | id { var := tokenval } := expr { emit2(istore, var) } | if expr { emit(iconst 0); loc := pc; emit3(if icmpeq, 0) } then stmt { backpatch(loc, pc−loc) } | while { test := pc } expr { emit(iconst 0); loc := pc; emit3(if icmpeq, 0) } do stmt { emit3(goto, test-pc); backpatch(loc, pc−loc) } | output expr { emit(istore 2) } opt stmts → stmt ; opt stmts | ε expr → term moreterms moreterms → + term { emit(iadd) } moreterms | – term { emit(isub) } moreterms | ε term → factor morefactors morefactors → * factor { emit(imul) } morefactors | div factor { emit(idiv) } morefactors | mod factor { emit(irem) } morefactors | ε factor → ( expr ) | - factor { emit(ineg) } | int8 { emit2(bipush, tokenval) } | int16 { emit3(sipush, tokenval) } | int32 { emit2(ldc, constant pool add Integer(cf, tokenval)) } | id { emit2(iload, tokenval) } | $ int8 { emit(aload 1); emit2(bipush, tokenval); emit(iaload) }
where the start symbol of the grammar is stmt.
The grammar describes Pascal-like programs. Example programs can be found in the Appendix of this document. The usual C-like (!) semantics of the constructs are applicable. Thus, assignments are expected to overwrite the value of the identifier assigned, the if-then construct evaluates a condition (zero is false, nonzero is true), and the while loop executes the statement (or block statement) as long as the condition is true (nonzero). The output statement is special. It specifies the output value the program needs to return, without actually returning to the flow of control to the caller. Thus, multiple output statements can be executed, but only the last value will be returned to the caller when the execution of the statement (block) ends.
The $ in expressions refer to program arguments. Thus, $0 is the first program argument, $1 is the second, and so on (these values are stored in the val array of the template code listed above). Identifiers refer to local temporary variables. These local variables are allocated in the frame of the method by the JVM, so the code simply refers to the appropriate frame slots starting with 3 (the first 3 slots are used by the method argument array and local variables used to convert command-line arguments to an integer array).
Write a predictive parser for the grammar (in a new source file compile.c). The semantic actions in the grammar should be part of the predictive parser. The semantic actions emit the bytecode translation of the program being compiled.
To complete the compiler, you also have to write the necessary code to set up the internal Java class file structure. Use the javaclass.h API functions developed for this assignment to do so. See calcclass for an example. Name the class ”Code”, add a static public void main method with String[] argument, generate the method’s code using the bytecode emitter, and save the Java class file. Consult the calcclass.c source for examples.
The initial part of the bytecode of the method is always the same. This code converts the command-line arguments into an integer array that is accessed with the $ references in expressions. The initial part of the code before the generated part is:
emit(aload_0); emit(arraylength); // arg.length emit2(newarray, T_INT); emit(astore_1); // val = new int[arg.length] emit(iconst_0); emit(istore_2); // i = 0 label1 = pc; // label1: emit(iload_2); emit(aload_0); emit(arraylength); label2 = pc; emit3(if_icmpge, PAD); // if i >= arg.length then goto label emit(aload_1); emit(iload_2); emit(aload_0); emit(iload_2); emit(aaload); // push arg[i] parameter for parseInt index1 = constant_pool_add_Methodref(&cf, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"); emit3(invokestatic, index1); // invoke Integer.parseInt(arg[i]) emit(iastore); // val[i] = Integer.parseInt(arg[i]) emit32(iinc, 2, 1); // i++ emit3(goto_, label1 - pc); // goto label
Example input: java Code 2 1
Example output: 1
begin n := $0; fac := 1; while n do begin fac := fac * n; n := n - 1; end; output fac; end
Compile: compile < prog
Example input: java Code 3
Example output: 6
begin pow := 1; n := $0; m := $1; while m do begin if m mod 2 then begin pow := pow * n; m := m - 1; end; if (m - 1) mod 2 then begin n := n * n; m := m div 2; end; end; output pow; end
Compile: compile < prog