| K31 Compilers | |||||||||||||||||||||
| Spring Semester 2026 | |||||||||||||||||||||
|
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
|
Course Project
For both course assignments you’ll be required to show your
incremental development effort in the form of version control history on
github. You will create a private github repository and
invite our You are expected to show multiple commits for every day you worked on the project and the commits are expected to be consistent with normal development effort, i.e., intermediate stages are likely to be executable, new commits are likely to remove/change existing code and not just add new code, etc. Don’t stress too much about forgetting to commit often on some day—the assignments will take you enough time that incremental effort will be easy to document, overall. For submitting the assignment, you need to email the assistants with the commit hash of your assignment’s submission commit. NO SUBMISSION IS COMPLETE WITHOUT EMAILING A COMMIT HASH HW 1Part 1For the first part of this homework you should implement a simple
string expression evaluator. The evaluator should accept expressions
with the The operator That is, it concatenates its second operand twice at the end of its first operand. The operator Operator The grammar is summarized in: exp -> str | exp op exp | (exp) op -> / | ** str -> char | char str char -> a-z | A-Z You need to change this grammar to support priority between the operators, to remove the left recursion for LL parsing, etc. This part of the homework is divided in two tasks:
Your parser should read its input from the standard input (e.g., via an InputStream on System.in) and write the computed values of expressions to the standard output (System.out). Parse errors should be reported on standard error (System.err). Examples:
Part 2In the second part of this homework you will implement a parser and translator for a language supporting string operations. The language supports the concatenation (+) operator over strings, function definitions and calls, conditionals (if-else i.e, every “if” must be followed by an “else”), and the following logical expressions:
All values in the language are strings. The precedence of the operator expressions is defined as: precedence(if) < precedence(concat). Your parser, based on a context-free grammar, will translate the input language into Java. You will use JavaCUP for the generation of the parser combined either with a hand-written lexer or a generated-one (e.g., using JFlex, which is encouraged). You will infer the desired syntax of the input and output languages from the examples below. The output language is a subset of Java so it can be compiled using javac and executed using Java or online Java compilers like this, if you want to test your output. There is no need to perform type checking for the argument types or a check for the number of function arguments. You can assume that the program input will always be semantically correct. Note that each file of Java source code you produce must have the same name as the public Java class in it. For your own convenience you can name the public class “Main” and the generated files “Main.java”. In order to compile a file named Main.java you need to execute the command: javac Main.java. In order to execute the produced Main.class file you need to execute: java Main. To execute the program successfully, the “Main” class of your Java program must have a method with the following signature: public static void main(String[] args), which will be the main method of your program, containing all the translated statements of the input program. Moreover, for each function declaration of the input program, the translated Java program must contain an equivalent static method of the same name. Finally, keep in mind that in the input language the function declarations must precede all statements. As with the first part of this assignment, you should accept input programs from stdin and print output Java programs to stdout. Example #1Input:
Output (Java):
Example #2Input:
Example #3Input:
|
|||||||||||||||||||