Let’s say I have the following grammar:
Expr -> Expr plus Expr (1)
| Expr minus Expr (2)
| num (3)
When doing a left-most-derivation, after “expanding” Expr, how should one know what production to use?
For instance, if I want to parse 1 + 2 - 3 I’d start off with:
Expr => Expr minus Expr
but this would only happen because this is a small example and it is easy to see that the (3) would rapidly lead to nowhere and (2) wouldn’t fit in the next step. Would the example be a bit more complex and I’d have to make things basically be trial and error. Is this the “right” approach or am I missing something?
Because parsing CFG’s is essentially non-deterministic, there is no general way to “know” beforehand which of the possible rules is the correct one. Basically at the simplest level it really is trial and error. Just like a disjunction boolean expression
if(a OR b OR c), the individual terms will be evaluated one by one until there is a success, otherwise the rule as a whole fails. So your parser should just start with rule (1), attempt to parse the input and if it fails, restart from that point with rule (2) and so on.