Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete CST to include parens #6

Open
glebec opened this issue Jan 17, 2018 · 1 comment
Open

Complete CST to include parens #6

glebec opened this issue Jan 17, 2018 · 1 comment

Comments

@glebec
Copy link
Owner

glebec commented Jan 17, 2018

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.

@glebec
Copy link
Owner Author

glebec commented Feb 1, 2018

…another alternative is to consider that there should actually be three separate "factor" node types, one for every arrow in the grammar.

Rule Node Type Children
F -> (E) ExpFactor (, Expression, )
F -> -F NegFactor -, Factor
F -> num NumFactor 3631

This way you can still dispatch based on node type in the generator, without explicit logic. It means the parse tree has a node for every rule rather than one for every symbol.

@glebec glebec closed this as completed Feb 1, 2018
@glebec glebec reopened this Feb 1, 2018
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

No branches or pull requests

1 participant