FM Compiler Project  

Introduction

The ultimate goal of this project is understanding the structure of a compiler and building a compiler by implementing its fundamental components step by step. A compiler should be able to read and analyze entire program in a certain programming language, and then translate the source program to semantically equivalent program in a target language. To accomplish its roll, the compiler will have three major components - I/O, scanner, and parser component - in this project. Eventually, the compiler will take and compile source files in a simple language called FM (for flip movie) and will output Postscript files that describe the pages of a flip movie. However, a compiler for any other languages can be designed mostly in the same manner.

CompilerIO

A class CompilerIO will handle input and output for the compiler. The idea behind this class is that the compiler will use a single CompilerIO object to manage input and output, which will shield the rest of the compiler from the details of file handling. When the compiler starts, it will create an instance of the CompilerIO class. In addition to reading and writing files, the CompilerIO class will manage the temporary buffer to which the compiler emits generated code so that one can later use it several times as needed.

Scanner

A class Scanner (lexical analyzer) reads the characters representation of a program and transforms it into a stream of tokens representing the basic lexical items in the specified language. The scanner skips over whitespaces and comments in the source code; these are ignored by the compiler and do not appear in the token stream that will eventually be read by the parser. The scanner demonstrates the use of a simple DFA (Deterministic Finite Automata) to recognize regular expressions. (see also the keywords of FM language)

Parser

A class Parser reads the tokenized representation of a program (generated by the scanner) and validates it against the grammar for the language generated by the associated grammar. Thus, the parser takes advantage of CFG (Context-Free Grammar). The parser also creates symbol table entries for defined variables in the language, and generates actual codes in a target language as the parser discovers any specific structure of the program. Although the parser handles basic syntax errors and exceptions, it does not do extensive error processing or recovery as the industrial-power-compiler usually do. In this project, we adopts a Recursive-Descent Parser which is considered to be easier to implement, but sufficient for a simple language. Taken advantage of top-down parsing, we shall use LL(1), leftmost derivation with 1-pass lookahead, while we still need the power of ad-hoc in some cases. (see also the definition of FM language grammar)


Note: You're free to improve the program, or use it as a framework to build a compiler for another language. Just let me know if you happen to kick off an awesome compiler anyhow. I'd like to see it!

Full Packaged File

See JavaDocs for Source Files

Source Files List

  • CompilerIO.java
  • Scanner.java 
  • Symbol.java
  • SymbolTable.java
  • Token.java
  • Parser.java 
  • SyntaxException.java
  • Flip.java (main class) - check this file if you want to change the file-path (default as '/fm') for input and output files.
  • Other Files Required

  • FlipPrologEnd.ps - must be in the same directory as all other source files to compile them
  • FlipPrologStart.ps - must be in the same directory as all other source files to compile them
  • FlipTypes.ps (library file for a flip movie) - must be in the same directory (default as /fm) as Postscript output files in order to view them
  • Other Optional Files

  • FM Language Specification (Grammar)  .PDF
  • ScanTest.java
  • ParserTest.java
  • runFlipTests.bat - executes the compiler
  • 10 sample source codes (flip movie) in FM language - save them in the sub-directory '/fm'
  • 9 sample Postscript output files w/o source codes in FM  (Check this out even if you're not a computer man! It's awesome!)
  • To View Postscript files (Flip movies)

  • Install Ghostscript 8.14 first and then GSview 4.6.
  • Open .ps files and switch to the full screen and use space bar to view as a movie.
  • Sample Still Images of Postscript files



    by Hyung-Joon Kim, CSE 413, University of Washington