Open glebec opened 6 years ago
…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.
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 havelhs
,child
, andrhs
fields as follows:lhs
child
rhs
That way the catamorphism for the recursive generators could look like:
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
'scata
function for branching.