100% found this document useful (1 vote)
75 views11 pages

One Line Ques Ans1

The document provides definitions and explanations of key concepts in compiler construction and theoretical computer science, including: 1) A compiler is a program that translates a program written in one language into an equivalent program in another language. An interpreter translates and executes source code directly without compiling to machine code. 2) A compiler consists of phases like lexical analysis, syntax analysis, code generation, etc. The front-end depends on the source program while the back-end depends on the target machine language. 3) Lexical analysis scans the input characters and groups them into tokens. It is the first phase of a compiler and is desirable to read input from a buffer for efficiency.

Uploaded by

Nikhil Patkal
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
100% found this document useful (1 vote)
75 views11 pages

One Line Ques Ans1

The document provides definitions and explanations of key concepts in compiler construction and theoretical computer science, including: 1) A compiler is a program that translates a program written in one language into an equivalent program in another language. An interpreter translates and executes source code directly without compiling to machine code. 2) A compiler consists of phases like lexical analysis, syntax analysis, code generation, etc. The front-end depends on the source program while the back-end depends on the target machine language. 3) Lexical analysis scans the input characters and groups them into tokens. It is the first phase of a compiler and is desirable to read input from a buffer for efficiency.

Uploaded by

Nikhil Patkal
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/ 11

One Line Answers

Paper 2: Theoretical Computer Science & Compiler Construction


Chapter 1: Introduction
1. What is translator? Give one example.
It is a system program that takes as input a program written in one programming language (source) and
produces as output a program in another language (target or object). e.g. Compiler
2. Define Complier.
Complier is a program that reads a program written in one language –the source language- and translates
it into an equivalent program in another language- the target language. In this translation process, the
complier reports to its user the presence of the errors in the source program.
3. Define interpreter.
Interpreter is a language processor program that translates and executes source code directly, without
compiling it to machine code.
4. What do you mean by phase of compiler?
A phase is a logically cohesive (inter related) operation that takes as input one representation of the source
program and produces as output another representation.
5. What is front-end and back-end of the compiler?
Front-end consists of those phases that depend primarily on the source program and largely independent
of the target machine.
Back-end consists of those phases that depend on the target machine language and generally those
portions do not depend on the source language, just the intermediate language.
6. List all phases of compiler in sequence.
1. Lexical Analysis 2. Syntax Analysis
3. Semantic Analysis 4. Intermediate Code Generator
5. Code Optimizer 6. Code Generator
7. What is the main difference between phase and pass of a compiler?
A phase is a sub process of the compilation process whereas combination of one or more phases into a
module is called pass.
8. Define pass of a compiler
In an implementation of a compiler, portion of one or more phases are combined into a module called
pass. A pass reads the source program or the output of the previous pass, makes the transformation
specified by its phases and writes output into an intermediate file, which is read by subsequent pass.
9. Define syntax and semantics.
The rules and regulations used to form a language are known as syntax. The meaning given to a
programming construct is known as semantics.
10. What are the classifications of a compiler?
1. Single-pass compiler.
2. Multi-pass compiler.
3. Load and go compiler.
4. Debugging compiler.
5. Optimizing compiler.
11. What is boot strapping?
Suppose we have a new language L, which we want to make available on several machines say ‘A’ & ‘B’.
We might write for machine ‘A’ a small computer CASA that translates a subset S of language L into the
machine or assembly code of A. this compiler is written in a language that is already available on A.
We than write a compiler CSLA in the simple language S. this program when run through CASA, becomes
CALA, the compiler for complete language L, running on machine A and producing object code for A.
CASA
CSLA CALA
This process is called as bootstrapping.

