0% found this document useful (0 votes)
9 views7 pages

1791 Lab3

The document describes an algorithm for identifying keywords in a C program file. The algorithm reads an input C file, removes comments, and counts the occurrences of keywords. It handles single-line and multi-line comments, as well as text within quotation marks. The algorithm is implemented in a Java program that takes a C file as input, counts the keywords, and outputs the results to a text file. Potential improvements are discussed, such as better handling of long quoted strings, exception handling, and adapting the program to other languages.

Uploaded by

Md Ehsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

1791 Lab3

The document describes an algorithm for identifying keywords in a C program file. The algorithm reads an input C file, removes comments, and counts the occurrences of keywords. It handles single-line and multi-line comments, as well as text within quotation marks. The algorithm is implemented in a Java program that takes a C file as input, counts the keywords, and outputs the results to a text file. Potential improvements are discussed, such as better handling of long quoted strings, exception handling, and adapting the program to other languages.

Uploaded by

Md Ehsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

0

Problem No: 03
Problem Name: Write a c program to identity keywords from a c program print them.
Algorithm:
Here are the algorithm steps for the given code:
1. Create an array of C keywords.
2. Read from the input file ("input.c") and write to the output file ("output.txt").
3. Make a HashMap to keep track of the keyword counts.
4. The boolean variables isComment and isInQuotes should be set to false.
5. Each line of the input file should be read.
6. Remove all leading and following spaces from the line.
7. Check to see whether the line begins with "/*" to see if it is the start of a multi-line
comment. If this is the case, set isComment to true.
❖ Set isComment to false if the line ends with "*/" and go on to the following line.
8. Determine whether the line ends with "*/" to see if it is the end of a multi-line
comment. If this is the case, set isComment to false and move on to the next line.
9. If the line is not within a comment (isComment is false) and is not empty, use the
regular expression "W+" to break it into words.
10. Iterate through the line, one word at a time.
❖ Check if the term finishes with a double quotation if isInQuotes is true. If this is
the case, set isInQuotes to false and go on to the next word.
❖ Set isInQuotes to true if the word begins with a double quotation and go on to
the following word.
❖ If the word is a C keyword, update the keywordCountMap with its count.
11. The input file should be closed.
12. Add keyword counts to the output file.
13. The output file should be closed.
14. To the console, print a success message.

1
Code:

2
Figure 01. Lab3_1791 java code photo.

3
Output:

Figure 02. Lab3_1791 java code output photo.

4
Lab_1791 java code input.c file screen short

Figure 03. Lab3_1791 java code input.c file photo.

Lab_1791 java code output.txt screen short

Figure 04. Lab3_1791 java code output.txt file photo.

5
Discussion:
The developed code collects keywords and eliminates comments from a C programming
language file successfully. It uses a well-defined algorithm to run through each line of the
input file, identifying keywords from a specified list and counting their occurrences. The
keyword counts are then saved to a file.
The code displays proficiency in text manipulation, regular expressions, and file handling
in Java. It handles single-line and multi-line comments properly, ensuring that keywords
within comments are not tallied. The software additionally takes into account quoted
string literals by eliminating the surrounding quotation marks and appropriately omitting
them from keyword counts.
However, a few issues and potential areas for development should be noted. To begin, the
code might be improved to handle situations in which quoted strings cover many lines or
when a single line contains numerous quoted strings. Furthermore, robustness should be
ensured by implementing complete error handling and exception catching techniques in
circumstances such as missing input files or write permission difficulties for the output file.
Furthermore, scalability may be an issue when processing huge files or projects with a
significant number of files. Because the code records all keyword occurrences in memory,
memory utilization may grow. To remedy this, optimizing the code to handle files in
smaller parts or employing efficient data structures may help.
Furthermore, the code is unique to the C programming language and is based on a
specified set of C keywords. If it is intended to be used for other programming languages,
changes will be necessary to adjust the set of keywords or enable dynamic programming
language specification.
In conclusion, the supplied code is a useful solution for extracting keywords and deleting
comments from C programming language files. It demonstrates understanding of essential
programming principles and techniques. However, there is potential for development in
special case handling, error management, scalability for bigger files, and adaptability for
other programming languages.

You might also like