Assignment 2: lex

Due Thursday, Oct. 9th

Some C functions that you may find helpful: Use man <function_name> if you need more information about a particular function.

You should not need any other string functions. Note that problem 3 asks you to print the string in reverse, not to store the reversed string in memory. C strings are arrays of characters, so this task can be accomplished by a simple loop.

Problem 1.

Write a lex program that reads the input file (via the standard input) and outputs the following: The counting is inclusive (i.e. the word 'common' should be counted in all three categories). However, a word cannot be counted twice in the same category.

For instance, the output for the sentence

a common meaning of the word "mammoth" is "something immense or monumental"

should be:


total words: 12
words with at least one 'm': 6
words with at least 2 'm': 4
words with a double 'm': 3 
Use lex command 'REJECT' to allow a token to match more than one rule (see the example of counting occurrences of he/she in the online manual).

Problem 2

Write a lex program that will read afile and output it to the standard output, replacing all dates in the form 'mm/dd/yy' by the format 'Month Day, Full Year. For instance, 10/02/03 will be replaced by October 2, 2003. You may assume that if the year starts with a zero or one, then it will be preceded by 20, otherwise by 19 (i.e. 03/03/13 means March 3, 2013, but 03/03/22 means March 3, 1922).

Write helper C functions in the third part of your lex file.

Problem 3

Write a lex program that will parse a Java program and output to the standard output another Java program as follows: You don't have to write a complete list of keywords, some reasonable subset is sufficient. Note that you need to distinguish between Java strings (in double quotes) and identifiers.
This page is a part of CSci 4607 course at UMM.