Thanks to visit codestin.com
Credit goes to github.com

Skip to content

nijam001/SpeakMath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SpeakMath: Natural Expressions into Verified Computations

Project Status Course Paradigm

Running the Web Interface (Chatbot)

This project includes an educational chatbot interface that visualizes the compilation pipeline (Lexer, Parser, Interpreter).

  1. Install dependencies:

    pip install -r requirements.txt
  2. Run the Streamlit app:

    python -m streamlit run streamlit_app.py
  3. Open the link displayed in the terminal (usually http://localhost:8501).

Running the CLI Demo

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


πŸ“– Table of Contents


πŸ€– About the Project

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)

Why SpeakMath?

  • 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

πŸ— System Architecture

Updated Architecture with LLM Fallback

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
Loading

Legend:

  • 🟒 Green: Grammar-First (No LLM)
  • 🟑 Yellow: LLM Fallback Layer
  • πŸ”΄ Red: Error Handling
  • πŸ”΅ Blue: Metadata Logging

Flow Summary:

  1. Lexer: Tokenizes input (MAP/REDUCE = keywords)
  2. Parser: Matches grammar or builds phrase for unknown verbs
  3. LLM Resolver: 4-layer strategy (Map→Synonym→Heuristic→API)
  4. AST Builder: Creates nodes with LLM metadata
  5. Interpreter: Executes and logs LLM resolutions

See: Complete Architecture Diagram


🎯 LLM Integration Rules

βœ… LLM CAN:

  • Map unknown verb phrases to canonical operators
  • Provide reasoning for operator choices
  • Return "UNKNOWN" for unsupported operations
  • Resolve operation names within map/reduce

❌ LLM CANNOT:

  • 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


πŸ“œ Grammar Design (Week 8)

Syntax Definition

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]

LLM Fallback System

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:

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]

Example Commands

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

Sample Parse Tree (Week 8 Deliverable)

Command: "sum 1, 2, 3"

    <command>
        |
    ____|____
   |         |
<operation> <expression>
   |            |
  "sum"    1, 2, 3

πŸ‘₯ Team Roles

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

πŸ“… Current Progress

Week 5 βœ…

  • Team formation
  • Project proposal submitted
  • Group contract signed
  • Selected "SpeakMath" as project title

Week 8 οΏ½ (Completed)

  • 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)

Week 8+ (NEW: LLM Integration)

  • Implement structured failure objects
  • Update AST with LLM-resolved nodes
  • Create grammar-first enforcement tests
  • Document LLM intervention rules
  • Build 4-layer resolution strategy

🎯 Next Steps

Immediate (Week 8)

  1. Complete formal grammar definition
  2. Design 3-5 parse tree examples
  3. Create high-level architecture diagram
  4. Prepare Presentation 1

Week 8 (After Presentation)

  1. Start implementing lexer
  2. Build basic parser
  3. Test with simple commands

Week 10

  1. Integrate LLM API
  2. Implement functional features (map/reduce)
  3. Prepare Presentation 2

πŸ“Š Assessment Focus

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

πŸ“š Quick References

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages