Fix unit reduction elimination bugs

* Handle 'chains' of unit reductions starting in a single state
* Avoid eliminating rules which will later receive aliases
This commit is contained in:
Max Brunsfeld 2018-03-12 07:54:18 -07:00
parent 72849787b1
commit 7183f8d3e7
4 changed files with 112 additions and 35 deletions

View file

@ -0,0 +1,12 @@
==========================================
Aliases on rules that are unit reductions
==========================================
one two three;
---
(statement
(identifier)
(b_prime (identifier))
(c_prime (identifier)))

View file

@ -0,0 +1,70 @@
{
"name": "aliased_unit_reductions",
"extras": [
{"type": "PATTERN", "value": "\\s"}
],
"rules": {
"statement": {
"type": "SEQ",
"members": [
{"type": "SYMBOL", "name": "_a"},
{
"type": "ALIAS",
"named": true,
"value": "b_prime",
"content": {
"type": "SYMBOL",
"name": "_b"
}
},
{
"type": "ALIAS",
"named": true,
"value": "c_prime",
"content": {
"type": "SYMBOL",
"name": "_c"
}
},
{
"type": "STRING",
"value": ";"
}
]
},
"_a": {
"type": "SYMBOL",
"name": "_A"
},
"_A": {
"type": "SYMBOL",
"name": "identifier"
},
"_b": {
"type": "SYMBOL",
"name": "_B"
},
"_B": {
"type": "SYMBOL",
"name": "identifier"
},
"_c": {
"type": "SYMBOL",
"name": "_B"
},
"_C": {
"type": "SYMBOL",
"name": "identifier"
},
"identifier": {"type": "PATTERN", "value": "[a-z]+"}
}
}

View file

@ -0,0 +1,5 @@
Normally, when there are invisible rules (rules whose names start with an `_`) that simply
wrap another rule, there is an optimization at parser-generation time called *Unit Reduction Elimination* that avoids creating nodes for those rules at runtime. One case where this
optimization must *not* be applied is when those invisible rules are going to be aliased
within their parent rule. In that situation, eliminating the invisible node could cause
the alias to be incorrectly applied to its child.