Compiler implementation for the Functional Language.
Semester 6, 3rd study-year, Innopolis University.
This is a project assignment about creating a compiler for the toy functional language (similar to Lisp).
Haskell team B19-SD-01 members
- Roman Soldatov
- Daniil Livitin
- Timur Nugaev
- Mikhail Martovitsky
The program can run with provided command-line arguments if you execute it from a terminal. You can specify the path to the source program and the compiler compiles it. Alternatively, in the Main.java file, you can simply specify the path in defaultPath
variable without providing any argument in a command line: String defaultPath = <path to the source program>
.
In the lexical_analysis folder, you can find a Lexer that tokenizes an input from the source program file. The lexer was automatically created with the use of a JFlex scanner. Tokens are defined in a lexer.flex file. Their class representation is stored in the tokens folder.
Run jflex lexer.flex
command in your terminal to generate a Lexer.java java file. We are using lexer.tokenize()
and lexer.getTokens
to parse and get a list of all found tokens.
In the syntax_analysis folder, you can fina a Parser which creates AST via Parser.makeAST(<sourceProgramPath>)
method. This parser was generated by Bison. The grammar is described in Parser.y file. AST contains nodes that represent particular syntax subtrees. You can find them in the node folder.
We use LexerAdapter.java class which connects our Lexer.java and implements necessary methods from Parser.Lexer
interface.
Run bison Parser.y -L java
to create Parser.java parser.
In the interpreter package there are classes related to AST interpreteation.
- AtomsTable.java saves defined atoms and their values. This class is represented as a singleton. Atoms are stored in some local context. Atoms with similar name shadows atoms from outer scope. Each local context is perpesented as a HashMap. The hirerachy of local contexts is presented as a Stack. When you enter into some function or loop the interpreter calls
introduceLocalContext()
function to create a new temporary local context which will be deleted after leaving the form (leaveLocalContext()
will be called). - FunctionsTable.java is similar to AtomsTable.java, but it stores defined user-functions.
- PredefinedFunction.java and DefinedFunction.java classes are utility functions to interpret a list as a function call and evaluate it.