All Exams  >   Computer Science Engineering (CSE)  >   Compiler Design  >   All Questions

All questions of Parsing for Computer Science Engineering (CSE) Exam

An LR-parser can detect a syntactic error as soon as
  • a)
    The parsing starts
  • b)
    It is possible to do so a left-to-right scan of the input.
  • c)
    It is possible to do so a right-to-left scan of the input.
  • d)
    Parsing ends
Correct answer is option 'C'. Can you explain this answer?

Ipsita Patel answered
LR-parser and detecting syntactic errors

LR-parser: An LR-parser is a type of bottom-up parser that reads input text from left to right and constructs a rightmost derivation of the input. It uses a stack to keep track of previously seen symbols and a parsing table to determine the next action based on the current state and the next input symbol.

Syntactic error: A syntactic error occurs when the input text does not conform to the grammar of the language being parsed. For example, a missing semicolon or a mismatched parenthesis can cause a syntactic error.

Detection of syntactic error: An LR-parser can detect a syntactic error as soon as it is possible to do so with a right-to-left scan of the input. This is because LR-parsers are bottom-up parsers that attempt to construct a rightmost derivation of the input. If the parser encounters an input symbol that cannot be reduced to a valid production in the grammar, it can backtrack and try a different production. If all possible productions fail, the parser reports a syntax error.

Left-to-right scan: While it is possible to detect some syntactic errors with a left-to-right scan of the input, it is not always sufficient. This is because LR-parsers attempt to construct a rightmost derivation of the input, which means they need to look ahead to determine the correct action to take. For example, consider the input "a + b * c". A left-to-right scan would detect the missing semicolon at the end of the input, but it would not detect the fact that the expression is ambiguous without additional parsing information. An LR-parser, on the other hand, can use its parsing table to determine the correct parse tree for the input.

Conclusion: In conclusion, an LR-parser can detect syntactic errors as soon as it is possible to do so with a right-to-left scan of the input. This is because LR-parsers attempt to construct a rightmost derivation of the input and need to look ahead to determine the correct action to take. While it is possible to detect some syntactic errors with a left-to-right scan, it is not always sufficient for LR-parsers.

Can you explain the answer of this question below:

How many strings of length less than 4 contains the language described by the regular expression (x+y)*y(a+ab)*?

  • A:

    7

  • B:

    10

  • C:

    12

  • D:

    11

The answer is d.

Baishali Bajaj answered
The answer is 12.
String of length 1 = y
String of length 2 = xy,yy,ya
String of length 3 = xxy,xyy,yxy,yyy,yab,yaa,xya,yya
total = 1 + 3 + 8 = 12

Given the following expression grammar:
E -> E * F | F + E | F
F -> F - F | id
which of the following is true?
  • a)
    * has higher precedence than +
  • b)
    – has higher precedence than *
  • c)
    + and — have same precedence
  • d)
    + has higher precedence than *
Correct answer is option 'B'. Can you explain this answer?

Ravi Singh answered
-Precedence is defined as the thing that comes first, has the highest ranking or has a higher priority because of superiority. 
-An example of precedence is the priority of a resume with a recommendation from the CEO.
-
Explanation: Let say i/p is 3*4-5 when we draw parse tree according to grammar

      E
   /  |  \
  E   *   F
  |     / | \
  F    F  -  F
  |    |     |
id(3) id(4) id(5)
As we can see first ‘- ‘ will be evaluated then ‘ * ‘ is evaluated so ‘ – ‘ has higher precedence then *.

So correct choice is B

Grammar that produce more than one Parse tree for same sentence is:
  • a)
    Ambiguous
  • b)
    Unambiguous
  • c)
    Complementation
  • d)
    Concatenation Intersection
Correct answer is option 'A'. Can you explain this answer?

Rohan Shah answered
Understanding Ambiguous Grammars
Ambiguous grammars are an essential concept in the study of formal languages and parsing in computer science. When a grammar produces more than one parse tree for the same sentence, it indicates that the grammar is ambiguous.
Key Characteristics of Ambiguous Grammars:
- Multiple Interpretations: An ambiguous grammar allows a single sentence to be interpreted in multiple ways. For example, the sentence "The chicken is ready to eat" can mean either the chicken is cooked and ready to be consumed or that the chicken is ready to consume something else.
- Parse Trees: A parse tree is a tree representation of the syntactic structure of a sentence according to a grammar. In an ambiguous grammar, a single sentence will yield two or more distinct parse trees, highlighting different interpretations or structures.
- Challenges in Parsing: Ambiguity poses challenges in parsing, as it can lead to confusion in understanding the intended meaning of a sentence. This is particularly critical in programming languages and compilers, where precise syntax is required.
Examples of Ambiguity:
- Natural Language: Ambiguities are common in natural languages, where context often resolves the intended meaning.
- Programming Languages: An example in programming could involve operators, where expressions like "a + b * c" can be parsed differently based on operator precedence.
Conclusion:
The presence of multiple parse trees for a single sentence signifies an ambiguous grammar. Understanding and resolving these ambiguities is fundamental in both natural language processing and compiler design, ensuring clear and accurate interpretation of sentences.

 YACC builds up
  • a)
    SLR parsing table
  • b)
    Canonical LR parsing table
  • c)
    LALR parsing table
  • d)
    None of the mentioned
Correct answer is option 'C'. Can you explain this answer?



YACC and Parsing Tables

Parsing tables are used in compiler construction to facilitate the process of syntax analysis. YACC (Yet Another Compiler Compiler) is a tool used to generate parsers for programming languages.

Types of Parsing Tables:

SLR Parsing Table:
- YACC is capable of building SLR parsing tables.
- SLR (Simple LR) parsing is a type of bottom-up parsing method that uses a lookahead symbol to make parsing decisions.

Canonical LR Parsing Table:
- YACC does not directly build Canonical LR parsing tables.
- Canonical LR parsing is a more powerful parsing method than SLR parsing, but it requires more memory and time to construct the parsing table.

LALR Parsing Table:
- YACC builds LALR (Look-Ahead LR) parsing tables.
- LALR parsing is a compromise between the power of Canonical LR parsing and the efficiency of SLR parsing. It uses a smaller parsing table while still being able to parse a wider range of grammars.

Conclusion:
YACC is a versatile tool that can generate parsing tables for different parsing methods. While it may not directly generate Canonical LR parsing tables, it is still capable of building SLR and LALR parsing tables, which are widely used in compiler construction.

An optimizer Compiler
  • a)
    Is optimized to occupy less space
  • b)
    Both of the mentioned
  • c)
    Optimize the code
  • d)
    None of the mentioned
Correct answer is option 'D'. Can you explain this answer?

Explanation: In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program.

The language accepted by a Push down Automata:
  • a)
    Type0
  • b)
    Type1
  • c)
    Type2
  • d)
    Type3
Correct answer is option 'C'. Can you explain this answer?

Nilesh Chavan answered
Push Down Automata (PDA) Language
Push Down Automata (PDA) is a type of automaton that can recognize context-free languages. It is an extension of a Finite Automaton with a stack to help in the recognition of context-free languages. The language accepted by a PDA belongs to Type 2 in the Chomsky hierarchy of formal languages.

Chomsky Hierarchy
The Chomsky hierarchy classifies formal languages into four types based on their generative power. These types are Type 0, Type 1, Type 2, and Type 3, with Type 0 being the most powerful and Type 3 being the least powerful.

Type 2 - Context-Free Languages
Context-Free Languages, which are accepted by Push Down Automata, belong to Type 2 in the Chomsky hierarchy. These languages can be generated by context-free grammars and can be recognized by push down automata.

Characteristics of Type 2 Languages
- Context-Free Languages have a context-free grammar that consists of production rules with a single non-terminal on the left-hand side.
- They can be recognized by a push down automaton, which has a stack to store and retrieve symbols.
- Context-Free Languages are more powerful than Regular Languages (Type 3) but less powerful than Context-Sensitive Languages (Type 1).

Conclusion
In conclusion, the language accepted by a Push Down Automata belongs to Type 2 in the Chomsky hierarchy of formal languages. Context-Free Languages, which are recognized by PDAs, are characterized by context-free grammars and can be generated by push down automata.

 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 to parse a string with n tokens?
  • a)
    n/2
  • b)
    n-1
  • c)
    2n-1
  • d)
    2^n
