https://github.com/st235/pratt-parser
A lightweight Pratt parser for made-up language expressions
https://github.com/st235/pratt-parser
parser pratt-parser scanning
Last synced: about 19 hours ago
JSON representation
A lightweight Pratt parser for made-up language expressions
- Host: GitHub
- URL: https://github.com/st235/pratt-parser
- Owner: st235
- License: mit
- Created: 2026-05-06T12:49:16.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2026-05-09T13:46:13.000Z (2 months ago)
- Last Synced: 2026-05-09T15:42:24.462Z (2 months ago)
- Topics: parser, pratt-parser, scanning
- Language: C
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pratt Parser
A lightweight expression parser built around [the Pratt parsing technique](https://en.wikipedia.org/wiki/Operator-precedence_parser).
`pratt-parser` focuses on clear and extensible operator precedence handling while also providing a complete lexer and scanner implementation for tokenization.
## Supported Operators
Operators are listed from **lowest precedence** to **highest precedence**.
| Precedence | Operators | Associativity |
| ---------- | -------------------- | ------------- |
| 1 | `?:` (ternary) | Right |
| 2 | `+`, `-` | Left |
| 3 | `*`, `/`, `%` | Left |
| 4 | Unary `+`, Unary `-` | Right |
| 5 | `!` (factorial) | Left |
| 6 | `[]` (array access) | Left |
| 7 | `.` (member access) | Right |
## Example
>[!INFO]
>See [`tests.c`](./tests/tests.c) for complete tests set.
Running the tests suit should yeild something similar to the output below.
```txt
==== SCANNER ====
1 PASSED
-91 PASSED
1 + 2 PASSED
14 / 3 PASSED
3+15*-7 PASSED
a%5 PASSED
120! PASSED
a ? b : c PASSED
a[5] PASSED
a().b(-3) + 31 PASSED
==== PARSER ====
2 PASSED
1+2+3 PASSED
11 + 2 - 3 * 7 PASSED
3 * 5 - 4 PASSED
2 - 3 / 4 PASSED
1 + 44 - 7 * 21 % 3 PASSED
2 + a.b.c PASSED
1 + 2 + f . g . h * 3 * 4 PASSED
-1+3*-2 PASSED
--1 * 2 PASSED
--f . g PASSED
3*12 - 4!/6 PASSED
5!!!*3+3-2 PASSED
(1+2)*3 PASSED
(((0)))+(3*2) PASSED
a[(5+3)*2] PASSED
7*a.b[2+3]-4 PASSED
a[2+b[4]-3*d[4-3]] PASSED
x[0][1] PASSED
a ? 2 : 3 + 4 PASSED
a + 3*2 ? 2 - 1/6 : 3!-4 PASSED
```