Skip to content
Related Articles

Related Articles

Improve Article

FIRST Set in Syntax Analysis

  • Difficulty Level : Medium
  • Last Updated : 31 Jul, 2021

FIRST(X) for a grammar symbol X is the set of terminals that begin the strings derivable from X. 

Rules to compute FIRST set: 

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

  1. If x is a terminal, then FIRST(x) = { ‘x’ }
  2. If x-> Є, is a production rule, then add Є to FIRST(x).
  3. If X->Y1 Y2 Y3….Yn is a production, 
    1. FIRST(X) = FIRST(Y1)
    2. If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) }
    3. If FIRST (Yi) contains Є for all i = 1 to n, then add Є to FIRST(X).

Example 1: 



Production Rules of Grammar
E  -> TE’
E’ -> +T E’|Є
T  -> F T’
T’ -> *F T’ | Є
F  -> (E) | id

FIRST sets
FIRST(E) = FIRST(T) = { ( , id }
FIRST(E’) = { +, Є }
FIRST(T) = FIRST(F) = { ( , id }
FIRST(T’) = { *, Є }
FIRST(F) = { ( , id }

Example 2: 

Production Rules of Grammar
S -> ACB | Cbb | Ba
A -> da | BC
B -> g | Є
C -> h | Є

FIRST sets
FIRST(S) = FIRST(ACB) U FIRST(Cbb) U FIRST(Ba)
         = { d, g, h, b, a, Є}
FIRST(A) = { d } U FIRST(BC) 
         = { d, g, h, Є }
FIRST(B) = { g , Є }
FIRST(C) = { h , Є }

Notes: 

  1. The grammar used above is Context-Free Grammar (CFG). Syntax of most of the programming language can be specified using CFG.
  2. CFG is of the form A -> B , where A is a single Non-Terminal, and B can be a set of grammar symbols ( i.e. Terminals as well as Non-Terminals)

In the next article “FOLLOW sets in Compiler Design” we will see how to compute Follow sets. 

This article is compiled by Vaibhav Bajpai. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :