You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My issue lies with the sd=sizeDef? part of the portDef rule. I would like to keep the rule definition as simple as possible so I opted to use the ? operator rather than handling each case individually. As you can see, I have a line where I check if the sizeDef rule is present, and change the output based on that: sizeDef: $sd ? $sd.value : {msb: 0, lsb: 0},. The problem is that I get an error from the antlr4 tool when trying to generate the parser, which reads as follows:
missing attribute access on rule reference sd in $sd
I'm wondering why I cannot reference $sd directly, as I cannot reference $sd.value since $sd is null in some cases, and that causes a crash at runtime. This seems to only be an issue with the parser generation tool rather than the resulting code, as I manually added code to check for $sd !== null in the parser file (which ends up being localctx.sd in the generated code) and it works fine.
So, my question is, what is the best way to handle a case like this? I know I could just split the rule into two cases, one where sizeDef is present and one where it isn't, but then the grammar rules get exponentially larger when there are multiple uses of the ? operator in a rule. Also, it seems like direct references of terminals work just fine, and this problem only happens with nonterminals.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm relatively new to working with parsers so forgive me if I am using the wrong words for things.
I have the following rules as a subset of a grammar I am working on to parse System Verilog for a Javascript target:
My issue lies with the
sd=sizeDef?
part of the portDef rule. I would like to keep the rule definition as simple as possible so I opted to use the ? operator rather than handling each case individually. As you can see, I have a line where I check if the sizeDef rule is present, and change the output based on that:sizeDef: $sd ? $sd.value : {msb: 0, lsb: 0},
. The problem is that I get an error from the antlr4 tool when trying to generate the parser, which reads as follows:I'm wondering why I cannot reference
$sd
directly, as I cannot reference$sd.value
since$sd
is null in some cases, and that causes a crash at runtime. This seems to only be an issue with the parser generation tool rather than the resulting code, as I manually added code to check for$sd !== null
in the parser file (which ends up being localctx.sd in the generated code) and it works fine.So, my question is, what is the best way to handle a case like this? I know I could just split the rule into two cases, one where sizeDef is present and one where it isn't, but then the grammar rules get exponentially larger when there are multiple uses of the ? operator in a rule. Also, it seems like direct references of terminals work just fine, and this problem only happens with nonterminals.
Thanks in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions