2
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Michael Homer
    Commented Apr 28 at 7:57
  • $\begingroup$ 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? $\endgroup$
    – kaya3
    Commented Apr 28 at 15:24
  • 1
    $\begingroup$ 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). $\endgroup$
    – kaya3
    Commented Apr 28 at 15:27

1 Answer 1

2
$\begingroup$

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.
  1. Context-sensitive: the AST could be parsed using recursive-descent 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.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .