Compiler Design_ 2-Mark and 16-Mark Answers (1)
Compiler Design_ 2-Mark and 16-Mark Answers (1)
Unit-1
Number of next states Can have zero, one, or Has exactly one next state
multiple next states for a for each input symbol
given input symbol
Starting Point Starts with the start symbol Starts with the input string
of the grammar. (tokens).
Tree Construction Builds the parse tree from Builds the parse tree from
the root to the leaves the leaves to the root.
(pre-order).
Grammar Handling Works well with grammars Can handle a wider range of
where it's easy to predict grammars, including those
the production to use. with left recursion.
Error Handling May have difficulty with Can detect errors later in the
left-recursive grammars and parsing process.
grammars requiring
backtracking.
Error Handling Errors are detected early in Errors are detected later in
the parsing process. the parsing process.
■ This transformation replaces the left recursion with right recursion. E'
represents the part of the expression that can be repeated (i.e., "+ T").
2. For T -> T * F | F:
■ Rewrite as:
T -> F T'
T' -> * F T' | ε
■ Similar to the previous case, this eliminates the left recursion in the T
production.
3. F -> (E) | id: No left recursion.
○ Left Factoring:
○ Definition: Left factoring is a grammar transformation technique that is useful
for top-down parsers. If a non-terminal has multiple productions with a
common prefix, the prefix is factored out.
○ Problem: Top-down parsers have difficulty when a non-terminal has multiple
productions with the same starting symbols because it is not clear which
production to choose.
○ Example:
S -> i E t S | i E t S e S | a
Both productions for S start with "i E t S".
○ Solution: Factor out the common prefix. For productions A -> αβ1 | αβ2,
rewrite them as:
A -> α A'
A' -> β1 | β2
Applying this to the example:
S -> i E t S S' | a
S' -> e S | ε
S ( S -> ( L )
S a S -> a
L ( L -> S L'
L a L -> S L'
● Check whether the following grammar is SLR (1) or not. Explain your answer
with reasons.
S -> L = R
S -> R
L -> * R
L -> id
R -> L
Solution:
1. Augment the grammar:
S' -> S
S -> L = R
S -> R
L -> * R
L -> id
R -> L
3. Construct the SLR(1) parsing table (states and transitions omitted for
brevity).
4. Check for conflicts:
In the construction of the SLR(1) parsing table, a shift-reduce conflict arises in
a state when the lookahead symbol is '='. Specifically, there's a conflict
between:
■ Shifting '=' onto the stack (from the production S -> L = R).
■ Reducing by the production R -> L (since '=' is in FOLLOW(R)).
3. Construct the SLR(1) parsing table. (Table construction is lengthy and omitted
here, but would involve creating states, items, GOTO and ACTION tables, and
resolving any conflicts.)
● State and explain the rules to compute FIRST and FOLLOW functions
Given a grammar G, FIRST and FOLLOW are two functions associated with
non-terminal symbols.
FIRST(α)
* FIRST(α), where α is any string of grammar symbols, is the set of terminal symbols
that begin the strings derived from α.
* **Rules for computing FIRST(X), where X is a grammar symbol (terminal or
non-terminal):**
1. If X is a terminal, then FIRST(X) = { X }.
2. If X is a non-terminal and X -> Y1 Y2 ... Yn is a production, then:
* Add FIRST(Y1) - { ε } to FIRST(X).
* If Y1 can derive ε (i.e., ε is in FIRST(Y1)), then add FIRST(Y2) - { ε } to FIRST(X),
and so on, until Yn or a symbol that cannot derive ε.
* If Y1 Y2 ... Yn can all derive ε, then add ε to FIRST(X).
3. If X -> ε is a production, then add ε to FIRST(X).
**FOLLOW(A)**
* FOLLOW(A), where A is a non-terminal, is the set of terminals that can appear
immediately to the right of A in some sentential form.
* **Rules for computing FOLLOW(A) for a non-terminal A:**
1. If A is the start symbol, then add '$' (end-of-input marker) to FOLLOW(A).
2. If there is a production B -> αAβ, then add FIRST(β) - { ε } to FOLLOW(A).
3. If there is a production B -> αA, or a production B -> αAβ where ε is in FIRST(β),
then add FOLLOW(B) to FOLLOW(A).
**Example:**
```
E -> T E'
E' -> + T E' | ε
T -> F T'
T' -> * F T' | ε
F -> ( E ) | id
```
Computing FIRST and FOLLOW (result):
```
FIRST(E) = { '(', 'id' }
FIRST(E') = { '+', ε }
FIRST(T) = { '(', 'id' }
FIRST(T') = { '*', ε }
FIRST(F) = { '(', 'id' }
FOLLOW(E) = { ')', '$' }
FOLLOW(E') = { ')', '$' }
FOLLOW(T) = { '+', ')', '$' }
FOLLOW(T') = { '+', ')', '$' }
FOLLOW(F) = { '*', '+', ')', '$' }
```
3. Construct the SLR parsing table: (Table construction is lengthy and omitted
here. It involves creating states, items, GOTO and ACTION tables.)
4. Parse the sentence (a + b) * c:
Here's a sketch of the parsing process (using a stack and input):
$( E + b )*c$ Reduce E