⚠️ This is an archived project from Fall 2022, preserved for reference. No active development is planned.
This project was developed as part of the Languages & Compilers course during Fall 2022.
Team Members:
- Mouad Kabiri
- Amine Nakari
- El Mehdi Ziate
Figure 1: The complete compiler pipeline for the Flower language, showing:
- Input: Flower program file
- Lexer: Produces tokens
- Assembler (Parser): Creates parse tree
- Generator: Produces ML code
- VM: Executes the code with data input
- Output: Program results*
This project involves designing and implementing a simple task-oriented programming language called Flower. The language is used to write programs that direct a Picker agent to navigate through a two-dimensional World grid, picking flowers to gather in a bouquet.
The project is divided into multiple parts:
- Virtual Machine Interpreter
- Language Design
- Lexical Analyzer
- Parser & Static Semantics Analyzer
- Code Generator
Implement a virtual machine (VM) that executes a simple machine language (VML) similar to the one studied in Module 3.
- Memory: 1000-word memory for both data and code
- Word Format: Signed 10-digit words
- Instruction Format: 1-digit signed opcode and 3-digit operands
-
Memory Arrays
- Data memory array (1000 words)
- Code memory array (1000 words)
- Support for signed 10-digit integers
-
Symbol Table
- Maps "numeric symbolic" names to data memory locations
- Built during program loading
- Used for resolving data references in instructions
-
Label Table
- Maps "numeric symbolic" labels to code memory locations
- Built during program loading
- Used for resolving jump targets
-
Program Loader
- Reads program sections:
- Data section
- Code section
- Input data section
- Handles data initialization
- Resolves symbolic references
- Reads program sections:
-
Instruction Cycle Implementation
- Fetch instruction
- Decode opcode and operands
- Execute instruction
- Update instruction pointer
Opcode | Arguments | Description |
---|---|---|
+0 | src 000 dst | Assignment |
+1 | nn1 nn2 dst | Addition |
-1 | nn1 nn2 dst | Subtraction |
+2 | nn1 nn2 dst | Multiplication |
-2 | nn1 nn2 dst | Division (integer) |
+4 | nn1 nn2 dst | Equal test |
-4 | nn1 nn2 dst | Not equal test |
+5 | nn1 nn2 dst | Greater than or equal test |
-5 | nn1 nn2 dst | Less than test |
+6 | arr idx dst | Read from array |
-6 | src arr idx | Write to array |
+7 | inc bnd dst | Loop |
-7 | 000 000 lnm | Label |
+8 | 000 000 dst | Read input |
-8 | src 000 000 | Write output |
+9 | 000 000 000 | Stop |
Design the Flower programming language syntax and semantics.
- Reserved Words
- Punctuation
- Operators
- Built-in functions and constants
- User-defined identifiers
- Constants
- String literals (if needed)
- Program structure
- Declarations/Definitions
- Statement types
- Function prototypes and definitions
- World initialization syntax
- Simple types: integers, boolean, enumeration
- Arrays for world representation
- Global and local variables
- Functions with parameters (pass by value only)
- Single file programs
- No external libraries
- Built-in I/O functions
Figure 2: Example of a Flower World grid showing:
- Grid dimensions: width × depth
- Picker (blue circle) at coordinates (1,8)
- Entrance (IN) at bottom left (0,0)
- Exit (OUT) at top right
- Flowerbeds (black squares)
- Flowers (pink squares)
- Grass (green squares)
- Empty cells (white squares)*
The Picker must navigate from IN to OUT while collecting flowers. Flowers can only be picked when the Picker is adjacent to the flowerbed edge.
- Rectangular/square grid world
- Picker starts at lower-left corner
- Flowers planted in flowerbeds
- Flowerbed specification format: lower-left;upper-left;upper-right;lower-right
- Picking rules:
- Picker must be adjacent to flowerbed
- Can only pick flowers next to flowerbed edge
- Direct navigation programs
- Systematic exploration programs
- Reachability analysis programs
- Lexeme specification table
- BNF/EBNF grammar
- Example programs
- World initialization format
Convert source code text into a token stream for the parser.
- JLex (Java)
- lex/flex
- Other generator tools
- Python with re module
- Other languages with regex support
- Punctuation
- Operators
- Reserved words
- User-defined identifiers
- Numeric literals
- String literals
- Comments
- Whitespace
[Line Number] [Token ID] [Token Type] [Lexeme]
- Symbol Table (for identifiers)
- Literal Table (for constants)
- Verify syntactic correctness of token stream
- Generate Concrete Syntax Tree (CST)
- Perform basic semantic analysis
- Generate Abstract Syntax Tree (AST)
- Recursive Descent LL(1) parser
- Grammar requirements:
- No left recursion
- Unambiguous
- Pairwise disjoint RHS first sets
- Identifier declaration before use
- Scope tracking (global vs local)
- Function parameter matching
- Optional: Basic type checking
- Token stream processor
- Syntax error detection
- CST constructor
- Tree visualization
- Symbol table manager
- Scope checker
- Parameter validator
- AST constructor
E --- E --- T --- T --- F --- id
| --- *
| --- F --- id
| --- +
| --- T --- F --- id
+ --- * --- id
| --- id
| --- id
This compiler implementation for the Flower language, while functional for basic cases, has several areas that could benefit from improvement. Here are the key areas that could be enhanced, in order of importance:
- Error Handling: The lexer and parser currently stop at the first error encountered. They could be enhanced to continue parsing and collect multiple errors.
- Array Operations: The code generator handles only basic array operations. Support for more complex array manipulations and optimizations could be added.
- Symbol Table Management: Currently uses simple dictionaries for scope management. Could be improved with a proper symbol table implementation supporting nested scopes.
- The implementation prioritizes functionality over elegance. Code refactoring and cleanup would improve maintainability.
- While functional, the codebase would benefit from more comprehensive documentation and test coverage.
- The concrete syntax tree doesn't maintain all terminal nodes including punctuation, making it theoretically incomplete though functionally adequate.
All contributions are welcome! This is an educational project, and while not all features of a production compiler are implemented, it serves as a good learning tool for compiler construction concepts.
Note: Each part builds upon the previous parts. Ensure all components work together correctly before proceeding to the next phase.