BodmasParser API Documentation

Welcome to the BodmasParser API documentation. This API allows you to parse and evaluate mathematical expressions, validate expressions, and perform health checks.

Base URL

All API endpoints are relative to: https://bodmasparser.onrender.com

For interactive Swagger documentation, visit: https://bodmasparser.onrender.com/docs

Endpoints

POST /parse

Parse and evaluate a mathematical expression. This endpoint takes a mathematical expression, validates it, converts it to postfix notation, builds a parse tree, and evaluates the result.

Request Body

{ "expression": "string" // A valid mathematical expression (required) }

Parameters

Name Type Required Description
expression string Yes A mathematical expression to parse and evaluate (e.g., "3+4*5")

Response

Status Description
200 OK Expression successfully parsed and evaluated
400 Bad Request Invalid expression or parsing error

Response Body

{ "postfix": ["string"], // The expression in postfix notation as an array of tokens "parse_tree": {}, // A nested object representing the parse tree "result": number, // The numerical result of evaluating the expression "input_expression": "string", // The original input expression "valid": boolean, // Whether the expression is valid "error": "string" | null // Error message if the expression is invalid, null otherwise }

Example

Request:

POST /parse Content-Type: application/json { "expression": "3+4*5" }

Response:

{ "postfix": ["3", "4", "5", "*", "+"], "parse_tree": { "operator": "+", "left": "3", "right": { "operator": "*", "left": "4", "right": "5" } }, "result": 23.0, "input_expression": "3+4*5", "valid": true, "error": null }

GET /validate/{expression}

Validate a mathematical expression without parsing or evaluating it.

Parameters

Name Type Required Description
expression string (path parameter) Yes A mathematical expression to validate (e.g., "3+4*5")

Response

Status Description
200 OK Validation successful
400 Bad Request Validation error

Response Body

{ "valid": boolean // Whether the expression is valid }

Example

Request:

GET /validate/3%2B4*5 // URL-encoded as 3+4*5

Response:

{ "valid": true }

GET /ping

Simple health check endpoint to verify that the API is operational.

Response

Status Description
200 OK API is operational

Response Body

{ "status": "ok", "message": "API is operational" }

GET /

Root endpoint that provides basic information about the API.

Response

Status Description
200 OK API information retrieved successfully

Response Body

{ "message": "Welcome to the BodmasParser API", "status": "operational", "documentation": "/docs", "endpoints": [ { "path": "/parse", "method": "POST", "description": "Parse and evaluate a mathematical expression" }, { "path": "/validate/{expression}", "method": "GET", "description": "Validate a mathematical expression" }, { "path": "/ping", "method": "GET", "description": "Simple health check endpoint" } ] }

Error Handling

The API returns appropriate HTTP status codes and detailed error messages for different types of errors:

Error Type HTTP Status Response Format
Invalid Expression 200 OK (with valid=false)
{ "postfix": [], "parse_tree": {}, "result": 0.0, "input_expression": "invalid-expression", "valid": false, "error": "Specific error message" }
Invalid Request 400 Bad Request
{ "detail": "Error message" }

Common Error Messages

Usage Examples

Python Example

import requests # Parse and evaluate an expression response = requests.post( "https://bodmasparser.onrender.com/parse", json={"expression": "(3+4)*5"} ) data = response.json() print(f"Result: {data['result']}") # Result: 35.0 # Validate an expression response = requests.get("https://bodmasparser.onrender.com/validate/3%2B4*5") data = response.json() print(f"Valid: {data['valid']}") # Valid: true

JavaScript Example

// Parse and evaluate an expression fetch('https://bodmasparser.onrender.com/parse', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ expression: '(3+4)*5' }) }) .then(response => response.json()) .then(data => console.log(`Result: ${data.result}`)); // Result: 35.0 // Validate an expression fetch('https://bodmasparser.onrender.com/validate/3%2B4*5') .then(response => response.json()) .then(data => console.log(`Valid: ${data.valid}`)); // Valid: true

cURL Example

# Parse and evaluate an expression curl -X POST "https://bodmasparser.onrender.com/parse" \ -H "Content-Type: application/json" \ -d '{"expression": "(3+4)*5"}' # Validate an expression curl "https://bodmasparser.onrender.com/validate/3%2B4*5"

Back to Visualizer

Return to the BodmasParser Visualizer