0% found this document useful (0 votes)
66 views5 pages

Mid Sem Exam - Regular-Solution

This document is a midterm exam for a compiler construction course. It contains 5 questions regarding compiler phases, lexical analysis, LL(1) parsing, and constructing parse tables. Specifically: 1. It asks the student to identify which compiler phase would perform certain tasks like semantic analysis and syntax checking. 2. It provides a regular expression that can be used to tokenize a given input string. 3. It asks the student to provide sample input to a given flex specification that would produce a certain output string. 4. It asks the student to modify a grammar to make it LL(1) parsable, and then fill out the FIRST and FOLLOW sets and parse table for the modified grammar
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)
66 views5 pages

Mid Sem Exam - Regular-Solution

This document is a midterm exam for a compiler construction course. It contains 5 questions regarding compiler phases, lexical analysis, LL(1) parsing, and constructing parse tables. Specifically: 1. It asks the student to identify which compiler phase would perform certain tasks like semantic analysis and syntax checking. 2. It provides a regular expression that can be used to tokenize a given input string. 3. It asks the student to provide sample input to a given flex specification that would produce a certain output string. 4. It asks the student to modify a grammar to make it LL(1) parsable, and then fill out the FIRST and FOLLOW sets and parse table for the modified grammar
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
You are on page 1/ 5

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI

HYDERABAD CAMPUS
SECOND SEMESTER 2019 – 2020
Compiler Construction (CS F363) MID SEM EXAM(REGULAR)
Date: 02.03.2020 Weightage:30%(60M) Duration: 90mins.
Type: Closed Book Number of questions: 3 Number of pages: 5
ID NO: NAME:
Note: 1. In all the Grammars shown consider the Upper case letters to be the Non terminal,Lower case letters
to be the terminals and the first production Non terminal be the start production.
3. ϵ denotes the empty string.

Q1. Phases of compiler and Lexical Analysis [4+4+4=12 M]


a) Identify the appropriate phase of the compiler to perform each of the following tasks:
i.A function has to be called with the correct number of arguments.

Semantic Analyzer/Semantic Analysis. One has to compare the declaration and the call of the function.
That cannot be done by the parser.

ii.Underscore "_" is allowed in the middle of identifiers, but not at the beginning or the end (i.e. "my_id"
is legal but "_id" is not).

Lexer/ Scanner. There is no reason to postpone that. It is a very “standard” thing for a scanner to do
(as one can see by the fact that it’s possible to write regular expressions capturing this condition).
Identifiers (with restrictions such as the one mentioned) are typical tokens (which are the output of a
scanner and the input of the parser).

iii.Every variable must be declared before it is used.

Semantic Analyzer/Semantic Analysis

iv.Assignment statements must end with a semicolon ";"

Parser/Syntax Analyser:

b) The input string abaabaabababbaaabaab generates the following tokens


abaa ba ababa bba aa ba a b
Write flex specification using 2 regular expressions that produces these tokens. You are allowed to only
use *,+ and ? as the metacharacters. NOTE: You need not write any actions.

Multiple answers are possible one such answer is


(ab)*a+
b*a?
c) Given the following regular expressions in flex:
%%
(01|10) printf (" first ")
1(01) *0 printf (" second ")
(1011*0|0100*1) printf (" third ")
%%
Give an input to this lexer such that the output string is (first3second)5 third2, where Ai denotes A
repeated i times.
Note: Parentheses are not part of the output, also you may use similar shorthand notation in
your answer.
((01)3 1010)5 (01001)2
i. Why is the grammar as given not LL(1)?
The grammar as specified is leftrecursive,and that interferes with the ability of a predictive
parser to do its think given just one lookahead token.
ii. Give the modified grammar after removing the empty production and also convert the
grammar into a form that is LL(1) parsable.
Note: Do not introduce any new nonterminal.
Hint: Try changing the recursion.
Due to mistake in the question marks have been allotted based the correctness of your answer
1. List -> ( Sequence )
2. Sequence -> Cell Sequence
3. Sequence -> ε
4. Cell -> List
5. Cell -> Atom
6. Atom -> num
iii.Fill the First and Follow for all nonterminals from your modified grammar in question ii).
Since the answer is based on part b based on your answer the question is graded
First Follow Marking (First+Follow)
LIST ( $,(,num,) 0.5+1.5
SEQUENCE (,num, ε ) 1+0.5
CELL (,num (,num,) 1+1
ATOM Num (,num,) 0.5+1
iv. Complete the following LL(1) parse table using the First and Follow sets computed in question iii.
( ) num $ Marking
LIST 1 1
SEQUENCE 2 3 2 1
CELL 4 5 1
ATOM 6 1
v.Show the steps while parsing the input string (10 15)
************************ That’s all folks********************************

You might also like