©net2net 23
One Line Answers
12. Define cross-compiler.
When a compiler runs on one machine and produces object code for another machine such a compiler is
called as cross–compiler.
13. What are advantages of bootstrapping?
1. A compiler can be written in the language it compiles.
2. An optimizing compiler can optimize itself.
14. Multipass compilers are preferable over single pass compilers. Comment.
True – A multi-pass compiler can be made to use less space than a single pass compiler, since the space
occupied by the compiler program for one pass can be reused by following pass.
15. What happens during table management of bookkeeping phase of compilation?
The table management, or bookkeeping, portion of the compiler keeps track of the names used by the
program and records essential information about each, such as its type (integer, real, etc.). The data
structures used to record this information is called a symbol table.
16. Good error handling is difficult. Comment.
Good error handling is difficult because certain errors can mask subsequent errors. Other errors, if not
properly handled, can spawn an avalanche of spurious errors.
17. Define: symbol table
Information about source program (identifiers, type, size, etc.) is stored in a data structure called a symbol
table, which is passed to back end.
18. Define: single-pass compiler
In this type of compiler all phases occur during a single pass i.e. a source program is processed only once,
during the translation.
19. Define: multi-pass compiler
While translating source program into target program, we read through a representation of entire source
program multiple times is called as multi-pass compilers.
20. List the functions that compiler.
1. Translate high level language to low level language.
2. Reporting errors and warnings.
21. Which phase of compiler is also known as scanning phase?
Lexical Analyzer
22. Which phase of compiler is also known as parsing phase?
Syntax Analyzer

Chapter 2: Lexical Analysis


1. Give one major function performed by lexical analyzer.
The lexical analyzer scans the input program character by character ad groups the characters into the
lexical units called tokens.
2. Define lexeme:
The character sequence forming a token is called lexeme for the token.
3. What are tokens?
The first phase, called the lexical analyzer, or scanner, separates characters of the source language into
groups that logically belong together; these groups are called tokens. e.g. keywords, identifiers, operators
etc.
4. What do you mean by pattern?
The rules, which characterize the set of strings for a token. Pattern is expressed in terms of regular
expressions. e.g. [a-zA-Z][a-zA-Z0-9]*
5. Name two compiler construction tools
1. LEX
2. YACC
©net2net 24
One Line Answers
6. What is meant by recognizer?
A recognizer for a language L is a program that takes an input string “x” and response “yes” if “x” is
sentence of L and “no” otherwise.
7. Write state program for the token, “id-identifier”.
State-0: C=GETCHAR ();
If LETTER(C) then go to State-1
Else FAIL ();
State-1: C=GETCHAR ();
If LETTER(C) or DIGIT (C) then go to State-1
Else If DELIMITER(C) then go to State-2
Else FAIL ();
State-2: RETRACT ();
return (id, INSTRALL ());
8. “It is desirable for the lexical analyzer to read its input from an input buffer”. Give one reason
supporting the given statement.
The lexical analyzer scans the characters of the source program one at a time to discover tokens. Often,
however, many characters beyond the next token may have to be examined before the next token itself can
be determined. For this reasons, it is desirable for the lexical analyzer to read its input from an input
buffer.
9. What is preliminary scanning?
It is a process performed as characters are moved from the source file to the buffer. e.g. deleting
comments, deleting blanks, collapsing strings of several blanks to one blank, etc.
10. Draw neat labeled transition diagram for recognizing any valid identifier.

Letter Letter/Digit
0 1 2
Delimiter

11. What are the functions of lexical analyzer?


1. Its main task is to read the input characters and produce as output a sequence of tokens that the parser
uses for syntax analysis.
2. Stripping out from the source program comments and white space in the form of blank, tab, and
newline characters.
3. It also correlates error messages from the compiler with the source program.
4. Is responsible of making copy of the source program with the error messages.
5. If the source language supports macros then macro preprocessor functions also to be performed by the
lexical analyzer.
12. What is input buffering?
The lexical analyzer scans the characters of the source program one at a time to discover tokens. Often,
however, many characters beyond the next token may have to be examined before the next token itself can
be determined. For this reason, it is desirable for the lexical analyzer to read its input from an input buffer.
13. Lexical analyzer keeps the track of line number. State true or false.
True.
14. What is the O/P of a LEX program?
LEX program generates scanner written in C language (lex.yy.c)
15. State the use of function ‘retract()’.
It backtracks lookahead pointer and assigns it to input pointer.
16. State the use of function ‘install()’.
Adds symbol found into symbol table and returns pointer.
17. Give structure of LEX program.