Correct answer is option 'B'. Can you explain this answer?

Aryan Saha answered
Explanation:

To determine the maximum number of reduce moves that can be taken by a bottom-up parser, we need to analyze the given grammar and string. Let's break down the solution into steps:

Step 1: Analyze the grammar
The grammar is given to have no epsilon- and unit-production. This means that there are no productions of the form A -> ε or A -> B, where A, B are non-terminals. This is an important condition to ensure that the number of reduce moves is maximized.

Step 2: Analyze the string
The string is given to have n tokens. Tokens are the basic units of the input string and can be terminals or non-terminals.

Step 3: Determine the maximum number of reduce moves
In a bottom-up parser, reduce moves are applied when a production rule is matched in reverse to replace a sequence of tokens with a non-terminal. To maximize the number of reduce moves, we want to replace as many tokens as possible with non-terminals.

Since the grammar has no epsilon- and unit-production, each reduce move will replace at least two tokens with a non-terminal. Therefore, the maximum number of reduce moves can be achieved by replacing every two adjacent tokens with a non-terminal until only one non-terminal remains.

For example, consider the string "a b c d e", where each letter represents a token. The maximum number of reduce moves can be achieved by replacing "a b", "c d", and "e" with non-terminals, resulting in "A A A". This requires n-1 reduce moves.

Therefore, the maximum number of reduce moves for a string with n tokens is n-1.

Conclusion:
The correct answer is option B: n-1.

Which of the following statements is false?
  • a)
    Left as well as right most derivations can be in Unambiguous grammar
  • b)
    An LL (1) parser is a top-down parser
  • c)
    LALR is more powerful than SLR
  • d)
    Ambiguous grammar can’t be LR (k)
Correct answer is option 'A'. Can you explain this answer?

Ujwal Roy answered
Explanation: If a grammar has more than one leftmost (or rightmost) derivation the grammar is ambiguous. Sometimes in unambiguous grammar the rightmost derivation and leftmost derivations may differ.

A simple two-pass assembler does which of the following in the first pass.
  • a)
    It allocates space for the literals
  • b)
    Calculates total length of the program
  • c)
    Symbol table is built for the symbols and their value
  • d)
    All of the mentioned
Correct answer is option 'D'. Can you explain this answer?

Dipika Chavan answered
Understanding the Two-Pass Assembler
A two-pass assembler is a program that translates assembly language code into machine code. Its operation is divided into two main phases, each with distinct tasks. The first pass is crucial for setting up the necessary structure for the assembly process.

Key Functions of the First Pass
- Symbol Table Construction:
During the first pass, the assembler scans the entire source code to identify all the symbols (labels, variables, etc.). It builds a symbol table that records each symbol and its corresponding address or value.
- Literal Space Allocation:
The assembler identifies literals (constant values) used in the program and allocates space for them. This ensures that when the program is executed, there is a predefined area in memory for these constants.
- Program Length Calculation:
The assembler computes the total length of the program, which is essential for memory allocation. This calculation helps in understanding how much memory will be required for the entire code segment.

Conclusion: All Functions are Executed
In the first pass, all of the above functions are performed simultaneously. Therefore, the correct answer is option 'D': **All of the mentioned**. The assembler needs to gather all necessary information to facilitate the translation process in the second pass, where actual machine code is generated based on the data collected in the first pass.
In summary, the first pass of a two-pass assembler is a foundational step that prepares the assembler to effectively translate assembly language into machine code in the subsequent pass.

A system program that set-up an executable program in main memory ready for execution is
  • a)
    Assembler
  • b)
    Linker
  • c)
    Loader
  • d)
    Text editor
Correct answer is option 'C'. Can you explain this answer?

A loader is the part of an operating system that is responsible for loading programs and libraries. It is important that with the starting of a program, as it places programs into memory and executes it.

A grammar for a programming language is a formal description of
  • a)
    Syntax
  • b)
    Semantics
  • c)
    Structure
  • d)
    Library
Correct answer is option 'C'. Can you explain this answer?

