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

**Rules to compute FIRST set:**

- If x is a terminal, then FIRST(x) = { ‘x’ }
- If x-> Є, is a production rule, then add Є to FIRST(x).
- If X->Y1 Y2 Y3….Yn is a production,
- FIRST(X) = FIRST(Y1)
- If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) }
- 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) | idFIRST setsFIRST(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 setsFIRST(S) = FIRST(A) U FIRST(B) U FIRST(C) = { d, g, h, Є, b, a} FIRST(A) = { d } U FIRST(B) = { d, g , h, Є } FIRST(B) = { g , Є } FIRST(C) = { h , Є }

**Notes: **

- The grammar used above is Context-Free Grammar (CFG). Syntax of most of the programming language can be specified using CFG.
- 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

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- FOLLOW Set in Syntax Analysis
- Introduction to Syntax Analysis in Compiler Design
- Syntax Directed Translation in Compiler Design
- Compiler Design | Syntax Directed Definition
- S - attributed and L - attributed SDTs in Syntax directed translation
- C program to check syntax of 'for' loop
- Application of Syntax Directed Translation
- Difference between First Come First Served (FCFS) and Longest Job First (LJF) CPU scheduling algorithms
- Introduction of Lexical Analysis
- Analysis of different sorting techniques
- Data flow analysis in Compiler
- Difference between Posteriori and Priori analysis
- Semantic Analysis in Compiler Design
- Analysis and Design of Combinational and Sequential circuits
- Difference between Multi Level Queue Scheduling (MLQ) and First Come First Served (FCFS)
- Difference between First Come First Served (FCFS) and Round Robin (RR) Scheduling Algorithm
- Open shortest path first (OSPF) - Set 2
- Why FIRST and FOLLOW in Compiler Design?
- Shortest Job First CPU Scheduling with predicted burst time
- Open Shortest Path First (OSPF) Protocol fundamentals