©net2net 25
One Line Answers
declarations
%%
translation rules
%%
auxiliary functions
18. What is manifest constant?
Manifest constants are identifiers declared to stand for a constant, e.g., the name of a token.
19. State the pattern for regular expression ‘signed number’ in lex language notation.
[+-]?[0-9]+
20. Write the purpose of lex library functions-yylex ( )and yyerror( ).
yylex()- The lexical analyzer yylex() produces tokens consisting of a token name and its associated
attribute value.
yyerror() – Called by scanner or parser to display error encountered.
21. What are sentinels?
The sentinel is a special character that cannot be part of the source program, and a natural choice is the
character eof.
22. How conflict can be resolved in Lex
When several prefixes of the input match one or more patterns:
1. Always prefer a longer prefix to a shorter prefix.
2. If the longest possible prefix matches two or more patterns, prefer the pattern listed first in the Lex
program.
23. Explain the advantage of a lexical analyzer generator.
Advantage of lexical analyzer generator is that human efforts are reduced. Otherwise using handcoding
technique we are only suppose to construct NFA for different regular expressions, convert it to DFA,
minimize it and then convert it into code. This is happening within fraction of seconds using scanner
generator.
24. State the pattern for regular expression “set of a’s and b’s of length 2”, in lex language notation .
[ab]{2}
25. State the pattern for regular expression “any character between ‘P’ and ‘S’ irrespective of its case”,
in lex language notation.
[P-Sp-s]

Chapter 3: Parser
1. What is parser?
A parser for grammar G is a program that takes as input a string w and produces as output either a parse
tree for w, if w is a sentence of G, or an error message indicating that w is not a sentence of G.
2. What are the two functions of parser?
1. Checks whether the tokens appearing in its input, which is the output of the lexical analyzer, occurs in
patterns that are permitted by the specification for the source language (grammar).
2. Imposes a tree like structure that is used by the subsequent phases of the compiler.
3. Define handle.
While reduction we find substring in the sentential form which matches RHS of some production. Such
substring is called handle.
4. What is handle pruning?
The process of discovering a handle in the sentential form & reducing it to the appropriate left-hand side
is called handle pruning.
5. What are the demerits of SLR?
1. It will not produce uniquely defined parsing action tables for all grammars.

©net2net 26
One Line Answers
2. Shift-Reduce conflict.
6. Why LR parsing is good and attractive?
1. LR parsers can be constructed for all programming language constructs for which CFG can be
written.
2. LR parser can detect syntactic error as soon as possible, when left-to-right scan of the input.
7. How will you change the given grammar to an augmented grammar?
If ‘G’ is a grammar with start symbol ‘S’ then GWis the augmented grammar for G, such that GWis G
with a new start symbol SWand with production SW→S.
8. Explain the need of augmentation.
To indicate to the parser that, when it should stop parsing and when to announce acceptance of the input.
Acceptance occurs when the parser is about to reduce by SW→S.
9. Let I is a set of LR(O) items, and X is a grammar symbol. Define GOTO(I, X)
GOTO(I,X)={CLOSURE({AαX.β})|Aα.Xβ is in I}
10. Construct LR(O) item for A→ →∈
LR(0) item for A→∈ is A→•
11. What do LL and LR stand for?
LL – First L indicates leftmost derivation and second L indicates input scanned from left to right.
LR – Input scanned from left to right and generates rightmost derivation in reverse.
12. Construct LR(0) items for following production: A XYZ.
AXYZ yields four items
A•XYZ
AX•YZ
AXY•Z
AXYZ•
13. Explain the function CLOSURE(I).
If I is a set of items for a grammar G, then the set of items CLOSURE(I) is constructed from I by the
rules.
1. Every item in I is in CLOSURE (I)
2. If Aα.Bβ is in CLOSURE (I) and Bγ is a production, add the item B.γ to I, if it isn’t already
there.
14. Define CLOSURE(I) operation in LR parsers.
For LR(1) item I, CLOSURE (I) is constructed as follows:
1. Everything in I is in CLOSURE(I)
2. If [Aα.Bβ,a] is in I and Bγ is a production then add β.γ,b] to CLOSURE (I) for every terminal
‘b’ in FIRST(βa)
15. Which type of conflict is not possible in LR parser?
Shift-Shift conflict is not possible in LR parser.
16. Which kind of parser is most common in compilers?
LALR(Look Ahead Left Right) parser is most common in compilers.
17. Recursive decent parser is bottom up type parser. State True or False.
False
18. What is left recursive grammar?
A grammar G is said to be left-recursive if it has a non-terminal A such that there is a derivation A→Aα
for some α
19. What is left factoring?
If A→αβ|αγ are two A-productions, and the input begins with a non-empty string derived from α, we do
not know whether to expand A to αβ or αγ. Left factoring the above grammar we get following
productions:

