Index-1              Index-2              Home Page

Compiler Construction Question and Answer


a)
Illustrate your concept regarding Token and Regenerate Token with example.

b)
Write a C/C++/Java/Python Program to Create token from x=(a+b)/c*2. Take input from a file and generate input to another file.




c)
Describe the basic structure of a compiler.

d)
What is Parser? Write an algorithm to parse a token. You may provide source code using C/C++/Java/Python but you have to explain it with comments.




e)
Draw the Structure of a Compiler with mentioning all the phases.

f)
Find all the relations and dependency between Lexical Analyzer and Syntax Analyzer.




g)
How Lexical Analyzer reads the input characters from source program to produce Lexeme? Explain.

h)
Write a program to Re-Generate a token using C/C++/Java/Python




i)
Explain Semantic Analysis with Example.

j)
Write a code using C/C++/Java/Python to remove ambiguity and take value for the appropriate variable.


k)
Difference between LL and LR parsing.

l)
Write an algorithm for construction of a predictive parsing table. Construct the parsing table for the following grammar:   
            E → E+T │ T; T → T*F │ F; F → (E) │ id.     

m)
State the difference between compiler and interpreter.
n)
Production rules:
E → E + E
E → E * E
E → id
Input string: id + id * id
Draw parsing table using left most derivation and Right most derivation

