CD Assignment Question Bank
CD Assignment Question Bank
This strategy is used by most parsing methods. In this method of discovering the error, the
parser discards input symbols one at a time. This process is continued until one of the
designated sets of synchronizing tokens is found.
In this strategy, on discovering an error, parser performs local correction on the remaining
input. It can replace a prefix of the remaining input with some string.
Error Production:
It requires good knowledge of common errors that might get encountered, then we can augment
the grammar for the corresponding language with error productions that generate the
erroneous constructs.
Global Correction:
We often want such a compiler that makes very few changes in processing an incorrect input
string to the correct input string
Symbol Table:
In semantic errors, errors are recovered by using a symbol table for the corresponding identifier
and if data types of two operands are not compatible, automatically type conversion is done by
the compiler.
i. Artificial Intelligence: Compilers are used in the field of artificial intelligence (AI) to
optimize and generate code for deep learning models, computer vision, natural
language processing, and other AI applications. AI compilers can optimize code for
specific hardware architectures and can generate highly efficient code for AI
workloads.
ii. Gaming: Game development often involves the use of compilers to generate code that
runs on game consoles and PCs. Gaming compilers are optimized for performance,
allowing game developers to create immersive, high-performance games.
iii. Security: Compilers are used in security applications to create code that is resistant to
various forms of attacks, including buffer overflows, code injections, and other
security vulnerabilities. Security compilers can generate code that is highly resistant
to reverse engineering and tampering.
iv. Embedded Systems: Embedded systems are computer systems that are designed to
perform specific functions in various devices such as automobiles, medical
equipment, and consumer electronics. Compilers are used to generate machine
code that runs on these devices, ensuring efficient use of resources and optimal
performance.
Assignment No 2
2. This process begins at the top level of the grammar, where the parser identifies the non-
terminal symbol that corresponds to the starting rule of the language.
3. The parser then calls the corresponding parsing function for that non-terminal symbol, which
recursively calls parsing functions for each of its sub-rules.
4. During this process, the parser compares the current input token to the expected token for the
current rule, using lookahead to determine which rule to follow next.
5. If the current input token matches the expected token, the parser moves on to the next token
and continues the parsing process.
6. If the input token does not match the expected token, the parser generates a syntax error and
stops processing the input text.
7. As the parser descends through the grammar rules, it builds a parse tree that represents the
structure of the input text according to the language’s grammar rules.
8. This parse tree can then be used to further analyze and process the input text, such as by
generating code or performing semantic analysis.
9. Overall, the RDP method provides a simple and efficient way to analyze and process the
syntax of a language.
10. By recursively descending through the grammar rules, the parser can efficiently handle a
wide variety of context-free grammar, making it a popular choice for parsing algorithms in
programming language design.
2. Explain non recursive predictive parsing with example
Ans.
1. A non-recursive predictive parser can be built by maintaining a stack a stack explicitly,
rather than implicitly via recursive calls. The parser mimics a leftmost derivation. If w is
the input that has been matched so far, then the stack holds a sequence of grammar
symbols α such that,
2. S ⇒∗ wα
3. The parser we are going to use is called table driven parser with following
arrangements:
• It has an input buffer that contains the string to be parsed with $ as the end marker
symbol,
• a stack containing a sequence of grammar symbols, and
• a parsing table.
4. The output is parse-tree. The bottom of the stack also holds the end marker symbol $.
Initially the symbol on top of $ symbol in stack is start symbol of the grammar.
5. The non-recursive predictive parser constructs a top-down parse-tree.
6. The parser is con trolled by a program that read X, the symbol on top of the stack, and a
– the current input symbol. If X is non-terminal, the parser chooses an X-production by
consulting entry M[X, a] in the parsing table M. In addition, a code be executed here,
say, the code for constructing a node for a parse-tree. If X is a terminal symbol, then it
checks for a match between the terminal symbol X and current symbol input a. if
matched, the terminal is popped from stack, input pointer is advanced to next symbol,
and the process repeats for next symbol on the stack. In case X is terminal but not
matching with input symbol, then it is case of error. The behaviour of the parser can be
described in terms of configurations, which give the stack contents and the remaining
input.
8. Advantages :
11. Disadvantages:
Assignment No 3
1.Explain backpatching in detail
Ans.
1. Backpatching is basically a process of fulfilling unspecified information. This
information is of labels.
2. It basically uses the appropriate semantic actions during the process of code
generation. It may indicate the address of the Label in goto statements while
producing TACs for the given expressions.
3. Here basically two passes are used because assigning the positions of these label
statements in one pass is quite challenging.
4. It can leave these addresses unidentified in the first pass and then populate them
in the second round.
5. Backpatching is the process of filling up gaps in incomplete transformations and
information.
6. Backpatching is mainly used for two purposes:
a. Boolean expression:
Boolean expressions are statements whose results can be either true or
false.
b. Flow of control statements:
The flow of control statements needs to be controlled during the execution
of statements in a program.
7. Applications of Backpatching:
i. Backpatching is used to translate flow-of-control statements in one pass
itself.
ii. Backpatching is used for producing quadruples for boolean expressions
during bottom-up parsing.
iii. It is the activity of filling up unspecified information of labels during the
code generation process.
iv. It helps to resolve forward branches that have been planted in the code.
The production must have non-terminal as The production must have non-terminal as a
2. its head. symbol in its body.
It can be evaluated during a single bottom- It can be evaluated during a single top-down
4. up traversal of parse tree. and sideways traversal of parse tree.
7.
Assignment No 4
Basic Block
Basic block contains a sequence of statement. The flow of control enters at the
beginning of the statement and leave at the end without any halt (except may be the last
instruction of the block).
The following sequence of three address statements forms a basic block:
Output: it contains a list of basic blocks with each three address statement in exactly one
block
Method: First identify the leader in the code. The rules for finding leaders are as follows:
For each leader, its basic block consists of the leader and all statement up to. It doesn't
include the next leader or end of the program.
Consider the following source code for dot product of two vectors a and b of length 10:
1. begin
2. prod :=0;
3. i:=1;
4. do begin
5. prod :=prod+ a[i] * b[i];
6. i :=i+1;
7. end
8. while i <= 10
9. end
When an object is created, its mark bit is set to 0(false). In the Mark phase, we set the marked
bit for all the reachable objects (or the objects which a user can refer to) to 1(true). Now to
perform this operation we simply need to do a graph traversal, a depth-first search
approach would work for us. Here we can consider every object as a node and then all the
nodes (objects) that are reachable from this node (object) are visited and it goes on till we have
visited all the reachable nodes
Algorithm: Mark phase
Mark(root)
If markedBit(root) = false then
markedBit(root) = true
For each v referenced by root
Mark(v)
Assignment No 5
Code generator is used to produce the target code for three-address statements. It uses
registers to store the operands of the three address statement.
Example:
Consider the three address statement x:= y + z. It can have the following sequence of
codes:
MOV x, R0
ADD y, R0
The algorithm takes a sequence of three-address statements as input. For each three
address statement of the form a:= b op c perform the various actions. These are as
follows:
1. Invoke a function getreg to find out the location L where the result of
computation b op c should be stored.
2. Consult the address description for y to determine y'. If the value of y currently in
memory and register both then prefer the register y' . If the value of y is not
already in L then generate the instruction MOV y' , L to place a copy of y in L.
3. Generate the instruction OP z' , L where z' is used to show the current location of
z. if z is in both then prefer a register to a memory location. Update the address
descriptor of x to indicate that x is in location L. If x is in L then update its
descriptor and remove x from all other descriptor.
4. If the current value of y or z have no next uses or not live on exit from the block
or in register then alter the register descriptor to indicate that after execution of x
: = y op z those register will no longer contain y or z.