Skip to content

Complete CST to include parens #6

@glebec

Description

@glebec

Losing the parens makes the parse tree lose information that could be used e.g. to rebuild the original expression. In other words, we technically have an AST, not a CST. Refactoring to be a CST could work if the Factor tree nodes have lhs, child, and rhs fields as follows:

Production Rule lhs child rhs
F -> (E) Lparen E Rparen
F -> -F Sub F EpsilonF
F -> num EpsilonF num EpsilonF

That way the catamorphism for the recursive generators could look like:

{
  Factor: (lhs, child, rhs) => generate(lhs) * generate(child) * generate(rhs),
  LParen: () => 1, // identity: '' for RPN, '(' for rebuilding original, etc.
  Rparen: () => 1,
}

This is an artifact of the way tree nodes can have varying shape, making it difficult to apply identical logic to every node. An alternative is to simply embrace different node shapes and embed explicit conditional logic in the generator, instead of relying entirely on daggy's cata function for branching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions