0% found this document useful (0 votes)
79 views1 page

Compiler Construction MST Paper UCS802

This document contains a compiler construction exam for a 7th semester BE Computer Science and Engineering student, to be held on September 26th, 2022 for 2 hours with 5 questions worth a total of 35 marks and weighing 25% of the course. The exam will be proctored by 5 faculty members and requires the student to answer all questions with proper justification, assuming any missing data.

Uploaded by

hsingh6be20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views1 page

Compiler Construction MST Paper UCS802

This document contains a compiler construction exam for a 7th semester BE Computer Science and Engineering student, to be held on September 26th, 2022 for 2 hours with 5 questions worth a total of 35 marks and weighing 25% of the course. The exam will be proctored by 5 faculty members and requires the student to answer all questions with proper justification, assuming any missing data.

Uploaded by

hsingh6be20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Roll Number:

Thapar Institute of Engineering and Technology, Patiala


Department of Computer Science and Engineering

B E- COE, CSE (VII Semester) MST Course Code: UCS802


Course Name: Compiler Construction
September 26, 2022 10:30
Time: 2 Hours, M. Marks: 35 Name Of Faculty: Karun Verma, Sunita
Weightage: 25 Garhwal, Avadh Kishor, Rupali
Bhardwaj, Rohit Ahuja

Note: Attempt all questions with proper justification. Assume missing data, if any, suitably.

Ql.a Consider the statement


counter = counter + interest*60 (5+2)
Apply each phase of the compiler on the above statement to generate
the target code.
Q1.b Consider the given grammar S -4 SS + I SS *Ia
Draw the parse tree for the string: aa+a*

Q2 Given the regular expression r = (al b)*Iba (2+3+2)


a) Convert the given r into NFA using Thompson's construction.
b) Convert the obtained NFA into DFA using subset construction.
c) Minimize the obtained DFA in 2 (b).

Q3 Consider the given grammar (3+2+2)


S —> AaAbIBbBa
A —> E
B -4 E
a) Construct First and Follow sets for the non-terminals.
b) Construct the LL(1) parsing table
c) Show the parsing stack and the actions for the string: ba
Q4 Consider the given grammar (4+3)
S-* Ilother
I —> if SI if S else S
a) Construct LR(0) items for the given grammar.
b) Construct the DFA of LR(0) items.
Q5 a) Check whether the given grammar is ambiguous or not. (2+2+3)
E —> E + ESE * EIid
b) Remove left recursion of the grammar Q —> QLIL.
c) Explain in brief the kind of errors handled by lexical and syntax
analysis phase of a compiler.

*********** End of Paper***************

Common questions

Powered by AI

To construct LR(0) items for S -> if S | if S else S, create closure items: - [S -> . if S], [S -> . if S else S] Apply closure by adding possible productions that begin on the right. The DFA has states representing each item set (closure of items). Begin with the initial item [S -> . if S], expanding by reading input symbols 'if' and 'else', moving or adding items accordingly as transitions between states. Create new DFA states when new items are encountered, ensuring all are correctly marked as acceptance or intermediary.

Left recursion appears when a non-terminal leads to a derivation that begins with itself, causing infinite recursion. For Q -> QL | L, it has immediate left recursion. Eliminate it by refactoring: 1. Introduce Q' to handle subsequent derivations. 2. Change production to Q -> LQ' 3. Define Q' as Q' -> LQ' | ε This ensures finite derivations maintaining language validity but removes recursion at production commencement.

Parse trees illustrate the syntactical structure by displaying derivation relationships for valid input according to production rules. They validate syntax conformance and guide further compilation by enabling semantic checks and transformation operations (e.g., cascading grammar transformations into intermediate representations). Parse trees form the backbone for generating target code efficiently, directly influencing parsing efficacy in compilers.

To draw a parse tree for 'aa+a*' using the grammar S -> SS + | SS * | a, start by analyzing how the production rules apply: 1. S -> SS +: The initial part 'aa+' can be derived here using S -> a, S -> a, followed by '+' due to SS +. 2. S* is derived through SS * using the remaining part, 'a' for both S's and the '*' symbol in SS *. Thus, form the parse tree with the root S expanding into its children according to these derivations.

First sets: - First(S) = First(AaAb) ∪ First(BbBa) = {ε, ε} = {a, b} - First(A) = {ε} - First(B) = {ε} Follow sets: - Follow(S) = {$} as S is the start symbol with no follow. - Follow(A) = {a, b} as A concludes parts derivable under AaAb and BbBa. - Follow(B) = {a, b} by similar logic. The LL(1) parsing table is built by using First to determine which production to apply given an input symbol and non-terminal, and Follow to handle epsilon productions determining the next valid symbol. The table is populated using these rules to decide parse actions.

Thompson's construction for (a|b)*|ba begins by creating NFAs for individual components '(a|b)*' and 'ba'. For (a|b)*, use epsilon transitions from a start state to two states for 'a' and 'b', loop each back to the start for Kleene star. 'ba' uses straightforward transitions from 'b' to 'a'. Using subset construction, each NFA's set of states is transformed into DFA states, considering epsilon closures. For instance, starting from the NFA's initial state, generate DFA states based on symbol traversals. Minimizing the DFA involves merging equivalent states (those with identical transitions and accepting conditions). States with epsilon closure as inclusive can often be merged in such steps.

The phases of a compiler handle the statement 'counter = counter + interest*60' as follows: Lexical Analysis identifies tokens: 'counter', '=', 'counter', '+', 'interest', '*', '60'. Syntax Analysis checks the structure: validates 'counter = counter + interest*60' as a valid assignment statement. Semantic Analysis verifies types and meanings, ensuring 'interest' and '60' are compatible (e.g., numerical types). Intermediate Code Generation creates an abstract representation, such as 'T1 = interest * 60; counter = counter + T1;'. Code Optimization improves performance by reordering instructions, e.g., optimizing the multiply operation. Code Generation converts intermediate code to target machine code, such as assembly code. Finally, Code Linking and Loading organizes/links code for execution.

Apply Thompson's construction by first building NFAs for 'a|b', '*' (Kleene star), and concatenation with 'ba': 1. For '(a|b)', create epsilon move-based paths from a start to 'a' and 'b'. 2. For '(a|b)*', introduce looping epsilon transitions on the same states. 3. For 'ba', make a sequential transition NFA from 'b' to 'a'. 4. Concatenate to form a composite NFA ensuring cascading correct state transfers isomorphic to regex operations.

A grammar is ambiguous if a string can have multiple valid parse trees. For E -> E + E | E * E | id, 'E + E * E' could derive two different trees: 1. First parsing E + (E * E) 2. Second parsing (E + E) * E To address ambiguity, introduce precedence rules (like multiplication over addition) or transform the grammar to reflect intended association, e.g., Use E -> T + E, T -> F * T as separate non-terminals with explicit precedence.

Lexical analysis handles errors like invalid tokens, unrecognized characters, or malformed literals (e.g., incorrect strings). Syntax analysis identifies structural errors that violate grammar rules, such as missing operators, unmatched parentheses, or incorrect statement formats. These phases ensure the input adheres to language definitions up to a syntactical correctness level before semantic consideration.

You might also like