parsing - Explanation on this FIRST function -
ll(1) grammar:
(1) var -> id dimlist (2) dimlist -> ε dimlist' (3) dimlist' -> dim dimlist' (4) dimlist' -> ε (5) dim -> [ const ] and, in script reading, says function first(ε dimlist') gives {#, [}. but, how?
my guess since right side of (2) begins ε, skips epsilon , takes first(dimlist') is, considering (3) , (5), equal {[}, also, because of (4), takes follow(dimlist') {#}.
other way that, since (2) begins ε skips epsilon , takes first(dimlist') takes follow(dimlist) (2)...
first 1 makes more sense me, though i'm still in process of learning basics of ll(1) grammars appreciate if takes time make clear, thank you.
edit: and, of course, neither of these true.
the usual definition of first function result in first(dimlist) (or, if like, first(ε dimlist') being {ε, [}. ε in first(ε dimlist') because both ε , dimlist' nullable. [ element because first symbol in derivation of ε dimlist, same saying first symbol in derivation of dimlist'.
another way of saying that:
first(ε dimlist' #) = {#, [}
we define function predict:
predict(ω) = first(ω follow(ω))
and can see
predict(dimlist) = first(dimlist follow(dimlist)) = {#, [}
here, first(ω) set of strings of terminals (of length ≤ 1) appear @ beginning of derivation of ω, while predict(ω) set of strings of terminals (of length ≤ 1) present in input when derivation of ω possible.
it's not uncommon confuse first , predict, it's better keep difference straight.
note of these functions can generalized strings of maximum length k, written firstk, followk , predictk, , definition of predictk similar above:
predictk(ω) = firstk(ω followk(ω))
Comments
Post a Comment