<p>One of the most confusing things to me about VHDL is that the <code>&lt;=</code> operator can mean both &quot;less than or equal to&quot; and it can be a signal assignment operator. When it means &quot;less than or equal to&quot;, it is, of course, left-associative. But when it is a signal assignment operator, it is, of course, right-associative. So, how can it be parsed?</p>

<parsing><operators>
How is the VHDL operator `<=`, which can be both right-associative (signal assignment) and left-associative (less-than-or-equal), parsed?
CC BY-SA 4.0
2024-04-28T07:57:27.423Z
I am not familiar with VHDL, but is this meaningfully different to reusing `=` for assignment and equality? What does using this operator look like concretely?
Michael Homer
CC BY-SA 4.0
2024-04-28T15:24:42.547Z
One option would be to have an AST node which represents a sequence `a <= b <= c <= ... <= x` of arbitrary length, and then determine its meaning later. I don't know VHDL, but in general if `<=` has multiple meanings but only at most one meaning in each lexical position (e.g. `=` might mean assignment in a statement or equality in a condition, as Michael Homer suggests) then it can be parsed differently in each of those positions. Are there any examples in VHDL where `<=` really could mean either depending on something other than the position it appears in the source code?
kaya3
CC BY-SA 4.0
2024-04-28T15:27:00.210Z
Also when you say that less-than-or-equal is "of course" left-associative, I don't see this as obvious or natural; there is very rarely any need to apply a less-than-or-equal operation to the result of another less-than-or-equal operation, and in the few cases where this is done, I would expect the programmer to write parentheses for clarity anyway. And some languages such as Python allow `a <= b <= c` but this is neither left- nor right-associative, it really means `a <= b && b <= c` (but without evaluating `b` twice).
kaya3
<p>General ways to resolve this kind of ambiguity:</p>
<ol>
<li><strong>Context-free then rewrite:</strong> the nodes for both operations are initially parsed into a common AST node type, then a rewrite step somewhere in the pipeline fixes them. For instance, all <code>&lt;=</code> are initially parsed as less-than-or-equal-to operations with left associativity, but when a statement block is parsed, its constructor rewrites all inner <code>&lt;=</code> into signal assignment operations, and re-parents them to make them right-associative.</li>
</ol>
<ol start="2">
<li><strong>Context-sensitive:</strong> the AST could be parsed using <a href="https://en.wikipedia.org/wiki/Recursive_descent_parser" rel="nofollow noreferrer">recursive-descent</a> or another algorithm which intrinsically supports context-sensitivity. In a recursive descent parser, there is a function for every AST node type that parses just nodes of that type, and these functions parse child nodes by calling the respective parser for their type. The statement parser can call a different parser for <code>&lt;=</code> operations then the expression parser.</li>
</ol>
<p>For this specific case, I'm not familiar with VHDL, but some languages don't support composing less-than-or-equal operations at all, so <code>a &lt;= b</code> in expressions has no associativity. <code>bool1 &lt;= bool2</code> is just another way of writing <code>!bool1 || bool2</code>, and the latter is almost always clearer, so some languages make <code>&lt;=</code> on booleans illegal; since <code>&lt;=</code> evaluates to a boolean, it follows that <code>a &lt;= b &lt;= c</code> in an expression is also illegal. If this is the case, then one can parse all <code>&lt;=</code> operations as right-associative.</p>

CC BY-SA 4.0
2024-04-28T16:18:53.410Z
2024-04-28T16:18:53.410Z
tarzh
# How is the VHDL operator `<=`, which can be both right-associative (signal assignment) and left-associative (less-than-or-equal), parsed?
Posted by: username_0 on 2024-04-28
One of the most confusing things to me about VHDL is that the `<=` operator can mean both "less than or equal to" and it can be a signal assignment operator. When it means "less than or equal to", it is, of course, left-associative. But when it is a signal assignment operator, it is, of course, right-associative. So, how can it be parsed?
**Tags:** parsing operators
> **Comments:**
> **username_1**: I am not familiar with VHDL, but is this meaningfully different to reusing `=` for assignment and equality? What does using this operator look like concretely?  
> **username_2**: One option would be to have an AST node which represents a sequence `a <= b <= c <= ... <= x` of arbitrary length, and then determine its meaning later. I don't know VHDL, but in general if `<=` has multiple meanings but only at most one meaning in each lexical position (e.g. `=` might mean assignment in a statement or equality in a condition, as Michael Homer suggests) then it can be parsed differently in each of those positions. Are there any examples in VHDL where `<=` really could mean either depending on something other than the position it appears in the source code?  
> **username_2**: Also when you say that less-than-or-equal is "of course" left-associative, I don't see this as obvious or natural; there is very rarely any need to apply a less-than-or-equal operation to the result of another less-than-or-equal operation, and in the few cases where this is done, I would expect the programmer to write parentheses for clarity anyway. And some languages such as Python allow `a <= b <= c` but this is neither left- nor right-associative, it really means `a <= b && b <= c` (but without evaluating `b` twice).  
---
## 1 answers
### Answer by username_3 on 2024-04-28. Score: 2 | Accepted answer
General ways to resolve this kind of ambiguity:

1. **Context-free then rewrite:** the nodes for both operations are initially parsed into a common AST node type, then a rewrite step somewhere in the pipeline fixes them. For instance, all `<=` are initially parsed as less-than-or-equal-to operations with left associativity, but when a statement block is parsed, its constructor rewrites all inner `<=` into signal assignment operations, and re-parents them to make them right-associative.

2. **Context-sensitive:** the AST could be parsed using [recursive-descent](https://en.wikipedia.org/wiki/Recursive_descent_parser) or another algorithm which intrinsically supports context-sensitivity. In a recursive descent parser, there is a function for every AST node type that parses just nodes of that type, and these functions parse child nodes by calling the respective parser for their type. The statement parser can call a different parser for `<=` operations then the expression parser.

For this specific case, I'm not familiar with VHDL, but some languages don't support composing less-than-or-equal operations at all, so `a <= b` in expressions has no associativity. `bool1 <= bool2` is just another way of writing `!bool1 || bool2`, and the latter is almost always clearer, so some languages make `<=` on booleans illegal; since `<=` evaluates to a boolean, it follows that `a <= b <= c` in an expression is also illegal. If this is the case, then one can parse all `<=` operations as right-associative.
---
