Skip to content

Add a basic Pratt parser#27

Merged
jahav merged 3 commits into
developfrom
pratt-parser-1
Oct 20, 2025
Merged

Add a basic Pratt parser#27
jahav merged 3 commits into
developfrom
pratt-parser-1

Conversation

@jahav

@jahav jahav commented Oct 20, 2025

Copy link
Copy Markdown
Member

The PR adds a basic Pratt parser that can parse numbers, arithmetic operations and numbers. In 3.0, it should replace current descent recursive parser.

What is a Pratt parser? A parser uniquely suited to parsing expressions. Instead of mucking around with Expressions, Factors, Terms, Atoms and all that rather ugly stuff in a recursive parser, Pratt parser is a modular and it's core is super simple (=maintainable). Since every formula in Excel is an expression, it's a match made in heaven. Here are some resources

Anyway, here are some other reasons why it's a great fit:

  • Different formulas can't have some rules ([MS-XLSX] 2.2.2.1 -2.2.2.7). Thanks to modularity, I can easily create a parser that does that. Example:
    • cell formulas - no bang references
    • name formulas - can't use sheet-less cell references, can use macro function call
    • conditional format/data validation formulas - basically no reference operators allowed
  • With new lexer design and more generic tokens, I don't have to have two special lexer for A1 and for R1C1. That goes back to bad design that had too much logic in lexer. I will be able to switch parselets from A1 to R1C1 and keep the rest more or less the same (I hope).

@jahav jahav added this to the 3.0 milestone Oct 20, 2025
jahav added 2 commits October 20, 2025 22:47
This initial parser can parse numbers, arithmetic operations and numbers.
IAstFactory needs to know the whole extend of a node, e.g. to replace
it.

Example: factory gets BinaryNode callback and it should replace a node
with a result. The binary node might hav ebeen deeply nested. The range
parameter of callbacks is used to know the whole extent of original formula
that was used to create the binary node. Thus callback can combine left
side, replacement for the binary node, and left side.
@jahav jahav merged commit f711fac into develop Oct 20, 2025
1 check passed
@jahav jahav deleted the pratt-parser-1 branch October 20, 2025 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant