# Parsing and Syntax directed translation

Please wait while the activity loads.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled.

Question 1 |

What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a) to parse a string with n tokens?

n/2 | |

n-1 | |

2n-1 | |

2 ^{n} |

**GATE CS 2013**

**Parsing and Syntax directed translation**

**Discuss it**

Question 1 Explanation:

Given in the question, a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a).
To get maximum number of Reduce moves, we should make sure than in each sentential form only one terminal is reduced. Since there is no unit production, so last 2 tokens will take only 1 move.
So To Reduce input string of n tokens, first Reduce n-2 tokens using n-2 reduce moves and then Reduce last 2 tokens using production which has . So total of n-2+1 = n-1 Reduce moves.
Suppose the string is abcd. ( n = 4 ).
We can write the grammar which accepts this string as follows:

S->aB B->bC C->cdThe Right Most Derivation for the above is:

S -> aB ( Reduction 3 ) -> abC ( Reduction 2 ) -> abcd ( Reduction 1 )We can see here that no production is for unit or epsilon. Hence 3 reductions here. We can get less number of reductions with some other grammar which also does't produce unit or epsilon productions,

S->abA A-> cdThe Right Most Derivation for the above as:

S -> abA ( Reduction 2 ) -> abcd ( Reduction 1 )Hence 2 reductions. But we are interested in knowing the maximum number of reductions which comes from the 1st grammar. Hence total 3 reductions as maximum, which is ( n - 1) as n = 4 here. Thus, Option B.

Question 2 |

Consider the following two sets of LR(1) items of an LR(1) grammar.

X -> c.X, c/d X -> .cX, c/d X -> .d, c/d X -> c.X, $ X -> .cX, $ X -> .d, $Which of the following statements related to merging of the two sets in the corresponding LALR parser is/are FALSE?

1. Cannot be merged since look aheads are different. 2. Can be merged but will result in S-R conflict. 3. Can be merged but will result in R-R conflict. 4. Cannot be merged since goto on c will lead to two different sets.

1 only | |

2 only | |

1 and 4 only | |

1, 2, 3, and 4 |

**GATE CS 2013**

**Parsing and Syntax directed translation**

**Discuss it**

Question 2 Explanation:

The given two LR(1) set of items are :

X -> c.X, c/d X -> .cX, c/d X -> .d, c/d and X -> c.X, $ X -> .cX, $ X -> .d, $The symbols/terminals after the comma are Look-Ahead symbols. These are the sets of LR(1) ( LR(1) is also called CLR(1) ) items. The LALR(1) parser combines those set of LR(1) items which are identical with respect to their 1st component but different with respect to their 2nd component. In a production rule of a LR(1) set of items, ( A -> B , c ) , A->B is the 1st component , and the Look-Ahead set of symbols, which is c here, is the second component. Now we can see that in the sets given, 1st component of the corresponding production rule is identical in both sets, and they only differ in 2nd component ( i.e. their look-ahead symbols) hence we can combine these sets to make a a single set which would be :

X -> c.X, c/d/$ X -> .cX, c/d/$ X -> .d, c/d/$This is done to reduce the total number of parser states. Now we can check the statements given. Statement 1 : The statement is false, as merging has been done because 2nd components i.e. look-ahead were different. Statement 2 : In the merged set, we can't see any Shift-Reduce conflict ( because no reduction even possible, reduction would be possible when a production of form P -> q. is present) Statement 3 : In the merged set, we can't see any Reduce-Reduce conflict ( same reason as above, no reduction even possible, so no chances of R-R conflict ) Statement 4: This statement is also wrong, because goto is carried on Non-Terminals symbols, not on terminal symbols, and c is a terminal symbol. Thus, all statements are wrong, hence option D.

Question 3 |

For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. is the empty string, $ indicates end of input, and, | separates alternate right hand sides of productions.

A | |

B | |

C | |

D |

**GATE CS 2012**

**Parsing and Syntax directed translation**

**Discuss it**

Question 3 Explanation:

First(X) - It is the set of terminals that begin the strings derivable from X. Follow(X) - It is the set of terminals that can appear immediately to the right of X in some sentential form. Now in the above question, FIRST(S) = { a, b, epsilon} FIRST(A) = FIRST(S) = { a, b, epsilon} FIRST(B) = FIRST(S) = { a, b, epsilon} FOLLOW (A) = { b , a } FOLLOW (S) = { $ } U FOLLOW (A) = { b , a , $ } FOLLOW (B) = FOLLOW (S) = { b ,a , $ } epsilon corresponds to empty string.

Question 4 |

Consider the date same as above question. The appropriate entries for E1, E2, and E3 are

A | |

B | |

C | |

D |

**GATE CS 2012**

**Parsing and Syntax directed translation**

**Discuss it**

Question 4 Explanation:

As we need to find entries E1, E2 and E3 which are against Non terminals S and B, so we will deal with only those productions which have S and B on LHS.
Here representing the parsing table as M[ X , Y ], where X represents rows( Non terminals) and Y represents columns(terminals).

Rule 1: if P --> Q is a production, for each terminal 't' in FIRST(Q) add P-->Q to M [ P , t ] Rule 2 : if epsilon is in FIRST(Q), add P --> Q to M [ P , f ] for each f in FOLLOW(P).For the production rule S--> aAbB, it will be added to parsing table at the position M[ S , FIRST( aAbB ) ], Now FIRST(aAbB) = {a}, hence add S--> aAbB to M[ S , a ] which is E1. For the production rule S--> bAaB, it will be added to parsing table at the position M[ S , FIRST( bAaB ) ], Now FIRST(bAaB) = {b}, hence add S--> bAaB to M[ S , b ] which is E2 For the production rule S--> epsilon , it will be added to parsing table at the position M[ S , FOLLOW(S) ], Now FOLLOW(S) = { a , b , $ }, hence add S --> epsilon to M[ S , a ] and M[ S , b ] which are again E1 and E2 respectively. For the production rule B --> S, it will be added to parsing table at the position M[ B , FIRST( S ) ], Now FIRST(S) also contains epsilon, hence add B --> S to M[ S , FOLLOW(B) ], and FOLLOW(B) contains {$}, i.e. M[ S , $ ] which is E3. epsilon corresponds to empty string.

Question 5 |

The grammar S → aSa | bS | c is

LL(1) but not LR(1) | |

LR(1)but not LR(1) | |

Both LL(1)and LR(1) | |

Neither LL(1)nor LR(1) |

**GATE CS 2010**

**Parsing and Syntax directed translation**

**Discuss it**

Question 5 Explanation:

First(aSa) = a First(bS) = b First(c) = c All are mutually disjoint i.e no common terminal between them, the given grammar is LL(1). As the grammar is LL(1) so it will also be LR(1) as LR parsers are more powerful then LL(1) parsers. and all LL(1) grammar are also LR(1) So option C is correct.Below are more details. A grammar is LL(1) if it is possible to choose the next production by looking at only the next token in the input string.

Formally, grammar G is LL(1) if and only if For all productions A → α1 | α2 | ... | αn, First(αi) ∩ First(αj) = ∅, 1 ≤ i,j ≤ n, i ≠ j. For every non-terminal A such that First(A) contains ε, First(A) ∩ Follow(A) = ∅Source: https://s3-ap-southeast-1.amazonaws.com/erbuc/files/4147_870334aa-9922-4f78-9c2c-713e7a7f0d53.pdf

Question 6 |

Match all items in Group 1 with correct options from those given in Group 2.

Group 1 Group 2 P. Regular expression 1. Syntax analysis Q. Pushdown automata 2. Code generation R. Dataflow analysis 3. Lexical analysis S. Register allocation 4. Code optimization

P-4. Q-1, R-2, S-3 | |

P-3, Q-1, R-4, S-2 | |

P-3, Q-4, R-1, S-2 | |

P-2, Q-1, R-4, S-3 |

**GATE-CS-2009**

**Parsing and Syntax directed translation**

**Discuss it**

Question 6 Explanation:

Regular expressions are used in lexical analysis.
Pushdown automata is related to context free grammar which is related to syntax analysis.
Dataflow analysis is done in code optimization.
Register allocation is done in code generation.

Question 7 |

Which of the following statements are TRUE?

I. There exist parsing algorithms for some programming languages whose complexities are less than O(n^{3}). II. A programming language which allows recursion can be implemented with static storage allocation. III. No L-attributed definition can be evaluated in The framework of bottom-up parsing. IV. Code improving transformations can be performed at both source language and intermediate code level.

I and II | |

I and IV | |

III and IV | |

I, III and IV |

**GATE-CS-2009**

**Parsing and Syntax directed translation**

**Discuss it**

Question 7 Explanation:

