Assignment 2: lex
Due Thursday, Oct. 9th
Some C functions that you may find helpful:
- int atoi(string s) converts a
string representation of an integer number to its numerical value,
i.e. output of atoi("66") is the integer value 66.
- double atof(string s) converts a
string representation of a real number to its numerical value as a
double, i.e. output of atoi("66.55") is the double value
66.55.
- int strlen(string s) returns the length of the string,
for instance strlen("hi there!") returns 9.
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:
- Total number of words (a word is a sequence of letters that may
have a dash (-) or an apostrophe (') in a non-starting position but
no other symbols).
- The number of words with at least one letter 'm' (upper or lower case).
- The number of words with at least 2 letters 'm'.
- The number of words with at least one double 'm' (such as in
'common').
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:
- All the keywords such as 'if', 'else', 'for', 'class', etc. and
all special symbols are left as before.
- All integers are doubled (for instance, 2 is replaced by 4, -1 by
-2).
- All double or float numbers are multiplied by 10 (3.14 will be
replaced by 31.4).
- All names of identifiers will be reversed (for instance, the
variable name 'count' will be replaced by 'tnuoc').
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.