Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aaravmalani/lexmath
A lexer for mathematical expressions in C
https://github.com/aaravmalani/lexmath
arithmetic c cmake collaborate expressions lexer math mathematics tokenizer
Last synced: 4 days ago
JSON representation
A lexer for mathematical expressions in C
- Host: GitHub
- URL: https://github.com/aaravmalani/lexmath
- Owner: AaravMalani
- License: mit
- Created: 2023-05-12T11:51:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-12T16:43:11.000Z (over 1 year ago)
- Last Synced: 2024-11-05T22:12:13.599Z (about 2 months ago)
- Topics: arithmetic, c, cmake, collaborate, expressions, lexer, math, mathematics, tokenizer
- Language: C
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# LexMath: A lexer/tokenizer for mathematical expressions
## Installation
- Go to the [latest release](https://github.com/AaravMalani/lexmath/releases/latest)
- Download the RPM file for Red-Hat based Linux Systems, DEB file for Debian based systems, the DLL or LIB file for Windows, or the PKG file for MacOS
```sh
# Ubuntu/Debian
sudo dpkg -i .deb# RedHat Based Linux
sudo rpm -i .rpm# MacOS
installer -pkg .pkg -target /
```
For Windows, you have to link it into the Visual Studio Project using the Linker menu in the Properties of the Project.
Then download the [`lexmath.h`](https://raw.githubusercontent.com/AaravMalani/lexmath/main/include/lexmath.h) file and include that as a header file.## Usage
```c
#includeconst char* lexList[9] = {
"LEX_ADD", // + token
"LEX_DIV", // / token
"LEX_MUL", // * token
"LEX_BRAC_START", // ( token
"LEX_BRAC_END", // ) token
"LEX_EXP", // ^ token
"LEX_SUB", // - token
"LEX_NAME", // list of characters from a-z
"LEX_NUM" // A number (floating point included)
};int main() {
struct LexList* list = lex("1+sin(2x) * 1+3+3^3"); // List of tokens
struct LexToken* lst = list->data; // The pointer to the tokens
for (size_t i = 0; i < list->length; i++){ // Iterate over the tokens
printf("Type: %s (%d)\n", lexList[lst[i].type], lst[i].type); // Print the Type
if (lst[i].data){ // If there is data to print
printf("Value: %s\n", lst[i].data); // Print the data
}
printf("\n"); // Print a new line
}
free_lex(list); // Free the tokens
}```
### OUTPUT
```
Type: LEX_NUM (8)
Value: 1Type: LEX_ADD (0)
Type: LEX_NAME (7)
Value: sinType: LEX_BRAC_START (3)
Type: LEX_NUM (8)
Value: 2Type: LEX_NAME (7)
Value: xType: LEX_BRAC_END (4)
Type: LEX_MUL (2)
Type: LEX_NUM (8)
Value: 1Type: LEX_ADD (0)
Type: LEX_NUM (8)
Value: 3Type: LEX_ADD (0)
Type: LEX_NUM (8)
Value: 3Type: LEX_EXP (5)
Type: LEX_NUM (8)
Value: 3```
## Credits
- [avighnac](https://github.com/avighnac) who introduced the idea of making a mathematical expression parser.