II is false, in recursion, compiler cannot determine the space needed for recursive calls.
III is false. See http://www.cs.sunysb.edu/~cse304/Fall09/Lectures/attributes-handout.pdf

Question 8 |

Which of the following describes a handle (as applicable to LR-parsing) appropriately?

It is the position in a sentential form where the next shift or reduce operation will occur
| |

It is non-terminal whose production will be used for reduction in the next step | |

It is a production that may be used for reduction in a future step along with a position in the sentential form where the next shift or reduce operation will occur | |

It is the production p that will be used for reduction in the next step along with a position in the sentential form where the right hand side of the production may be found |

**Parsing and Syntax directed translation**

**GATE CS 2008**

**Discuss it**

Question 8 Explanation:

Let's first understand the terminology used here.

**LR Parsing**- Here 'L' stands for Left to Right screening of input string, and 'R' stands for Right Most Derivation in Reverse ( because it is about bottom-up parsing).**Sentential Form**- Suppose For a given Context Free Grammar G, we have a start symbol S, then to define Language generated by Grammar G, i.e. L(G), we start the derivation starting from S using the production rules of the grammar. After one complete derivation we get a string w which consists of only terminal symbols, i.e. w belongs to L(G). Then we can say that w is a sentence of the Grammar G. Now, while the derivation process, if it gets some form q, where q may contain some Non-terminal then we say that q is a sentential form of the Grammar G. Even the start symbol S is also the sentential form of the Grammar G ( because it also contains Non-terminal S).For Ex : Grammar is : S-> aAcBe A->Ab|b B->d Input string : abbcde Derivation : ( Top-Down, Right Most Derivation) S->aAcBe ->aAcde ->aAbcde ->abbcdeHere { abbcde } is the sentence of the Grammar( because it contains only terminal symbols), and { S, aAcBe, aAcde, aAbcde } are the sentential forms of the G ( because these forms contain non-terminals during the derivation process ) Now, let's look at the question. The question is related to LR parsing which is a bottom-up parsing. Let's take the same grammar above, as it is a bottom up parsing we need to start from the string "abbcde" and try to get S using production rules.

: abbcde ->aAbcde ( using A-> b ) ->aAcde ( using A-> Ab ) ->aAcBe ( using B -> d ) ->S ( using S-> aAcBe )The above process is called reduction. The RHS of the Production which is being replaced by their LHS is called Handle, So , { b, Ab, d, aAcBe} are handles, and replacing it with its LHS is called Handle-Pruning. Hence option D suits best.

Question 9 |

An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if

the SLR(1) parser for G has S-R conflicts | |

the LR(1) parser for G has S-R conflicts | |

the LR(0) parser for G has S-R conflicts | |

the LALR(1) parser for G has reduce-reduce conflicts |

**Parsing and Syntax directed translation**

**GATE CS 2008**

**Discuss it**

Question 9 Explanation:

Both LALR(1) and LR(1) parser uses LR(1) set of items to form their parsing tables. And LALR(1) states can be find by merging LR(1) states of LR(1) parser that have the same set of first components of their items.
i.e. if LR(1) parser has 2 states I and J with items A->a.bP,x and A->a.bP,y respectively, where x and y are look ahead symbols, then as these items are same with respect to their first component, they can be merged together and form one single state, let's say K. Here we have to take union of look ahead symbols. After merging, State K will have one single item as A->a.bP,x,y . This way LALR(1) states are formed ( i.e. after merging the states of LR(1) ).
Now, S-R conflict in LR(1) items can be there whenever a state has items of the form :

A-> a.bB , p C-> d. , b i.e. it is getting both shift and reduce at symbol b, hence a conflict.Now, as LALR(1) have items similar to LR(1) in terms of their first component, shift-reduce form will only take place if it is already there in LR(1) states. If there is no S-R conflict in LR(1) state it will never be reflected in the LALR(1) state obtained by combining LR(1) states. Note: But this process of merging may introduce R-R conflict, and then the Grammar won't be LALR(1).

Question 10 |

Which one of the following is a top-down parser?

Recursive descent parser. | |

Operator precedence parser. | |

An LR(k) parser. | |

An LALR(k) parser |

**Parsing and Syntax directed translation**

**GATE-CS-2007**

**Discuss it**

Question 10 Explanation:

Recursive Descent parsing is LL(1) parsing which is top down parsing.

There are 76 questions to complete.