Gargi Sarkar answered
Understanding Programming Language Grammar
In programming languages, grammar serves as a framework that defines how code is structured. Let’s explore why the correct answer is option 'C'.
1. Definition of Grammar
- A grammar specifies the set of rules that govern the formation of valid statements and expressions in a programming language. It is crucial for parsing and interpreting code.
2. Syntax vs. Semantics
- Syntax refers to the arrangement of symbols and words. It dictates how code should be written but does not define what the code means.
- Semantics, on the other hand, deals with the meaning behind the syntactic structures. It explains what a given syntax does when executed.
3. Structured Nature of Grammar
- Grammar is inherently structured. It organizes the syntax rules into clear categories such as expressions, statements, and declarations. This structure allows for the efficient parsing of code by compilers and interpreters.
4. Importance of Structure in Programming
- A well-defined grammar enables developers to write code that follows a consistent format, improving readability and maintainability.
- It also facilitates error detection by compilers, as any deviation from the defined grammar can be flagged as a syntax error.
5. Conclusion
- The answer to the question is option 'C' because grammar provides the necessary structure for defining the syntax of a programming language, distinguishing it from semantics and other concepts. Understanding this structure is essential for both language design and implementation.
By recognizing the structured nature of grammar, one can appreciate its vital role in programming languages, ensuring that they are not only functional but also coherent.

Given the following expression grammar:E -> E * F | F + E | FF -> F – F | idWhich of the following is true?
  • a)
    * has higher precedence than +
  • b)
    – has higher precedence than *
  • c)
    + and — have same precedence
  • d)
    + has higher precedence than *
Correct answer is option 'B'. Can you explain this answer?

Dishani Shah answered
E - T | E + T | T
T - F | T * F | F
F - ( E ) | id
where id is a token representing an identifier.

This grammar describes arithmetic expressions consisting of addition, multiplication, and parentheses, with identifiers as operands.

Here is an example parse tree for the expression "a + b * (c + d)":

E
/ \
E T
/ \ \
T + F
/ \ \
F T E
/ / \
F T F
| |
F F
|
id

In this parse tree, the non-terminal symbols are represented by nodes, and the terminal symbols are represented by leaves. The tree is built from the top down, starting with the start symbol E and expanding each non-terminal symbol according to the production rules of the grammar. The leaves of the tree represent the tokens in the input expression.

An intermediate code form is
  • a)
    Postfix notation
  • b)
    Syntax Trees
  • c)
    Three Address code
  • d)
    All of the mentioned
Correct answer is option 'D'. Can you explain this answer?

Rohan Patel answered
Understanding Intermediate Code Forms
Intermediate code forms are crucial in the compilation process as they bridge the gap between high-level source code and low-level machine code. They allow for optimization and easier translation to various machine architectures.
Types of Intermediate Code Forms
  • Postfix Notation:
    - Also known as Reverse Polish Notation (RPN).
    - Operators follow their operands, which makes it easier for stack-based evaluation.
  • Syntax Trees:
    - A tree representation where each node corresponds to an operator or operand.
    - Provides a clear hierarchical structure of expressions, aiding in optimization and code generation.
  • Three Address Code:
    - A low-level code format that uses at most three addresses for instructions.
    - Typically has the form `x = y op z`, allowing for straightforward translation to machine code.

Conclusion
All three forms—Postfix notation, Syntax Trees, and Three Address Code—serve as effective intermediate representations in the compilation process. They cater to different aspects of code optimization and generation, making option 'D' (All of the mentioned) the correct answer. Each of these forms plays a significant role in achieving efficient and portable code execution across different platforms.

The linker
  • a)
    Is similar to interpreter
  • b)
    Uses source code as its input
  • c)
    I s required to create a load module
  • d)
    None of the mentioned
Correct answer is option 'C'. Can you explain this answer?

Kunal Gupta answered
Explanation: It is a program that takes one or more object files generated by a compiler and combines them into a single executable file, library file, or another object file.

