AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 766179
Accepted
jwzumwalt
jwzumwalt
Asked: 2024-01-05 13:21:40 +0800 CST2024-01-05 13:21:40 +0800 CST 2024-01-05 13:21:40 +0800 CST

将所有 C 注释打印到单独的文本文件中

  • 772

我想将所有 C 注释打印到单独的文本文件中。

  • 使用 awk、sed、grep 或 bash
  • 输出 /* ... */ 之间的所有多行 C 注释(包含)
  • 带有 // 注释的输出行
  • 可选:打印行号

我尝试了这些解决方案,但它在 Ubuntu 上不起作用

  • 解析 C 风格多行注释
  • https://stackoverflow.com/questions/57723357/how-can-i-grep-matching-a-comment-string

我的请求的目的是能够快速使用源代码注释作为良好文档的起点。我不喜欢专用文档程序的额外混乱和专有嵌入式命令(即 Doxygen)。例如,正确注释每个源代码函数并删除表面的一行注释将节省大量时间,并提供近乎完整的参考。这也将鼓励更好的源代码注释。

c
  • 4 4 个回答
  • 1301 Views

4 个回答

  • Voted
  1. Opifex
    2024-01-05T22:01:29+08:002024-01-05T22:01:29+08:00

    已经有很多使用 shell-magic 的答案,但我认为通过使用您可能已经拥有的工具可以更容易地完成。即,海湾合作委员会。

    diff -u <(gcc -fpreprocessed -dD -E main.c) main.c | grep '^+' | cut -c 2-

    怎么运行的?

    1. gcc -fpreprocessed -dD -E main.c 从文件中删除所有注释并将其放在标准输出上

    2. diff -u <(...) main.c 从 stdout 获取输入并将其与原始数据进行比较

    3. grep '^+' 过滤以 . 开头的所有行+。换句话说:过滤之前确定的评论

    4. cut -c 2-+从输出中 删除符号

    不需要超级复杂的正则表达式、perl 或 awk 的东西,同时还涵盖其他答案可能错过的所有边缘情况。

    • 26
  2. Stéphane Chazelas
    2024-01-05T16:16:20+08:002024-01-05T16:16:20+08:00

    如果您考虑以下因素,这并不像看起来那么微不足道:puts("string with /*")记住"s 可以出现在 中ch = '"'。

    或者续行:

    printf("...");    /\
    * yes, this is a comment */
    /\
    / and this as well
    

    或三字母组合。

    为了涵盖这些,我们可以将此答案改编为相反的问题以使其打印而不是删除注释:

    perl -0777 -pe '
      s{
        (?<comment>
          # /* ... */ C comments
          / (?<lc> # line continuation
              (?<bs> # backslash in its regular or trigraph form
                \\ | \?\?/
              )
              (?: \n | \r\n?) # handling LF, CR and CRLF line delimiters
            )* \* .*? \* (?&lc)* /
          | / (?&lc)* / (?:(?&lc) | [^\r\n])* # // C++/C99 comments
        ) |
           "(?:(?&bs)(?&lc)*.|.)*?" # "strings" literals
           | '\''(?&lc)*(?:(?&bs)(?&lc)*(?:\?\?.|.))?(?:\?\?.|.)*?'\'' # (w)char literals
           | \?\?'\'' # trigraph form of ^
           | .[^'\''"/?]* # anything else
      }{$+{comment} eq "" ? "" : "$+{comment}\n"}exsg'
    

    在另一个问题中的人为示例中,涵盖了大多数极端情况:

    #include <stdio.h>
    int main()
    {
      printf("%d %s %s %c%c%c%c%c %s %s %d\n",
      1-/* comment */-1,
      /\
    * comment */
      "/* not a comment */",
      /* multiline
      comment */
      // comment
      /\
    / comment
      // multiline\
    comment
      "// not a comment",
      '"' /* comment */ , '"',
      '\'','"'/* comment */,
      '\
    \
    "', /* comment */
      "\\
    " /* not a comment */ ",
      "??/" /* not a comment */ ",
      '??''+'"' /* "comment" */);
      return 0;
    }
    

    给出:

    /* comment */
    /\
    * comment */
    /* multiline
      comment */
    // comment
    /\
    / comment
    // multiline\
    comment
    /* comment */
    /* comment */
    /* comment */
    /* "comment" */
    

    为了获取行号,因为我们在 slurp 模式下运行,其中主题是整个输入,而不是一次处理一行输入,所以有点棘手。我们可以通过使用(?{code})正则表达式运算符在每次找到行分隔符(C 中的 CR、LF 或 CRLF)时增加计数器来做到这一点:

    perl -0777 -pe '
      s{
        (?<comment>(?{$l=$n+1})
          /
          (?<lc>  # line continuation
            (?<bs> # backslash in its regular or trigraph form
              \\ | \?\?/
            ) (?<nl>(?:\n|\r\n?) (?{$n++})) # handling LF, CR and CRLF line delimiters
          )*
          (?:
            \* (?: (?&nl) | .)*? \* (?&lc)* / # /* ... */ C comments
            | / (?:(?&lc) | [^\r\n])*         # // C++/C99 comments
          )
        ) |
           "(?:(?&bs)(?&lc)*.|.)*?" # "strings" literals
           | '\''(?&lc)*(?:(?&bs)(?&lc)*(?:\?\?.|.))?(?:\?\?.|.)*?'\'' # (w)char literals
           | \?\?'\'' # trigraph form of ^
           | (?&nl)
           | .[^'\''"/?\r\n]* # anything else
      }{$+{comment} eq "" ? "" : sprintf("%5d %s\n", $l, $+{comment})}exsg'
    

    在同一个样本上给出:

        5 /* comment */
        6 /\
    * comment */
        9 /* multiline
      comment */
       11 // comment
       12 /\
    / comment
       14 // multiline\
    comment
       17 /* comment */
       18 /* comment */
       21 /* comment */
       26 /* "comment" */
    
    • 13
  3. Best Answer
    td211
    2024-01-05T14:19:19+08:002024-01-05T14:19:19+08:00

    可以按如下方式完成awk:

    #!/bin/awk
    
    # Handles case where both /* and */ are on the same line
    { line_printed = 0; }
    
    # Find the beginning of a multiline comment
    /^[[:space:]]*\/\*/ {
        multiline = 1;
    
        # Remove leading spaces
        sub(/^[[:space:]]+/,"");
        printf "[%d] %s\n", NR, $0;
        line_printed = 1;
    }
    
    # Find the end of a multiline comment
    /\*\/[[:space:]]*$/ {
        multiline = 0;
        if (line_printed == 0)
            printf "%s", $0;
    
        print "\n"
        next;
    }
    
    # The content between /* and */
    {
        if ( multiline == 1 && line_printed == 0 )
        {
            print $0;
            next
        }
    }
    
    # A single line comment
    /^[[:space:]]*\/\// {
        # Remove leading spaces
        sub(/^[[:space:]]+/,"");
        printf "[%d] %s\n\n", NR, $0;
    }
    

    将此脚本保存为foo.awk(或任何其他名称;扩展名是可选的),然后使用awk -f foo.awk input.c. 该脚本将打印所有注释(由额外的换行符分隔),并在每个注释之前添加行号。

    • 2
  4. Bog
    2024-01-05T15:56:24+08:002024-01-05T15:56:24+08:00

    好吧,绝对不是最奇特的,也不是最推荐的,因为它有一些缺陷。但我认为它看起来真的很酷:

    awk '/\/\//; /\/\*.*\*\//; /\*\//; /^\/\*/ { a=1 } /\*\// { a=0 } a && $0 != "/*" { print }'
    
    • 2

相关问题

  • 从 objdump 获取仅十六进制的输出

  • 使用 read() 读取 /proc/pid/maps

  • 堆什么时候用于动态内存分配?

  • GNU make dep 究竟做了什么?

  • 基于 OpenCV 的程序优化嵌入式 linux OS [关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve