Assignment 0
1 This is the first trial line in the file,Try the problem once using fgetc() function and once using fgets() function for reading the input. Why is fread() not suitable for this purpose?
2 and this is the second line.
C is a programming language.and if the field-number specified is 4, then the output of the program is -
lex produces a lexical analyser
cc is a compiler
programming
lexical
(NULL)
compiler
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.
(a) Write the lexical analyser in C without using a tool such as flex.
(b) Use flex for creating the lexical analyser.
Implement a desk calculator using operator precedence parsing.
Imagine the syntax of a programming language construct such as while-loop --
while ( condition )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).
begin
statement ;
:
end
Write a context free grammar for the above construct, and create an operator precedence parsing table based on the grammar. Hint: Represent the grammar in appropriate data structures, then create the LEADING and TRAILING sets for the non-terminals of the grammar.
Assignment 4 Write a recursive descent parser for the construct mentioned in the previous question.
Assignment 5 Implement the routines to compute the FIRST and FOLLOW sets of the non-terminals of a given grammar. Also, write the routine to create an LL(1) parsing table for the grammar.
Hint: Take a suitable grammar and represent it in appropriate data structures.
Assignment 6
Implement an LL(1) parser that uses the LL(1) parsing table you have defined in the previous exercise.
Assignment 7
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.
Assignment 8
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 9 Implement a desk calculator using bison. Include addition, subtraction, multiplication and division operations in input arithmetic expressions.
An useful page related to bison (local)
Assignment 10
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.
Additional Exercise
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 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.