DParser is a simple yet powerful mathematical expression parser and evaluator designed to interpret and compute arithmetic expressions. This project demonstrates a lexer and parser for a subset of arithmetic operations, including addition, subtraction, multiplication, and division. It can handle expressions with parentheses to define operation precedence, making it an ideal example for those looking to understand or build similar parsing systems.
- Lexical Analysis: Tokenizes input strings into meaningful symbols for parsing.
- Syntax Analysis: Builds an abstract syntax tree (AST) from tokens.
- Expression Evaluation: Computes the result of the parsed expressions.
- Error Handling: Provides detailed error messages for unexpected tokens or syntax errors.
- Test Coverage: Includes unit tests to ensure the robustness of the lexer and parser.
Ensure you have the following installed:
- .NET Core SDK: Version 3.1 or later.
- Visual Studio: Recommended for development and testing.
-
Clone the repository:
git clone https://github.com/dimant/dparser.git cd dparser
-
Build the project:
dotnet build
-
Run the tests:
dotnet test
To execute the sample program that parses and evaluates an expression, follow these steps:
-
Navigate to the project directory:
cd DParser
-
Run the application:
dotnet run
By default, the sample program evaluates the expression "1+2*((23-3)/5))"
and prints the result.
(1 + (2 * ((23 - 3) / 5)))
Input: '1+2*((23-3)/5))' Result: 9
-
DParser: Contains the main source files for the lexer, parser, and expression evaluation.
BinaryExpression.cs
: Defines binary operations (addition, subtraction, etc.).Expression.cs
: Abstract base class for expressions.Lexer.cs
: Tokenizes input strings.NumberExpression.cs
: Represents numerical values in expressions.Parser.cs
: Constructs the AST from tokens.Token.cs
: Defines token types and structures.Program.cs
: Entry point for the application.
-
DParser.Tests: Contains unit tests for validating the functionality.
LexerTests.cs
: Tests for the lexer.ParserTests.cs
: Tests for the parser.
The lexer converts an input string into a sequence of tokens. Each token represents a meaningful symbol in the language, such as numbers, operators, and parentheses.
Example Usage:
var lexer = new Lexer("1+2*3");
Token token;
while ((token = lexer.GetNextToken()).Type != TokenType.EOS) {
Console.WriteLine(token);
}
The parser processes tokens from the lexer to build an abstract syntax tree (AST), which represents the hierarchical structure of the expression.
Example Usage:
var lexer = new Lexer("1+2*(3-4)");
var parser = new Parser(lexer);
var expression = parser.Parse();
Console.WriteLine(expression);
Once the AST is built, it can be evaluated to produce the result of the expression.
Example Usage:
var result = expression.Evaluate();
Console.WriteLine($"Result: {result}");
We welcome contributions to improve the DParser project. Feel free to open issues or submit pull requests with enhancements or bug fixes.
See the LICENSE file for details.