Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions book/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down