©net2net 27
One Line Answers
A→αA’
A’→β|γ
20. Left recursive grammars are not suitable for top down parsing. State true or false.
True.
21. List actions taken by shift reduce parser.
1. Shift
2. Reduce
3. Accept
4. Error
22. Define viable prefixes.
The set of prefixes of right sentential forms that can appear on the stack of a shift reduce parser are called
viable prefixes.
23. List down the conflicts during shift-reduce parsing.
1. Shift/reduce conflict: Parser cannot decide whether to shift or reduce.
2. Reduce/reduce conflict: Parser cannot decide which of the several reductions to make.
24. Differentiate Kernel and non-Kernel items.
Kernel items include the initial item, S’.S and all items whose dots are not at the left end. Whereas the
non-kernel items have their dots at the left end.
25. What are the components of LR parser?
1. An input.
2. An output.
3. A stack.
4. A driver program.
5. A parsing table.
26. List the different techniques to construct an LR parsing table?
1. Simple LR(SLR).
2. Canonical LR.
3. Lookahead LR (LALR).
27. YACC is a parser. State true or false.
False
28. What is the syntax for YACC source specification program?
Declarations
%%
Translation rules
%%
Supporting C-routines
29. How YACC resolves parsing action conflicts?
A reduce/reduce conflict is resolved by choosing the conflicting production listed first in the YACC
specification. A shift/reduce conflict is resolved by shifting action or introduce associativity and
precedence.
30. What are the actions performed by a shift reduce parser?
1. Shift
2. Reduce
3. Accept
4. Error
31. What is a recursive descent parser?

©net2net 28
PAPER –II: THEORETICAL COMPUTER SCIENCE AND COMPILER CONSTRUCTION –II

A parser that uses a set of recursive procedures to recognize its input with no backtracking is called a
recursive-descent parser.
32. Why is the precedence and associativity required in the declaration section of a yacc program?
A shift reduce conflicts can be removed by giving precedence and associativity in the declaration section
of a yacc program?
33. What is an operator grammar?
The grammar not having ∈-production and right-side of each production is not having two adjacent non-
terminals.
e.g. SS+S|S*S|id
34. State the relation between number of states in SLR and LALR parser.
Total number of states in SLR are approximately equal to LALR parser.
35. Do left factoring in the following grammar.
AaBcC | aBb | aB | a
BW
CW
After left factoring
AaBA’|a
A’cC|b|W
BW
CW
36. What is conflict? Which type of conflict cannot occur in LR parsers?
If there are multiple entries in LR parsing table then if say conflict has occur. Reduce/reduce can never
occur in LR parsers.
37. State the meaning of ‘UMINUS’ in yacc.
UMINUS represents unary minus. If used as %prec UMINUS then it indicated give precedence of unary
minus.

Chapter 4: Syntax Directed Translation


1. What is syntax directed definition.
It is a generalization of a CFG in which each grammar symbol has an associated set of attributes like,
synthesized attribute and inherited attribute.

2. What is syntax directed translation.


A syntax-directed translation scheme embeds program fragments called semantic actions within
production bodies.

3. Compare SDD and SDT


SDD SDT
SDDs can be more readable SD translation scheme can be more efficient
More useful for specifications. More useful for implementations.
4. How the value of synthesized attribute is computed?
It is computed from the values of attributes at the children of that node in the parse tree.
5. How the value of inherited attribute is computed?

©net2net 29
PAPER –II: THEORETICAL COMPUTER SCIENCE AND COMPILER CONSTRUCTION –II

