Skip to content

AlexTuring010/compilers-hw1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 

Repository files navigation

compilers-hw1

Two-part homework for the Compilers course at the University of Athens (Department of Informatics & Telecommunications).

  • Part 1 — Hand-write an LL(1) recursive-descent parser for an expression grammar, derived from the grammar's FIRST/FOLLOW sets and a pre-built lookup table.
  • Part 2 — Build a complete scanner + parser + code generator for a small string-manipulation DSL using JFlex and JavaCUP: source → IR → Java. The generated Java is compiled and executed automatically as part of the pipeline.

Solo project.

Part 1 — LL(1) parser by hand

Source DSL: arithmetic expressions over numbers (the original grammar lives in Part1/LL1_grammars/expressionGrammar; the LL(1)-rewritten version that removes left recursion and factors the grammar is in expressionGrammarLL1).

The Java code:

File Purpose
Part1/evaluator/Main.java Reads an expression from stdin, hands it to the evaluator
Part1/evaluator/ExpressionEvaluator.java Recursive-descent parser using the lookup table; evaluates as it parses
Part1/evaluator/ParseError.java Custom exception for syntax errors
Part1/LL1_grammars/FirstFollowSets Computed FIRST and FOLLOW sets for the grammar
Part1/LL1_grammars/LookupTable The LL(1) parsing table — what production to apply for each (non-terminal, terminal) pair
cd Part1
make
make execute            # interactive: type an expression, get the value

Part 2 — Translator: string DSL → IR → Java

The source language is a small DSL for string manipulation. It supports:

  • Function definitions (name() { "John" }, fullname(first, sep, last) { first + sep + last })
  • String literals and concatenation ("a" + "b")
  • Built-in operations: prefix, suffix, reverse
  • Equality testing (=) and if/else

A sample input (from inputs/input1.txt):

name()  { "John" }
surname() { "Doe" }
fullname(first_name, sep, last_name) {
  first_name + sep + last_name
}

name()
surname()
fullname(name(), " ", surname())

The pipeline:

input.txt
   │
   ▼
 JFlex scanner (scanner.flex)  ──tokens──▶  JavaCUP parser (IRparser.cup)
                                                       │
                                                       ▼
                                                Translated.ir
                                                       │
                                                       ▼
                                          JavaCUP parser (JavaParser.cup)
                                                       │
                                                       ▼
                                                Translated.java
                                                       │
                                                       ▼
                                                   javac + java
                                                       │
                                                       ▼
                                                  program output

Two CUP grammars: one that produces IR from the source DSL, one that translates IR into Java source. Output files land in Part2/outputs/. The Makefile chains all of it — generate, compile, run.

cd Part2
make
make execute < inputs/input1.txt        # drives the full pipeline on one input
./tests.sh                              # runs all 21 example.txt + 6 error.txt tests

Sequence

Part of a two-piece Compilers arc:

  1. compilers-hw1 (you are here) — LL(1) parser construction + JFlex/JavaCUP translator
  2. compilers-hw2 — MiniJava semantic analysis (JavaCC/JTB, two-visitor design)

License

MIT — applies to my own code in this repo. Vendored libraries (JavaCUP runtime jars under Part2/libs/) and assignment-distributed materials retain their original licenses.

About

Compilers HW1 (UoA DI): Part 1 — hand-written LL(1) recursive-descent parser from FIRST/FOLLOW + lookup table. Part 2 — full JFlex/JavaCUP translator pipeline for a string-DSL → IR → Java. Solo project.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors