Вы находитесь на странице: 1из 3

Principles of Programming Language and Compilers

Assignment No 1

1. Download and analyze the Lex/Flex tool.

2. Analyze the object code

3. Write a program to Convert regular expression to DFA directly using firstpos(), laspos()
and followpos(). Display the result of firstpos(), laspos() and followpos() before printing
the DFA state transition.

4. Write a C/C++/Java program to tokenize the simple “Hello World” program of C language.
First of all, define the token for this program. The original program should necessarily
include few comments. Display the tokens by removing all the comments, i.e., the
comments should be ignored for tokenization. No predefined function for tokenization is
allowed. Assume the situations, with and without space between the tokens in the program.

5. Write a Program in Flex which identifies C Integers and float numbers. Your program
should respond to various inputs as follows:
Sample I/P Sample Lexical output

-273 Integer
645 Integer
8234.01 Float

6. Write a program to eliminate the Left Recursion using the given in Algorithm 4.19 (Page
No. 213) of Compilers Principles, Techniques and Tools book.

7. Write a program to identify if a given grammar is ambiguous

8. Write a program to find the FIRST for all grammar symbols and FOLLOW for all Non-
Terminals in a given grammar.

9. Write a C program to recognize strings under 'a', 'a*b+', 'abb'.

10. Write a C program to simulate lexical analyzer for validating operators.

11.Implement the lexical analyzer using JLex, flex or other lexical analyzer generating tools.
12. Write a C program to test whether a given identifier is valid or not.

13. Design a lexical analyzer for given language and the lexical analyzer should ignore redundant
spaces, tabs and new lines. It should also ignore comments. Although the syntax specification
states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value.
Simulate the same in C language.

14. Write a C program to identify whether a given line is a comment or not.

15. 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.

16. Implement a desk calculator using operator precedence parsing.

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

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

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.

18. Write a recursive descent parser for the construct mentioned in the previous question.

19. 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.

20. Consider following grammar

Write a c/c++/ Java program using the CYK algorithm to recognize the strings produced by the
above grammar.