It is computed from the value of attributes at the siblings and parent of that node.
6. Define annotated parse tree.
A parse tree showing the values of attributes at each node is called an annotated parse tree.
7. What do you mean by a syntax tree?
Syntax tree is a variant of a parse tree in which each leaf represents an operand and each interior node
represents an operator.
8. When can semantic analysis be done?
Semantic analysis can be done during syntactic analysis or sometimes it is done at the final code
generation phase.
9. Mention the role of semantic analysis?
1. Semantic analysis checks the source program for semantic errors and gathers information for the
subsequent code-generation phase. It uses hierarchical structure to identify the operators and
operands of expressions and statements.
2. An important component of semantic analysis is type checking .In type checking the compiler
checks that each operator has operands that are permitted by the source language specification. In
such cases, certain programming language supports operand coercion or type coercion also.
10. What is dependency graph?
A dependency graph (DG) depicts the flow of information among the attribute instances in a particular
parse tree; an edge from one attribute instance to another means that the value of the first is needed to
compute the second.
11. What are two important classes of SDDs
1. The “S-attributed” and
2. The more general “L-attributed” SDD’s.
12. What is attribute grammar?
An SDD without side effect is sometimes called an attribute grammar. The rules in an attribute grammar
define the value of an attribute purely in terms of the values of other attributes and constants.
13. What is postfix SDT?
SDT’s with all actions at the right ends of the production bodies are called postfix SDT’s.
14. Give applications of SDT?
1. Construction syntax tree
2. Generating type

Chapter 5: Code Generation and Optimization


1. State the need of intermediate code.
1. A compiler for a different machine can be created by attaching a back end for the new machine to
an existing front end.
2. A machine-independent code optimize can be applied to the intermediate representation.
2. Define DAG
DAG is a directed acylic graph with the following labels on nodes:
1. Leaves are labeled by unique identifiers, either variable names or constants.
2. Interior nodes are labeled by an operator symbol.
3. Nodes are also given an extra set of identifiers for label.
3. What is value number for node?
In array representation of DAG we refer to nodes by giving the index of the record. This integer is called
value number for the node.
4. List the issues in code generation
1. Input to the code generator
2. Target programs

©net2net 30
PAPER –II: THEORETICAL COMPUTER SCIENCE AND COMPILER CONSTRUCTION –II

3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
5. Define basic block.
A basic block contains sequence of consecutive statements, which may be entered only at the beginning
and when it is entered it is executed in sequence without halt or possibility of branch.
6. Define flow graph.
Relationships between basic blocks are represented by a directed graph called flow graph.
7. What is code optimization? Explain local optimization.
• It is a program transformation technique applied on intermediate code so that made the code
produced runs faster or takes less space.
• Local optimization applied over small segments of a program consisting of a few source statements.
8. What are the rules to find “leader” in basic block?
- The first statement in a basic block is a leader.
- Any statement which is the target of a conditional or unconditional goto is a leader.
- Any statement which immediately follows a conditional goto is a leader.
9. State some application of DAG
1. eliminating local common sub-expressions
2. eliminating dead code
3. reordering statements that do not depend on one another
4. applying algebraic laws to reorder operands of three-address instructions
10. What are dominators?
We say node d of a flow graph dominates node n, written d dom n, if every path from the entry node of
the flow graph to n goes through d. Note that under this definition, every node dominates itself.
11. What is a dominator tree?
A useful way of presenting dominator information is in a tree, called the dominator tree, in which the
entry node is the root, and each node d dominates only its descendants in the tree.
12. What is the use of dominator?
Dominators are used to detect the loops in a program, during data float analysis.
13. What is the need of ‘code optimization’?
1. To eliminate redundancies in program.
2. Computations in a program are rearranged or rewritten so to execution efficiency is improved,
which results in faster running machine code.
14. List different code optimization techniques.
1. Compile time evaluation
2. Common sub-expression elimination
3. Frequency reduction
4. Dead code elimination
5. Strength reduction.
15. What do you mean by ‘loop invariant’ computation?
This transformation takes on expression that yields the same result independent of the number of times a
loop is executed and places the expression before the loop.
16. What is ‘strength reduction’?
The strength reduction optimization replaces the occurrence of a time consuming operation by an
occurrence of a faster operation.
e.g. X = Y*2; can be replace as X = Y + Y;

