A complete interpreter implementation for RPAL (Right-reference Pedagogic Algorithmic Language) built as part of CS3513 - Programming Languages module at the University of Moratuwa.
This project implements a full-featured RPAL interpreter that processes source code through multiple stages: lexical analysis, parsing, tree standardization, and execution via a CSE (Control-Stack-Environment) machine. The interpreter faithfully follows RPAL language specifications and produces output compatible with the reference implementation.
- Complete Lexical Analysis - Tokenizes RPAL source code following official lexical rules
- Robust Parser - Generates Abstract Syntax Trees (AST) with comprehensive error handling
- Tree Standardization - Transforms AST into Standardized Trees (ST) for execution
- CSE Machine - Executes programs using the 13 standardized CSE evaluation rules
- Multiple Output Modes - View tokens, trees, and execution states for debugging
- Reference Compatibility - Output matches the official
rpal.exeimplementation
Before running the interpreter, ensure you have:
- Python 3.7+ installed on your system
- pip package manager
- Git (for cloning the repository)
-
Clone the repository
git clone <repository-url> cd rpal-interpreter
-
Install dependencies
pip install -r requirements.txt
# Run the default test using Makefile
make
# Or run manually
cd src
python myrpal.py ../testing_rpal_sources/test1.rpalThe project includes a comprehensive Makefile for streamlined development and testing:
# Run default test (test1.rpal)
make
# or
make default
# or
make run_test1make ast_test1 # Generate and display AST
make tokens_test1 # Generate and display all tokens
make ftokens_test1 # Generate and display filtered tokens
make st_test1 # Generate and display Standardized Tree
make cse_test1 # Generate and display CSE Table
make list_src_test1 # List the source code of test1.rpal# Run any RPAL file
make run_file FILE=filename.rpal
# Generate AST for any file
make ast_file FILE=filename.rpal
# Generate tokens for any file
make tokens_file FILE=filename.rpalmake clean # Remove temporary files
make help # Display help message# Test different programs
make run_file FILE=test2.rpal
make run_file FILE=fibonacci.rpal
# Analyze program structure
make ast_file FILE=factorial.rpal
make st_file FILE=complex_program.rpal
# Debug tokenization
make tokens_file FILE=my_program.rpal
# Clean up after testing
make cleanThe interpreter supports various command-line switches for different output modes:
python myrpal.py [OPTION] <rpal-file>| Option | Description | Output |
|---|---|---|
| (none) | Execute program | Program execution results |
-ast |
Show AST | Abstract Syntax Tree visualization |
-st |
Show ST | Standardized Tree structure |
-ct |
Show CSE Table | CSE machine control structures |
-t |
Show Tokens | Raw lexical tokens |
-ft |
Show Filtered Tokens | Processed tokens after screening |
# Execute a program
python myrpal.py ../testing_rpal_sources/fibonacci.rpal
# View the Abstract Syntax Tree
python myrpal.py -ast ../testing_rpal_sources/factorial.rpal
# Debug with token analysis
python myrpal.py -t ../testing_rpal_sources/simple.rpalThe interpreter follows a modular design with four main processing stages:
RPAL Source β Lexer β Parser β Standardizer β CSE Machine β Output
- Converts source code into tokens using finite state automata
- Handles all RPAL lexical elements (identifiers, operators, literals, etc.)
- Implements comprehensive error detection for invalid characters
- Filters token stream to remove whitespace and comments
- Prepares clean token sequence for parsing
- Maintains position information for error reporting
- Constructs Abstract Syntax Tree from token stream
- Implements RPAL grammar rules with recursive descent parsing
- Transforms AST into Standardized Tree for execution
- Executes standardized trees using stack-based evaluation
- Implements all 13 CSE machine rules
- Manages environments for variable scoping and function calls
rpal-interpreter/
βββ π docs/ # Documentation files
βββ π src/ # Source code
β βββ π cse_machine/ # CSE Machine implementation
β β βββ π binop.py # Binary operations
β β βββ π control_structure.py # Control structures
β β βββ π environment.py # Environment management
β β βββ π error_handler.py # Error handling
β β βββ π machine.py # Main CSE machine
β β βββ π stack.py # Stack operations
β β βββ π stlinearizer.py # ST linearization
β β βββ π unop.py # Unary operations
β β βββ π utils.py # Utility functions
β βββ π interpreter/
β β βββ π interpreter.py # Main interpreter logic
β βββ π lexical_analyzer/
β β βββ π scanner.py # Lexical analysis
β βββ π parser/
β β βββ π parser.py # Syntax analysis
β βββ π screener/
β β βββ π screener.py # Token filtering
β βββ π standardized_tree/
β β βββ π build_standardized_tree.py # Tree standardization
β βββ π table_routines/ # FSA tables and utilities
β β βββ π accept_states.py # Acceptance states
β β βββ π char_map.py # Character mapping
β β βββ π fsa_table.py # FSA transition table
β β βββ π keywords.py # RPAL keywords
β βββ π utils/ # Utility modules
β β βββ π control_structure_entities.py
β β βββ π file_handler.py # File I/O operations
β β βββ π node.py # Tree node definitions
β β βββ π stack.py # Stack data structure
β β βββ π token_printer.py # Token display utilities
β β βββ π tokens.py # Token definitions
β β βββ π tree_list.py # Tree list operations
β β βββ π tree_printer.py # Tree visualization
β βββ π myrpal.py # Main entry point
βββ π testing_rpal_sources/ # Test RPAL programs
β βββ π test1.rpal
β βββ π test2.rpal
β βββ π test3.rpal
β βββ π test4.rpal
β βββ π test5.rpal
β βββ π test6.rpal
β βββ π test7.rpal
β βββ π test8.rpal
β βββ π test9.rpal
β βββ π test10.txt
βββ π .gitignore # Git ignore rules
βββ π Makefile # Build automation
βββ π README.md # This file
βββ π requirements.txt # Python dependencies
The project includes comprehensive test cases in the testing_rpal_sources/ directory:
# Run basic tests
python myrpal.py ../testing_rpal_sources/test1.rpal
# Test complex programs
python myrpal.py ../testing_rpal_sources/test2.rpal
# Validate against reference implementation
python myrpal.py ../testing_rpal_sources/test3.rpal > output.txtThe test suite includes programs that verify:
- Basic arithmetic operations
- Function definitions and calls
- Recursive functions
- Conditional expressions
- Tuple operations
- String manipulations
- Complex nested expressions
- Modular Design: Each component is self-contained with clear interfaces
- Error Handling: Comprehensive error detection and reporting throughout
- Documentation: Well-documented code with clear function signatures
- Testing: Extensive test coverage with sample RPAL programs
- Lexical Analysis: Finite State Automaton for tokenization
- Parsing: Recursive Descent Parser with predictive parsing
- Tree Building: AST construction with proper node relationships
- Standardization: Rule-based AST to ST transformation
- Execution: Stack-based evaluation with environment management
The interpreter supports the complete RPAL language specification including:
- Data Types: Integers, strings, booleans, tuples, functions
- Operators: Arithmetic, logical, comparison, and string operations
- Control Flow: Conditional expressions, recursion
- Functions: Lambda expressions, function application, currying
- Pattern Matching: Tuple destructuring and parameter binding
- Built-in Functions: Print, arithmetic operations, string operations
// Factorial function
let rec factorial n =
n eq 0 -> 1 | n * factorial (n-1)
in factorial 5
// Fibonacci sequence
let rec fib n =
n le 1 -> n | fib(n-1) + fib(n-2)
in fib 10
// List operations
let list = (1, 2, 3, 4, 5) in
let head x = x 1 in
let tail x = x 2 in
print head list
When encountering issues:
- Check syntax: Use
-astto view the parsed tree structure - Examine tokens: Use
-tor-ftto inspect tokenization - Trace execution: Use
-ctto see CSE machine states - Validate input: Ensure proper RPAL syntax and semantics
This project was developed as part of academic coursework. If you'd like to contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
- Follow existing code style and conventions
- Add tests for new features
- Update documentation as needed
- Ensure compatibility with reference implementation
This project is licensed under the MIT License - see the LICENSE file for details.
Course: CS3513 - Programming Languages
Institution: Department of Computer Science & Engineering, University of Moratuwa
Semester: 4th Semester, Batch 22
This implementation demonstrates practical application of compiler design principles including lexical analysis, syntax analysis, semantic analysis, and code execution.
The interpreter is optimized for:
- Memory efficiency: Proper memory management in CSE machine
- Execution speed: Optimized tree traversal and evaluation
- Error handling: Fast error detection and meaningful error messages
- Import Errors: Ensure all dependencies are installed via
pip install -r requirements.txt - File Not Found: Check that RPAL source files exist and paths are correct
- Syntax Errors: Verify RPAL program syntax matches language specification
- Execution Errors: Use debug flags to trace program execution
The interpreter provides detailed error messages including:
- Lexical errors: Invalid characters or malformed tokens
- Syntax errors: Grammar violations with line numbers
- Runtime errors: Type mismatches and undefined variables
- Semantic errors: Invalid operations and scope violations
For questions or issues:
- π§ Create an issue in the repository
- π Refer to the RPAL language documentation in the
docs/folder - π Check existing test cases for usage examples
- π¬ Review the source code comments for implementation details
- Dr. [Professor Name] - Course instructor and guidance
- University of Moratuwa - Academic institution
- RPAL Language Specification - Reference documentation
- CS3513 Course Materials - Theoretical foundation
Built with β€οΈ for the Programming Languages community