14 comments:

  1. A) Illustrate your concept regarding Token and Regenerate Token with example.

    Ans :
    #include
    using namespace std;
    int main()
    {
    int count=0;
    char c;
    string s;
    cin>>s;
    ofstream wrtme(“test.txt”);
    ifstream rdme(“test.txt”);
    while(s[count]!=’\0’)
    count++;
    for(int i=0;i>c)
    {
    If(c>=’a’ && c<=’z’)
    cin>>c; //Re-generate Token for insert value
    }
    return 0;
    }

    ReplyDelete
  2. B) Write a C/C++/Java/Python Program to Create token from x=(a+b)/c*2. Take input from a file and generate input to another file.

    Ans:
    #include
    using namespace std;
    int main()
    {
    int count=0;
    char c;
    string s = “x=(a+b)/c*2”;
    ofstream mnvar(“mainvar.txt”);
    ofstream wrtme(“test.txt”);
    ifstream rdme(“test.txt”);
    ifstream iprd(“input.txt”);
    ofstream ipwrt(“inputwirte.txt”);

    while(s[count]!=’\0’)
    count++;
    mnvar<>c)
    {
    If(c>=’a’ && c<=’z’)
    {
    for(int j=0;j>c;j++); //get input
    i++;
    ipwrt<<c; //Put input to another file
    }


    }
    return 0;
    }

    ReplyDelete
  3. C) Describe the basic structure of a compiler.

    Ans : Simply stated, a compiler 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 (see Fig. 1 .I). As an important part of this translation process, the compiler reports to its user the presence of errors in the source program.
    Fig : https://snag.gy/RhJMB0.jpg

    Compilers arc sometimes classified as single-pass, multi-pass, load-and-go, debugging, or optimizing, depending on how they have been constructed or on what function they are supposed to perform. Despite this apparent complexity, the basic tasks that any compiler must perform arc essentially the same. By understanding these tasks, we can construct compilers hr a wide variety of source languages and target machines using the same basic techniques.

    ReplyDelete
  4. D) What is Parser? Write an algorithm to parse a token. You may provide source code using C/C++/Java/Python but you have to explain it with comments.

    Ans :
    Parser : A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree.
    Fig : https://snag.gy/rdRjPt.jpg

    In the expression initial + rate * 60, the phrase rate 60 is a logical unit bemuse the usual conventions of arithmetic expressions tell us that multiplication is performed before addition. Because the expression 5 initial + rate is followed by a *. it is not grouped into a single phrase by itself in Fig. 1.4.

    ReplyDelete
  5. E) Draw the Structure of a Compiler with mentioning all the phases.

    Ans :
    Fig : https://snag.gy/9JMCDs.jpg

    ReplyDelete
  6. A) Illustrate your concept regarding Token and Regenerate Token with example.

    Ans :
    #include
    using namespace std;
    int main()
    {
    int count=0; //initial a variable
    char c; //initial a variable
    string s; //initial a variable
    cin>>s; //take input
    ofstream wrtme(“test.txt”); //create file for write
    ifstream rdme(“test.txt”); //use file for read file
    while(s[count]!=’\0’) //calculate the length of string
    count++;
    for(int i=0;i>c) //read file for regenerate token
    {
    If(c>=’a’ && c<=’z’)
    cin>>c; //Re-generate Token for insert value
    }
    return 0;
    }

    ReplyDelete
  7. B) Write a C/C++/Java/Python Program to Create token from x=(a+b)/c*2. Take input from a file and generate input to another file.

    Ans:
    #include
    using namespace std;
    int main()
    {
    int count=0; //initial a variable
    char c; //initial a variable
    string s = “x=(a+b)/c*2”; //initial a variable
    ofstream mnvar(“mainvar.txt”); //create file for write
    ofstream wrtme(“test.txt”); //create file for write
    ifstream rdme(“test.txt”); //use file for read file
    ifstream iprd(“input.txt”); //use file for read file
    ofstream ipwrt(“inputwirte.txt”); //create file for write

    while(s[count]!=’\0’) //calculate the length of string
    count++;
    mnvar<>c) //read file for regenerate token
    {
    If(c>=’a’ && c<=’z’)
    {
    for(int j=0;j>c;j++); //get input
    i++;
    ipwrt<<c; //Put input to another file
    }


    }
    return 0;
    }

    ReplyDelete
  8. F) Find all the relations and dependency between Lexical Analyzer and Syntax Analyzer.

    Ans :

    Relations : Lexical analysis is the first phase of a compiler. It takes the modified source code from language pre processors that are written in the form of sentences. A syntax analyzer or parser takes the input from a lexical analyzer in the form of token streams
    Dependency : Linear analysis is called Iexical analysis. Synthesis analysis involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. Without lexical analysis syntax analysis can’t be proceed. The linear analysis is the first step of the process. After that, syntax analysis has come.

    ReplyDelete
  9. G) How Lexical Analyzer reads the input characters from source program to produce Lexeme? Explain.
    Ans :

    In a compiler, linear analysis is called Iexical analysis, or scanning. For example, in lexical analysis the characters in the assignment statement 'position := initial + rate * 60 would be grouped into the following tokens;:
    1. The identifier position.
    2. The assignment symbol : =.
    3. The identifier initial.
    4. The plus sign.
    5. The identifier rate.
    6. The multiplication sign.
    7. The number 60,
    The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.

    ReplyDelete
  10. H) Write a program to Re-Generate a token using C/C++/Java/Python

    Ans:
    #include
    using namespace std;
    int main()
    {
    int count=0; //initial a variable
    char c; //initial a variable
    string s = “a+b/2”; //initial a variable
    ofstream wrtme(“test.txt”); //create file for write
    ifstream rdme(“test.txt”); //use file for read file
    while(s[count]!=’\0’) //calculate the length of string
    count++;
    for(int i=0;i>c) //read file for regenerate token
    {
    If(c>=’a’ && c<=’z’)
    cin>>c; //Re-generate Token for insert value
    }
    return 0;

    ReplyDelete
  11. I) Explain Semantic Analysis with Example.

    Ans :
    Definition : The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent de-generation phase.

    It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operands of expressions and statements. An important component of semantic analysis is type checking. Here the compiler checks that each operator has operands that are permitted by the source language specification.
    Example 1.1, Inside a machine, the bit pattern representing an integer is generally different from the bit pattern for a real, even if the integer and the real number happen to have the same value.

    ReplyDelete
  12. J) Write a code using C/C++/Java/Python to remove ambiguity and take value for the appropriate variable.

    Ans :
    #include
    using namespace std;
    int main()
    {
    int count=0; //initial a variable
    char c; //initial a variable
    string s1 = “a + b / 2” ,s2; //initial a variable
    ofstream wrtme(“test.txt”); //create file for write
    ifstream rdme(“test.txt”); //use file for read file
    while(s1[count]!=’\0’) //calculate the length of string
    count++;

    for(int i=0;i>c) //read file for regenerate token
    {
    If(c>=’a’ && c<=’z’)
    cin>>c; //Re-generate Token for insert value
    }
    return 0;

    ReplyDelete
  13. K) Difference between LL and LR parsing.

    Ans :
    • LL parser begin at the start symbol and try to apply productions to arrive at the target string, whereas LR parser begin at the target string and try to arrive back at the start symbol.
    • LL parsing, also known as top-down parsing and LR parsing, also known as bottom-up parsing .
    • LL starts with only the root non terminal on the stack, LR ends with only the root non terminal on the stack .
    • LL uses grammar rules in an order which corresponds to pre-order traversal of the parse tree, LR does a post-order traversal.
    • LL continuously pops a non terminal off the stack, and pushes a corresponding right hand side; LR tries to recognize a right hand side on the stack, pops it, and pushes the corresponding non terminal .
    • LL reads terminal when it pops one off the stack, LR reads terminals while it pushes them on the stack .

    ReplyDelete
  14. M) State the difference between compiler and interpreter.

    Ans :
    Pic : https://snag.gy/kqE3N6.jpg

    ReplyDelete