The Wayback Machine - https://web.archive.org/web/20250312180246/https://www.geeksforgeeks.org/first-set-in-syntax-analysis/
Open In App

FIRST Set in Syntax Analysis

Last Updated : 28 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The FIRST set is used in syntax analysis to identify which terminal symbols can appear at the start of strings derived from a non-terminal. It is crucial for LL and LR parsers, helping them decide which rules to apply.

To compute the FIRST set:

  1. Add the terminal itself to the FIRST set for all terminals.
  2. For non-terminals, add the FIRST terminal from the right-hand side of their productions.
  3. Repeat until no more symbols can be added.

In LL parsers, if the next input symbol matches the FIRST set of a non-terminal, that rule can be safely applied. The FIRST set is also used in computing the FOLLOW set, which identifies what can come immediately after a non-terminal in a grammar.

Rules to Compute FIRST Set 

  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 programming languages 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)

Features of FIRST Sets

  • Definition: The FIRST set of a nonterminal symbol is the set of terminal symbols that can appear as the first symbol in a string derived from that nonterminal. In other words, it is the set of all possible starting symbols for a string derived from that nonterminal.
  • Calculation: The FIRST set for each nonterminal symbol is calculated by examining the productions for that symbol and determining which terminal symbols can appear as the first symbol in a string derived from that production.
  • Recursive Descent Parsing: The FIRST set is often used in recursive descent parsing, which is a top-down parsing technique that uses the FIRST set to determine which production to use at each step of the parsing process.
  • Ambiguity Resolution: The FIRST set can help resolve ambiguities in the grammar by providing a way to determine which production to use based on the next input symbol.
  • Follow Set: The FOLLOW set is another concept used in syntax analysis that represents the set of symbols that can appear immediately after a nonterminal symbol in a derivation. The FOLLOW set is often used in conjunction with the FIRST set to resolve parsing conflicts and ensure that the parser can correctly identify the structure of the input code.

Advantages of Using FIRST Set in Syntax Analysis

  • Improved parsing: FIRST set can be used to determine which production rule should be used to expand a non-terminal in an LL or LR parser, which helps to improve the accuracy and efficiency of the parsing process.
  • Ambiguity resolution: FIRST set can be used to resolve ambiguities in the grammar, by determining which production rule should be used in cases where multiple production rules can apply to the same non-terminal.
  • Simplified error handling: By determining which production rule should be used based on the FIRST set, an LL or LR parser can detect errors in the source code more quickly and accurately.

Disadvantages of Using FIRST Set in Syntax Analysis

  • Complexity: Computing FIRST set can be a complex process, especially for grammars with many non-terminals and production rules.
  • Limited applicability: FIRST set is mainly used in LL and LR parsing algorithms, and may not be applicable to other types of parsing algorithms.
  • Limitations of LL parsing: LL parsing is limited in its ability to handle certain types of grammars, such as those with left-recursive rules, which can lead to an infinite loop in the parser.

Overall, the use of FIRST set in syntax analysis can improve the accuracy and efficiency of the parsing process, but it should be balanced against the complexity and limitations of the parsing algorithm being used. 

Frequently Asked Questions on FIRST Set in Syntax Analysis – FAQs

What is the first set of a function called?

A function is a specific relationship between two sets of numbers. In a function, each number in the first set, called the domain, corresponds to exactly one number in the second set, known as the range. This means that for every input, there is only one output. This strict definition distinguishes functions from general relationships, where one input can have multiple outputs. Understanding functions is crucial in math as they show how values are connected.

What is first and follow sets?

First sets are created by listing all the symbols (both terminals and non-terminals) that can come from the left side of a production rule. Then, Follow sets are built by adding all the symbols that can appear after a non-terminal on the right side of a production rule.

What are the two parts of syntax analysis?

Almost all compilers divide syntax analysis into two parts: The lexical analyzer focuses on smaller language elements like variable names and numbers, while the syntax analyzer handles larger structures like expressions, statements, and whole program sections.

What is the start symbol in parsing?

In formal language theory, you can think of the start symbol and the string you’re trying to parse as two points. The grammar rules act like ‘paths’ that connect the start symbol to any valid string in the language and back.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg