Chapter 4: Syntax Analysis

Example

. For the expression grammar

E T E'

E' +T E' | ε

T F T'

T' * F T' | ε

F ( E ) | id

First(E) = First(T) = First(F) = { (, id }

First(E') = {+, ε }

First(T') = { *, ε }

Consider the grammar shown above. For example, id and left parenthesis are added to FIRST(F) by rule (3) in the definition of FIRST with i = 1 in each case, since FIRST(id) = {id} and FIRST{'('} = { ( } by rule (1). Then by rule (3) with i = 1, the production T = FT' implies that id and left parenthesis are in FIRST(T) as well. As another example, e is in FIRST(E') by rule (2).