2016-12-29 118 views
-1

下面是我的代码:如何忽略读/ *注释* /的内容,但读取文件

string ckeywords = File.ReadAllText("E:\\ckeywords.csv"); 
string[] clines = File.ReadAllLines("E:\\cprogram\\cpro\\bubblesort.c"); 
string letters=""; 

foreach(string line in clines) 
{ 
    char[] c = line.ToCharArray(); 
    foreach(char i in c) 
    { 
     if (i == '/' || i == '"') 
     { 
      break; 
     } 
     else 
     { 
      letters = letters + i; 
     } 
    } 
} 
letters = Regex.Replace(letters, @"[^a-zA-Z ]+", " "); 

List<string> listofc = letters.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); 
List<string> listofcsv = ckeywords.Split(new char[] { ',', '\t', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList(); 
List<string> Commonlist = listofcsv.Intersect(listofc).ToList(); 

有了这个if条件下,我能够不理之间的单行注释的阅读内容和内容( “”)。

我需要忽略阅读多行注释的内容。我应该使用哪种情况? 假设我的.c文件有这样的注释,所以上面的代码我不知道如何开始从/ *到* /进行迭代,并忽略它们之间的内容。

/* printf(“按升序排序的列表:\ n”);

为(C = 0; C^< N; C++) 的printf( “%d \ n” 个,阵列[C]); */

+0

http://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689 –

+0

作为替代。 如果您阅读line的行,那么您可以查找start/*并删除该行的所有内容。你拉一个标志,并删除每一行,直到你读完* /。并防止删除任何东西。 –

+0

我知道这个逻辑。我需要在csharp中的代码以更简单的方式忽略。 –

回答

1

我成功地解决了我的问题现在我可以忽略阅读/ * * /的内容,而不使用正则表达式。 这里是我的代码:

string[] clines = File.ReadAllLines("E:\\cprogram\\cpro\\bubblesort.c"); 
List<string> list = new List<string>(); 
int startIndexofcomm, endIndexofcomm; 

for (int i = 0; i < clines.Length ; i++) 
    { 
     if (clines[i].Contains(@"/*")) 
      { 
      startIndexofcomm = clines[i].IndexOf(@"/*"); 
      list.Add(clines[i].Substring(0, startIndexofcomm)); 

      while(!(clines[i].Contains(@"*/"))) 
      { 
       i++; 
      } 

      endIndexofcomm = clines[i].IndexOf(@"*/"); 
      list.Add(clines[i].Substring(endIndexofcomm+2)); 

      continue; 
      } 
      list.Add(clines[i]); 
    } 
-2

C语言使用 “C预处理器” 剥离评论除文件外,这是该语言的重要组成部分。所以大多数C文件包含macros and definitions这是必不可少的,它们使用预处理器转换为C.

#define forever for(;;) /* Define a "new keyword" for convenience */ 

forever 
{ 
    /* Eternally looping code */ 
} 

所以如果没有预处理器,你的C程序就没什么意义了。

除非你真的想自己写,否则你可以通过现有的C preprocessor运行bubblesort.c去除注释。

我想从.c文件

你是从头开始编写自己的C编译器的路径上只挑C关键字信息。这是一个太大的问题在这里回答。你需要Dragon Book

+0

这通常是一种实用的方法,但C预处理器比OP要求的要多得多。特别是,它使'#include'指令拉入引用的头文件。我在这个问题中没有看到任何暗示这是可以接受的。 – hvd

+0

我正在阅读C#程序中的.c文件,并且我想忽略阅读多行,单行注释以及(“this”)之间的内容的内容。 –

+0

你是什么意思“之间(”这个“)”?你的意思是括号中的文字吗? –

0

这里是代码,天真地执行以下操作:

  1. 它剔除了任何多行注释开始/**/结束,即使有两间换行。
  2. 它剔除任何单行注释开始//和在该行的末尾结束
  3. 它确实不出像上述如果他们与"和端部开头的字符串中的任何评论与一个"

LINQPad代码:

void Main() 
{ 
    var code = File.ReadAllText(@"d:\temp\test.c"); 
    code.Dump("input"); 

    bool inString = false; 
    bool inSingleLineComment = false; 
    bool inMultiLineComment = false; 

    var output = new StringBuilder(); 
    int index = 0; 

    while (index < code.Length) 
    { 
     // First deal with single line comments: // xyz 
     if (inSingleLineComment) 
     { 
      if (code[index] == '\n' || code[index] == '\r') 
      { 
       inSingleLineComment = false; 
       output.Append(code[index]); 
       index++; 
      } 
      else 
       index++; 

      continue; 
     } 

     // Then multi-line comments: /* ... */ 
     if (inMultiLineComment) 
     { 
      if (code[index] == '*' && index + 1 < code.Length && code[index + 1] == '/') 
      { 
       inMultiLineComment = false; 
       index += 2; 
      } 
      else 
       index++; 
      continue; 
     } 

     // Then deal with strings 
     if (inString) 
     { 
      output.Append(code[index]); 
      if (code[index] == '"') 
       inString = false; 
      index++; 
      continue; 
     } 

     // If we get here we're not in a string or in a comment 
     if (code[index] == '"') 
     { 
      // We found the start of a string 
      output.Append(code[index]); 
      inString = true; 
      index++; 
     } 
     else if (code[index] == '/' && index + 1 < code.Length && code[index + 1] == '/') 
     { 
      // We found the start of a single line comment 
      inSingleLineComment = true; 
      index++; 
     } 
     else if (code[index] == '/' && index + 1 < code.Length && code[index + 1] == '*') 
     { 
      // We found the start of a multi line comment 
      inMultiLineComment = true; 
      index++; 
     } 
     else 
     { 
      // Just another character 
      output.Append(code[index]); 
      index++; 
     } 
    } 

    output.ToString().Dump("output"); 
} 

样品输入:

This should be included // This should not 
This should also be included /* while this 
should not */ but this should again be included. 

Any comments in " /* strings */ " should be included as well. 
This goes for "// single line comments" as well. 

样本输出(注意,有一些空间在一些下面不可见的线的端部) :

This should be included 
This should also be included but this should again be included. 

Any comments in " /* strings */ " should be included as well. 
This goes for "// single line comments" as well. 
+0

为了完整起见,与C:它不处理字符常量(在'int main(){'''; short s;}','short s;'不是字符串),它不处理字符串中的反斜杠(在'main main(){“\”“; short s;}','short s;'不再是字符串的一部分)或作为行拼接的一部分在''''/'''中,然后在下一行'* int main(){} * /'中,这两行形成一个注释),当修改它来处理反斜杠和字符常量时,trigraphs可以形成问题也在(int main(){0 ??'“”[0]; short s;}','short s;'不是字符常量的一部分)。这可能没问题。 – hvd

+0

是的,但正如我试图在我对这个问题的评论中陈述的那样,如果OP ***明确地不需要/想要这样的“复杂”的事情,那么一个天真的解决方案就是最好的解决方案。我甚至不想*尝试*编写一个可以处理所有符合C语法的解决方案。 –

+0

我不会;鉴于这是OP所要求的,我选择根本不回答。 :)我只是想说清楚什么可行,哪些行不通,这样OP和其他人阅读这个答案就可以做出明智的决定,是否足够满足他们的需求。 – hvd