From bbb416b7466603d2fbc6d52fa0132def1b840d29 Mon Sep 17 00:00:00 2001 From: roberts444 Date: Tue, 30 Sep 2025 13:04:23 +0200 Subject: [PATCH] Update README.md Practical constraints limiting the full generality of associativity in EBNF. --- book/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/book/README.md b/book/README.md index 27406d01..171a0f6a 100644 --- a/book/README.md +++ b/book/README.md @@ -1020,6 +1020,8 @@ To calculate the value of the expression, again with `4` as value for `n`, start There is, however, a subtle issue here. EBNF can express precedence but not associativity which controls the grouping of operators that have the same precedence such as `+` and `-`. So far, we silently assumed that expressions are grouped from left to right, not from right to left, which does make sense, however, because `-` in particular is left-associative, not *right-associative*. For example, `n * n + 1 - n / 2 + 42` is grouped as in `(n * n + 1 - n / 2) + 42`, not `n * n + 1 - (n / 2 + 42)`. +More specifically, EBNF, like any other context-free grammar, can prima facie be used to express precedence and associativity, in both cases by suitably leveraging its fundamental recursivity. However, to be useful, a context-free grammar still has to have an efficient implementation, which in practice means a recursive-descent parser. With that in mind, it turns out that implementing the full generality of associativity implied by recursivity for CFGs is not practical (it is difficult to detect termination in a general recursive-descent parser with limited lookahead). Grammars involving mixed associativity, e.g. in mathematical expressions where the left-associativity of arithmetic is combined with the right-associativity of exponentiation, require a hybrid (hand-crafted) parsing solution. + > Specification by context-free grammar, implementation by pushdown automaton Before moving on, we would like to answer an important question: is there a model of computation similar to finite state machines that can implement context-free grammars? The answer is yes. Any context-free grammar can be implemented by a *pushdown automaton* (PDA) which is a model of computation that can do just a bit more than a finite state machine but is still simpler than that of a processor.