infix_expression_parser

Last updated:

0 purchases

infix_expression_parser Image
infix_expression_parser Images
Add to Cart

Description:

infix expression parser

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.
Features #

Convert an infix expression into its prefix and postfix notations
Evaluate a mathematical expression
Support for basic mathematical operations

Usage #
You can use infix-expression-parser as a library or as a CLI tool. Below are some examples explaining how to use the package in each case.
As a library #
The package exposes two main classes:

InfixExpressionConverter: Use this class when you have an infix expression and want to convert it either to its postfix or infix notation.
ExpressionEvaluator: Use this class when you already have a postfix or prefix expression and want to evaluate it to a result. Use in combination with InfixExpressionConverter to also evaluate infix expressions easily. Note that in order to return a value, you need to provide a context in which you give each symbol in the expression a numeric value.

Example #1: Converting an infix expression
import 'package:infix_expression_parser/infix_expression_converter.dart';

void main() {
// Provide an infix expression
const infixExpression = '(a-b+c)*(d+e*f)';

// Create an instance of InfixExpressionConverter
final converter = InfixExpressionConverter(expression: infixExpression);

final postfixExpression = converter.toPostfixNotation();
final prefixExpression = converter.toPrefixNotation();

print(postfixExpression); // ab-c+def*+*
print(prefixExpression); // *+-abc+d*ef
}
copied to clipboard
Example #2: Evaluating a postfix expression
import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
// Define a context map
const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5};

// Provide a postfix expression
const postfixExpression = 'abc+*d/';

// Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
final value = ExpressionEvaluator.evaluatePostfix(expression: postfixExpression, context: context);

print(value); // 2.8
}
copied to clipboard
Example #3: Evaluating a prefix expression
import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
// Define a context map
const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6};

// Provide a postfix expression
const prefixExpression = '*-a/bc-/ade';

// Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
final value = ExpressionEvaluator.evaluatePrefix(expression: prefixExpression, context: context);

print(value); // -7.0
}
copied to clipboard
Example #4: Evaluating an infix expression
import 'package:infix_expression_parser/infix_expression_converter.dart';
import 'package:infix_expression_parser/expression_evaluator.dart';

void main() {
// Provide an infix expression
const infixExpression = 'a-b+c-d*e';

// Convert it to either its postfix or prefix notation
final converter = InfixExpressionConverter(expression: infixExpression);
final postfixExpression = converter.toPostfixNotation();

// Define a context map
const context = {'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6};

// Use the static method provided by the ExpressionEvaluator class and pass both the expression and the context
final value = ExpressionEvaluator.evaluatePostfix(expression: postfixExpression, context: context);

print(value); // -27.0
}
copied to clipboard
As a CLI tool #
The package expose a infix-parser command explained below:
A CLI tool for evaluating expressions in prefix or postfix notations or converting infix expressions.

Usage: infix-parser <command> [arguments]

Global options:
-h, --help Print this usage information.

Available commands:
convert Converts an expression to its prefix or postfix notation

Run "infix-parser help <command>" for more information about a command.
copied to clipboard
You can use the convert sub-command to parse infix expressions.
Example #1: Converting an infix expression to a prefix expression
pub run infix_expression_parser:main convert '(a-b/c)*(a/d-e)' --prefix
*-a/bc-/ade
copied to clipboard
Example #2: Converting an infix expression to a postfix expression
pub run infix_expression_parser:main convert '(a-b/c)*(a/d-e)' --postfix
abc/-ad/e-*
copied to clipboard
Future improvements #

A sub-command for evaluating expressions in the command line may come in future iterations of this package
Support for other mathematical operations, functions and symbols may come come in future iterations of this package

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.