Name-Akash Deep Das
Rollno:-SBU190275
Assignment 2
Compiler Design
1. What is parser? How many types?
2. How to design parser table by using First() and Follow() give one example?
Answers
1.A parser is a compiler or interpreter component that breaks data into smaller elements for easy
translation into another language. A parser takes input in the form of a sequence of tokens,
interactive commands, or program instructions and breaks them up into parts that can be used by
other components in programming.
A parser usually checks all data provided to ensure it is sufficient to build a data structure in the
form of a parse tree or an abstract syntax tree.
TYPES
Syntax analyzers follow production rules defined by means of
context-free grammar. The way the production rules are
implemented (derivation) divides parsing into two types : top-
down parsing and bottom-up parsing.
Types of Parser
Top-down Parsing
When the parser starts constructing the parse tree from the start
symbol and then tries to transform the start symbol to the input, it
is called top-down parsing.
Recursive descent parsing : It is a common form of top-down
parsing. It is called recursive as it uses recursive procedures to
process the input. Recursive descent parsing suffers from
backtracking.
Backtracking : It means, if one derivation of a production fails, the
syntax analyzer restarts the process using different rules of same
production. This technique may process the input string more than
once to determine the right production.
Bottom-up Parsing
As the name suggests, bottom-up parsing starts with the input
symbols and tries to construct the parse tree up to the start
symbol.
Example:
Input string : a + b * c
Production rules:
S→E
E→E+T
E→E*T
E→T
T → id
Let us start bottom-up parsing
A+b*c
Read the input and check if any production matches with the input:
A+b*c
T+b*c
E+b*c
E+T*c
E*c
E*T
E
S
Ans2:
If the compiler would have come to know in advance, that what is
the “first character of the string produced when a production rule
is applied”, and comparing it to the current character or token in
the input string it sees, it can wisely take decision on which
production rule to apply.
Let’s take the same grammar from the previous article:
S -> cAd
A -> bc|a
And the input string is “cad”.
Thus, in the example above, if it knew that after reading character
‘c’ in the input string and applying S->cAd, next character in the
input string is ‘a’, then it would have ignored the production rule A-
>bc (because ‘b’ is the first character of the string produced by this
production rule, not ‘a’ ), and directly use the production rule A->a
(because ‘a’ is the first character of the string produced by this
production rule, and is same as the current character of the input
string which is also ‘a’).
Hence it is validated that if the compiler/parser knows about first
character of the string that can be obtained by applying a
production rule, then it can wisely apply the correct production
rule to get the correct syntax tree for the given input string.
Why FOLLOW?
The parser faces one more problem. Let us consider below
grammar to understand this problem.
A -> aBb
B -> c | ε
And suppose the input string is “ab” to parse.
As the first character in the input is a, the parser applies the rule A-
>aBb.
A
/| \
A B b
Now the parser checks for the second character of the input string
which is b, and the Non-Terminal to derive is B, but the parser can’t
get any string derivable from B that contains b as first character.
But the Grammar does contain a production rule B -> ε, if that is
applied then B will vanish, and the parser gets the input “ab”, as
shown below. But the parser can apply it only when it knows that
the character that follows B in the production rule is same as the
current character in the input.
In RHS of A -> aBb, b follows Non-Terminal B, i.e. FOLLOW(B) = {b},
and the current input character read is also b. Hence the parser
applies this rule. And it is able to get the string “ab” from the given
grammar.
A A
/ | \ / \
A B b => a b
|
Ε
So FOLLOW can make a Non-terminal vanish out if needed to
generate the string from the parse tree.
The conclusions is, we need to find FIRST and FOLLOW sets for a
given grammar so that the parser can properly apply the needed
rule at the correct position.