Self Studies

Compiler Design Test 1

Result Self Studies

Compiler Design Test 1
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0
    If C is a programming language, then which of the following cannot be a token produced by lexical analysis phase?
    Solution
    • The first phase of compiler is called lexical analysis or scanning. The lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequence called lexemes. For each lexeme, the lexical analyzer produces as output a token of the form that it passes on to the subsequent phase, syntax analysis.
    • Tokens may be of the form of identifiers, keywords, constants etc. from the input program.

     

    ++

    unary operator (incremental operator)

    *=

    Assignment operator

    << 

    Bitwise operator

     

    ++, *=, << are lexemes that is mapped into the token < ++ >, < *= > and < << >

    All operators are token

    Therefore option 4 is correct.

  • Question 2
    1 / -0

    If below given options are written in C language, then which of the following code can produce lexical error?

    Solution
    • The first phase of compiler is called lexical analysis or scanning. The lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequence called lexemes. For each lexeme, the lexical analyzer produces as output a token of the form that it passes on to the subsequent phase, syntax analysis.
    • Tokens may be of the form of identifiers, keywords, constants etc. from the input program.
    • In C language identifiers are the names given to variables, constants, functions and user-define data. Identifier cannot start with number.
    • Since 2y is not valid identifier. Therefore, lexical analysis will produce an error


    Important Points:

    All 4 statement will produce error, but lexical error can be produced only with option 4.

  • Question 3
    1 / -0

    Which of the following statements is/are TRUE about the lexical analyzer?

    I. It reads the input characters and produces a sequence of tokens that the parser uses for syntax analysis.

    II. Lexical analyzer removes comments and white spaces in the form of blank, tab, and newline characters.

    III. It correlates error messages from the compiler with the source program.

    Solution
    • Lexical analyzer is the first phase of a compiler.
    • The input is a high-level language program, such as a ’C’ program in the form of a sequence of characters.
    • The output is a sequence of tokens that are sent to the parser for syntax analysis Strips off blanks, tabs, newlines, and comments from the source program Keeps track of line numbers and associates error messages from various parts of a compiler with line numbers.
  • Question 4
    1 / -0

    The number of tokens in the following C program

    int main() {
    float p  = 2.33;
    double q = 19;
    if(p != q)
    printf("p = %f", p++);
    else
    printf("q = %lf", ++q);

    }

    What is the number of tokens?

    Solution

    Line no.

    Tokens

    Number of Tokens

    Line 1int | main | ( | ) | } |5

    Line 2

    float | p |  = | 2.33 | ; |

    5

    Line 3

    double | q | = | 19 | ; |

    5

    Line 4

    printf | ( | "p = %f" | , | p  |++ |  ; |

     

    8

    Line 5if | ( | p | != | q | ) |6
    Line 6else |1
    Line 7printf | ( | "q = %lf" | , | ++  | q |  ; |8
    Line 8} |1

     

    Total Number of tokens = 5 + 5 + 5 + 8 + 6 + 1 + 8 + 1 = 39

    Notes:

    | is used to separate the token

  • Question 5
    1 / -0

    Consider the below given table in which regular expression is mapped to unique token:

    Regular ExpressionToken
    p*(p | q)p*3
    qp*1
    p+(r | q) pq2

    What will be the output when the string ''pppqppqprpqp" is scanned by the lexical analyzer if the analyzer outputs the token that matches the longest possible prefix if it is mandatory to use all the tokens at least once?

    Solution

    String: pppqppqprpqp

    Regular expression

    p*(p | q)p*

    qp*

     

    p+(r | q) pq

     

    p*(p | q)p*

    Token

    3

    1

    2

    3

    String

    pppqpp

    q

    prpq

    p

     

    The output is 3123

  • Question 6
    1 / -0

    Consider the following statements:

    S1: New entries are created in symbol table for each new identifier during lexical analysis.

    S2: Lexical analyzer detects ill-formed numeric literals and input characters that are not in the source language.

    Which of the following is true?

    Solution

    The correct answer is option 3 i.e. Both S1 and S2.

    A lexical analyzer creates a symbol-table entry as soon as it sees the characters that make up a lexeme. Also, it detects ill-formed numeric literals and input characters that are not in the source language.​​

  • Question 7
    1 / -0

    Consider the below-given code written in C language

    #include<stdio.h>

    int main()

    {

    int k  = 20;

    whlie(--k)

    printf(“GATE CS 2021\n”);

    }

    Identity the compiler’s response about the above-given code while creating the object- module
    Solution

    #include<stdio.h>

    int main()

    {

    int k  = 20;

    whlie(--k)                 \\incorrect while spelling

    printf(“GATE CS 2021\n”);

    }

    while is a keyword in C programming language but code has incorrect spelling of while, that is, whlie

    Now, it will be an error

    Lexical analyzer will treat whlie as a valid identifier

    But the syntax analyzer will catch this error. Hence it is a syntactic error.
  • Question 8
    1 / -0

    A particular BNF definition for a “word” is given by the following rules.

    <word>  :: = <letter> | <letter><charpair> | <letter><intpair>

    <charpair>  :: = <letter><letter> | <charpair><letter><letter>

    <intpair>  :: = <integer><integer> | <intpair><integer><integer>

    <letter> :: = A | B | C | ….. | Y | Z

    <integer> :: = 0 | 1 | 2 | …. | 9

    Which of the following lexical entries can be derived from < word >?

    I. TESTBOOK

    II. TTP

    III. T20

    Solution

    <word>  :: = <letter><charpair>

    <word>  :: = <letter><charpair><letter><letter>

    <word>  :: = <letter><charpair><letter><letter><letter><letter>

    <word>  :: = <letter><charpair><letter><letter>l<etter><letter>

    <word>  :: = <letter><charpair><letter><letter>letter><letter>l<etter><letter> // cannot derive TESTBOOK

    Since number of letter in TESTBOOK = 8

    <word>  :: = <letter><letter><letter><letter><letter>letter><letter>letter><letter> // cannot derive TESTBOOK

    <word>  :: = <letter><charpair>

    <word>  :: = <letter> <etter><letter>

    <word>  :: = TTP     //derived

    <word>  :: = <letter><intpair>

    <word>  :: = <letter><intpair>

    <word>  :: = <letter><integer><integer>.

    <word>  :: = T20 //derived

    Option 3 is correct

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Selfstudy
Selfstudy
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now