C代写:CS117 Comments

代写C语言基础作业,练习I/O使用,并编写一个Finite State Machines来输出所有Comments的内容。

Objectives

  • to compile C programs using the Unix command line
  • to use C I/O functions to use finite state machines
  • to model a process

Assignment

Write a program called Comments that reads C source code from standard input and writes the text of the comments to standard output. The text of a comment does not include the delimiters /* and */ at the beginning or end of a C-ctyle comment, or the marker // that starts a C++-style comment. In both cases, leading whitespace and asterisks (*) on each line should not be output; internal and trailing whitespace should be output as is and each non-empty comment should be output with a newline character at the end (“non-empty” means contains something other than whitespace, asterisks, and tags). Furthermore, doxygen/javadoc style tags - any word within a comment that starts with an at symbol (@) - should not be output regardless of what type of comment it is in and whether it is actually a valid tag (so don’t output something like @DrGlennNFA even though that is not a tag used by either tool). Additionally, note that

  • tags should be treated as whitespace for the purposes of determining what is leading whitespace,
  • line breaks should be output where found in comments, except
    • a backslash (\) at the end of a line immediately before a newline character is a linecontinuation character and should be ignored along with the following newline; for all purposes consider those two characters to not appear in the input
    • there should be no empty lines in the output,
  • the beginning of a comment should be treated as the beginning of a line regardless of whether the line contains code before the comment, and
  • all other punctation must be output as it appears in the input (we leave the task of filtering out such things to others).

Other things to consider:

  • within string and character literals, /* and // do not mark the beginning of a comment
  • quotes within comments need not be balanced
  • you may assume that string and character literals are properly terminated
  • you may assume that the character after an unescaped backslash within a string or character literal creates a valid escape sequence
  • line-continuation (see above) is valid within string and character literals as well as in comments
  • you need not do anything special for preprocessor directives; treat them like any other code
  • you may assume there are no trigraphs in the input

Your program must handle violations of those assumptions gracefully - it must not crash or go into an infinite loop.

Your program must use only a constant amount of space (in other words, the amount of memory used must not vary with the amount of input read). So, for example, you may not use arrays or call malloc.

Your submission must include a makefile that produces an executable file named Comments when make is run with no arguments.

Example

If the input file input_0.txt contains

1
2
3
4
5
6
7
8
/* This is a single-line C comment */
#include <stdio.h>
/******
This is a nicely formatted * multi-line comment. ******/

int main(int argc, char **argv)
{

// This is a C++ comment.
}

Then the execution of the program would be as follows.

$ ./Comments < input\_0.txt This is a single-line C comment This is a nicely formatted multi-line comment. This is a C++ comment.