Which of the following grammar rules violate the requirements of an operator grammar?
(i) P -> QR
(ii) P -> QsR
(iii) P -> ε
(iV) P -> QtRr
  • a)
    (i. only
  • b)
    (i. and (iii. only
  • c)
    (ii. and (iii. only
  • d)
    (iii. and (iv. only
Correct answer is option 'B'. Can you explain this answer?

Nitin Datta answered
Explanation:
An operator precedence parser is a bottom-up parser that interprets an operator-precedence grammar.
Consider the grammar with the following translation rules and E as the start symbol.
A -> A1 #B {A.value = A1.value * B.value}
| B {A.value = B.value}
B-> B1 & F {B.value = B1.value + C.value}
|C {B.value= C.value }
C -> num {C.value = num.value}.

 Which of the following actions an operator precedence parser may take to recover from an error?
  • a)
    Insert symbols onto the stack
  • b)
    Delete symbols from the stack
  • c)
    Inserting or deleting symbols from the input
  • d)
    All of the mentioned
Correct answer is option 'D'. Can you explain this answer?

Nabanita Basak answered
Understanding Operator Precedence Parsing Error Recovery
Operator precedence parsing is a technique used in compilers to analyze the syntax of expressions based on their operator precedence and associativity. However, when a parser encounters an error, it must have a strategy for recovery to continue with the parsing process. The actions an operator precedence parser may take include:
Insert Symbols onto the Stack
- In cases where the parser identifies a missing operator or operand, it can insert the necessary symbols onto the stack. This action helps to align the stack with the expected structure of the expression.
Delete Symbols from the Stack
- When the parser detects extraneous symbols—such as an unexpected operator or parenthesis—it can remove these symbols from the stack. This deletion helps to correct the structure and allows the parser to attempt to recover from the error.
Inserting or Deleting Symbols from the Input
- The parser may also modify the input stream. This can involve inserting missing symbols or deleting unnecessary ones to ensure that the remaining input aligns with valid syntax. This flexibility allows the parser to adapt to various types of errors.
All of the Mentioned
- Since all the aforementioned actions (inserting symbols onto the stack, deleting symbols from the stack, and modifying the input) are valid strategies for error recovery, the correct answer is option 'D'. Each action plays a crucial role in allowing the parser to regain a valid state and continue parsing efficiently.
In conclusion, an operator precedence parser employs a variety of strategies to handle errors effectively, ensuring robust parsing of expressions in programming languages.

A top down parser generates
  • a)
    Rightmost Derivation
  • b)
    Right most derivation in reverse
  • c)
    Left most derivation
  • d)
    Left most derivation in reverse
Correct answer is option 'C'. Can you explain this answer?

Rohan Shah answered
Understanding Top-Down Parsing
Top-down parsing is a method of syntax analysis used in compilers and interpreters to derive a string from a grammar. This approach starts from the root of the parse tree and works its way down to the leaves.
Rightmost vs. Leftmost Derivation
- Rightmost Derivation: This type of derivation always replaces the rightmost non-terminal first.
- Leftmost Derivation: Conversely, this derivation replaces the leftmost non-terminal first.
Why Option 'C' is Correct
- A top-down parser generates Leftmost Derivation because it begins with the start symbol and applies production rules by expanding the leftmost non-terminal at each step.
- This means, in every step of the parsing process, it focuses on the leftmost non-terminal, applying the corresponding production until it reaches the terminal symbols.
Characteristics of Top-Down Parsing
- Recursive Approach: Often implemented using recursive descent, where each non-terminal is represented by a function.
- Predictive Parsing: Utilizes lookahead tokens to determine which rule to apply next, commonly seen in LL parsers.
Significance of Leftmost Derivation
- Clarity: Leftmost derivation provides a clear path of how the string is derived from the grammar.
- Error Detection: Helps in identifying syntax errors early during the parsing process.
In summary, a top-down parser generates the leftmost derivation, making option 'C' the correct answer. This method is integral to many parsing techniques in compiler design, ensuring an organized approach to understanding and translating programming languages.

Chapter doubts & questions for Parsing - Compiler Design 2025 is part of Computer Science Engineering (CSE) exam preparation. The chapters have been prepared according to the Computer Science Engineering (CSE) exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Parsing - Compiler Design in English & Hindi are available as part of Computer Science Engineering (CSE) exam. Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.

Compiler Design

26 videos|83 docs|30 tests

Top Courses Computer Science Engineering (CSE)