This project includes an educational chatbot interface that visualizes the compilation pipeline (Lexer, Parser, Interpreter).
-
Install dependencies:
pip install -r requirements.txt
-
Run the Streamlit app:
python -m streamlit run streamlit_app.py
-
Open the link displayed in the terminal (usually
http://localhost:8501).
SpeakMath is a math-focused natural language mini-programming language that interprets expressions like "find the mean of these values" into verified computations. The LLM suggests operator meanings, while our grammar verifies expressions before evaluation.
University of Malaya | Faculty of Computer Science & Information Technology
WIF3010: Programming Language Concepts | Project Brief 2025
- About the Project
- System Architecture
- Grammar Design (Week 8)
- Team Roles
- Current Progress
- Next Steps
Project Title: SpeakMath (Topic #3 from WIF3010 Brief)
Core Concept:
Create a math-focused natural mini-language where:
- Users write commands like "find the mean of these values"
- LLM suggests operator meanings (e.g., "average" β
mean) - Our grammar verifies expressions before evaluation
- Execution is handled by our own interpreter
Paradigm Extension: Functional Programming (map/reduce/composition)
- Makes mathematical operations accessible through natural language
- Combines formal grammar verification with LLM flexibility
- Perfect for demonstrating functional programming concepts
- Clear scope for proof of correctness
graph TB
A[User Input] --> B[Lexer]
B --> C[Parser]
C --> D{Token Known?}
D -- "MAP/REDUCE<br/>(Grammar-First)" --> E[Parse Map/Reduce]
D -- "Known Op<br/>(SUM/MEAN/etc)" --> F[Semantic Map]
D -- "Unknown Verb" --> G[Build Phrase]
E --> H[AST Builder]
F --> H
G --> I{LLM Resolver}
I -- "SEMANTIC_MAP" --> J[Return Operator]
I -- "SYNONYM_MAP" --> J
I -- "Heuristics" --> J
I -- "LLM API" --> K{Found?}
K -- "Yes + Metadata" --> J
K -- "UNKNOWN" --> L[Structured Error]
J --> H
L --> M[Error Output]
H --> N[Interpreter]
N --> O{Is LLM Resolved?}
O -- "Yes" --> P[Log Metadata]
O -- "No" --> Q[Direct Execute]
P --> Q
Q --> R[Output]
style E fill:#90EE90
style F fill:#90EE90
style I fill:#FFD700
style L fill:#FF6B6B
style P fill:#87CEEB
Legend:
- π’ Green: Grammar-First (No LLM)
- π‘ Yellow: LLM Fallback Layer
- π΄ Red: Error Handling
- π΅ Blue: Metadata Logging
Flow Summary:
- Lexer: Tokenizes input (MAP/REDUCE = keywords)
- Parser: Matches grammar or builds phrase for unknown verbs
- LLM Resolver: 4-layer strategy (MapβSynonymβHeuristicβAPI)
- AST Builder: Creates nodes with LLM metadata
- Interpreter: Executes and logs LLM resolutions
See: Complete Architecture Diagram
- Map unknown verb phrases to canonical operators
- Provide reasoning for operator choices
- Return "UNKNOWN" for unsupported operations
- Resolve operation names within map/reduce
- Override grammar rules
- Change token types or syntax structure
- Generate AST nodes
- Handle MAP/REDUCE keywords (always grammar-first)
- Execute code
See: LLM Fallback Design
The formal BNF/EBNF syntax definition is available in docs/syntax_definition.md.
Implemented Extensions:
- β
Variable assignment:
set x to 5 - β
Conditionals:
if x > 10 then print x - β
Functional ops:
map add 2 over [1, 2, 3]
NEW: SpeakMath now includes a sophisticated LLM fallback system that supplements the grammar without overriding it.
Key Features:
- π― Grammar-First: MAP/REDUCE and known operations always use grammar
- π€ LLM Supplement: Unknown verbs resolved via AI (e.g., "tally up" β sum)
- π Structured Errors: Detailed failure objects with suggestions
- π Metadata Tracking: All LLM resolutions logged with reasoning
Documentation:
- π LLM Fallback Design - Complete design document
- π Architecture Diagram - System flow diagrams
- β‘ Quick Reference - Fast lookup guide
- π Implementation Summary - Task completion summary
Example:
# Grammar-First (No LLM):
Input: "sum [1, 2, 3]"
Output: 6
# LLM Fallback (AI Resolves Unknown Verb):
Input: "tally up [1, 2, 3]"
LLM: "tally up" β OP_SUM (Reasoning: "tally implies summation")
Output: 6 (with log: "βΉοΈ LLM Resolution: 'tally up' β SUM")
# Grammar-First Guarantee (MAP always parsed by grammar):
Input: "map add 2 over [1, 2, 3]"
Grammar: Handles structure, LLM only resolves "add" if needed
Output: [3, 4, 5]sum 1, 2, 3, 4, 5
mean 10, 20, 30, 40
multiply 5 and 6
tally up [10, 20, 30] # LLM-resolved
map increment 2 over [1,2,3] # Grammar-first structure
reduce product on [2,3,4] # Grammar-first
Command: "sum 1, 2, 3"
<command>
|
____|____
| |
<operation> <expression>
| |
"sum" 1, 2, 3
| Role | Name | Responsibilities |
|---|---|---|
| Language Architect | [Insert Name] | Design BNF/EBNF grammar, parse trees |
| Programmer/Integrator | [Insert Name] | Build lexer/parser, system integration |
| Semantics & Proof Specialist | [Insert Name] | Semantic mapping, correctness proofs |
| LLM Integration Engineer | [Insert Name] | LLM API integration, synonym resolution |
| Runtime & Execution Engineer | [Insert Name] | Execution engine, functional paradigm |
- Team formation
- Project proposal submitted
- Group contract signed
- Selected "SpeakMath" as project title
- Finalize BNF/EBNF grammar
- Create sample parse trees
- Design semantic mapping table
- Implement LLM fallback system
- Create comprehensive architecture diagrams
- Deliverable 2: Presentation 1 (Grammar + Architecture)
- Implement structured failure objects
- Update AST with LLM-resolved nodes
- Create grammar-first enforcement tests
- Document LLM intervention rules
- Build 4-layer resolution strategy
- Complete formal grammar definition
- Design 3-5 parse tree examples
- Create high-level architecture diagram
- Prepare Presentation 1
- Start implementing lexer
- Build basic parser
- Test with simple commands
- Integrate LLM API
- Implement functional features (map/reduce)
- Prepare Presentation 2
| Criteria | Marks | Current Status |
|---|---|---|
| Proposal & Grammar Design | 15 | π‘ In Progress |
| Parser & Interpreter | 20 | β³ Pending |
| LLM Integration | 15 | β³ Pending |
| Paradigm Extension (Functional) | 10 | β³ Pending |
| Proof of Correctness | 10 | β³ Pending |
| Testing & Evaluation | 10 | β³ Pending |
| Presentations | 10 | π‘ Preparing |
| Final Report | 10 | β³ Pending |
Required for Week 8:
- BNF/EBNF grammar specification
- 3-5 example parse trees
- High-level system architecture
- Semantic mapping plan
- LLM integration strategy
Tools We'll Use:
- Python 3.8+ (interpreter)
- OpenAI API or Gemini (LLM layer)
- PLY or recursive-descent parser
- GitHub (collaboration)
University of Malaya | FCSIT
Last Updated: Week 10, 2025