Laboratory Assignments for Compiler Design for Spring 2015

Assignment 0

  1. Write a C program that reads text from a file and prints on the terminal each input line, preceded by the line number. The output will look like -
    1     This is the first trial line in the file,
    2     and this is the second line.
    Try the problem once using fgetc() function and once using fgets() function for reading the input. Why is fread() not suitable for this purpose?

    Do not ignore the value returned by the functions fgetc() and fgets(). After this exercise the you should be comfortable with the formatted input and output functions of C.

  2. Write a program that takes from the user the name of a file and a "field-number", and then reads that file and for each line in that file prints on the terminal word at position "field-number". For example if there are the following lines in the specified file -
    C is a programming language.
    lex produces a lexical analyser

          cc is a compiler
    and if the field-number specified is 4, then the output of the program is -
    programming
    lexical
    (NULL)
    compiler

    After this exercise you should be able to deal with individual words in a text file.

  3. Write a C program that reads the names (as character strings of length upto 20 bytes) and corresponsing roll-numbers (as integers) of 10 students from the user and stores them in a file whose name is specified by the user-
    After running the program check the size of the file created using ls -l command. Also see the content of the binary file using the command
    od -c filename.

    After this exercise you should be clear about the difference between a text file and a binary file.

  4. Write a C program that works like the od program of LINUX (UNIX).

  5. Write a C program that works like the strings program of LINUX (UNIX).
[To be completed in 1 week]

Assignment 1

Write a lexical analyser for the C programming language using the grammar for the language given in the book "The C Programming Language", 2e, by B Kernighan and D Ritchie.

Use flex for creating the lexical analyser.

[To be completed in 1 week]
Assignment 2

Implement a desk calculator using operator precedence parsing.

[To be completed in 1 week]
Assignment 3

Imagine the syntax of a programming language construct such as while-loop --

while ( condition )
begin
      statement ;
            :
end
where while, begin, end are keywords; condition can be a single comparision expression (such as x == 20, etc.); and statement is the assignment to a location the result of a single arithmatic operation (eg., a = 10 * b).

Write a program that verifies whether the input follows the above syntax. Use flex to create the lexical analyser module and write a C program that performs the required task using that lexical analyser.

[To be completed in 1 week]

Assignment 4

Write a C program for implementing shift-reduce parsing using a given grammar.
Firstly, define the data structures for representing the given CFG in BNF, the stack for the parsing, and the parse tree to be created.

[To be completed in 1 week]

Assignment 5

Write a C program for creating the precedence table for a given grammar. The table should be created in an appropriate data structure that is compatible with the requirements of Assignment 3.
Hint: Use a distinct number to represent each grammar symbol - terminal and non-terminal.

[To be completed in 1 week]

Assignment 6

Write a LR parser program in C. Define the data structure for the parsing table in such a way that it can be initialised easily (manually) for a given grammar. Take a simple grammar, eg., expression grammar, compute the parsing table entries by hand using the steps discussed in the class, and initialize the table in your program with these values. Try to parse input expressions scanned by a lexical analyser (which can be easily created using flex). The output of the parser should be SUCCESS or FAILURE depending on the input. In case of FAILURE the parser should indicate the incorrect token in the input.

[To be completed in 1 week]

Assignment 7

Modify your LR parser program of the preceding assignment such that along with the reduce actions, the parser invokes routines associated with the particular grammar rule. For example, for a reduction by the rule E -> E + T of the exression grammar, the parser computes the sum of the numbers corresponding to the symbols E and T on the RHS, and associates the sum to the symbol E on the LHS.

Hint: Observe that it will be necessary to associate values with different symbols in the stack. Whenever a reduce action is taken some symbols of the stack are to be replaced by a non-terminal symbol. In this step the value to be associated with the non-terminal is to be computed using the values associated with the symbols that are being replaced.

Assignment 8

Take the C grammar from the book - The C Programming Language (by Kernighan and Ritchie), and try to generate a parser for the language using YACC. The notation for the grammar in the book is not strictly BNF (e.g. use of subscript "opt" with some symbols, use of "one of", etc.), so some rewriting shall have to be done due to that. Apart from that there are are some LALR conflicts which you shall have to resolve by any appropriate means.

Note: This exercise may require more time than available. So start the work, realise the issues and learn the tricks, so that completion of the remaining part should depend not on learning time, but only on adequate coding time.

A Simple example using bison

[To be done in 1 week]

An useful page related to bison (local)

Assignment 9

Take a common programming language construct of an HLL, such as the for-loop construct of the C language. Use LEX and YACC to create a translator that would translate input into three-address intermediate code. The output of the translator should finally be in a file. Assume a simple structure for "statements" that may appear inside the construct, and make necessary assumptions for the intermediate code format.

[To be done in 1 week]