©net2net 31
PAPER –II: THEORETICAL COMPUTER SCIENCE AND COMPILER CONSTRUCTION –II

17. Name some forms of intermediate code


1. Postfix notation or polish notation.
2. Syntax tree
3. Three address code (Quadruple, Triple and Indirect Triple)
18. Write the syntax for three-address code statement, and mention its properties.
Syntax: A= B op C
Three-address instruction has at most one operator in addition to the assignment symbol. The compiler
has to decide the order in which operations are to be done. The compiler must generate a temporary
name to hold the value computed by each instruction. Some three-address instructions can have less than
three operands.
19. State the advantages of ‘intermediate code’.
1. Retargeting – build a compiler for new machine quickly.
2. Optimization – reuse intermediate code optimizers in compilers for different languages and different
machines.
20. What is meant by constant folding?
Constant folding is the process of replacing expressions by their value if the value can be computed at
compile time.
21. List the criteria for selecting a good code optimization technique.
1. It should capture most of the potential improvement without an unreasonable amount of effort.
2. It should preserve the meaning of the program
3. It should reduce the time or space taken by the object program.
22. Define code generation.
The code generation is the final phase of the compiler. It takes an intermediate representation of the
source program as the input and produces an equivalent target program as the output.
23. Give an example of 3-address code statement.
A/B*C
T1 := A/B
T2 := T1*C
T1 and T2 are names of temporary variables.
24. What are the different types of three address statements?
1. Assignment statements of the form x=y op z.
2. Assignment statements of the form x= op y.
3. Copy statements of the form x=y.
4. An unconditional jump, goto L.
5. Conditional jumps, if x relop y goto L.
6. Indexed assignments of the form, x=y[i] , x[i]=y.

Compare SLR, LR and LALR parsers


No. SLR(1) LR(1) LALR(1)
1. Set of canonical collection of Set of LR(1) items are Set of LR(1) items are
LR(0) items are generated. generated. generated.
2. Number of items of SLR are less Number of items generated LALR(1) is a merger of LR(1).
than LR(1) but approximately are more than SLR(1) and Therefore number of items are
equal to LALR(1) LALR(1) less than LR(1)
3. To construct parsing table To construct parsing table To construct parsing table
FOLLOW function is used. FIRST function is used. FIRST function is used.

©net2net 32
PAPER –II: THEORETICAL COMPUTER SCIENCE AND COMPILER CONSTRUCTION –II

4. Shift-Reduce conflict can occur in Rarely shift-reduce conflict Reduce-reduce conflict can
SLR can occur. occur in LALR
5. Good method Better method Most powerful (best) method.
6. Memory requirement is less. Memory requirement is Memory requirement is less.
large.

Compare RDP and predictive parsers


No. RDP Predictive Parser
1. Uses recursive procedures for parsing Uses predictive parsing table for parsing
2. It is recursive form of top-down parser It is non-recursive form of top-down parser.
3. Requires more memory since it is recursive Requires less memory since it is non-recursive
4. Error is indicated by calling error handling Error is indicated with the help of parsing table.
function
5. FIRST and FOLLOW functions are not FIRST and FOLLOW functions are required.
required.
6. Can be implemented only in languages those Can be implemented in all languages those have
supports recursion. array data structure.

Compare top-down and bottom-up parsing


No. Top-down parsing Bottom-up parsing
1. Uses derivation Uses reduction
2. Starts building parse tree from root and proceeds Starts building parse tree from leafs and
towards leafs proceeds towards root.
3. Left-recursion and backtracking are problems No left-recursion and backtracking problems
occurring in this parsing
4. Ambiguous grammars are not suitable for top-down It accepts ambiguous grammars
parsing
5. Top-down parsers are: Bottom-up parsers are:
1. Recursive descent parser 1. Operator precedence parser.
2. Predictive parser 2. LR parsers (SLR, CLR, LALR)
6 Precise error indication is not possible Precise indication is possible.

©net2net 33

You might also like