Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【1.28】用awk去除C语言注释 #81

Open
vieyahn2017 opened this issue Jan 28, 2023 · 1 comment
Open

【1.28】用awk去除C语言注释 #81

vieyahn2017 opened this issue Jan 28, 2023 · 1 comment

Comments

@vieyahn2017
Copy link
Owner

vieyahn2017 commented Jan 28, 2023

# filename: strip_c_comment.awk  
# issue: awk -f scrip_c_comment.awk test.c  
BEGIN { FS="" }  
!(ignore_line && $NF == "\\"&& !ignore_line-- {  
    ignore_line = 0;  
    for(i = 1; i <= NF; i++) {  
        if (ignore_block) {  
            if ($i $(i+1) == "*/") {  
                ignore_block = 0  
                i++ # remove '*' 
            }  
            continue 
        }  
        if (!instr && $i $(i+1) == "/*") {  
            ignore_block = 1  
            i++ # remove '/' 
            continue 
        }  
        if (!instr && $i $(i+1) == "//") {  
            ignore_line = ($NF == "\\")? 1: 0  
            break  
        }  
        if ($i == "\"") {  
            instr = 1 - instr  
        }  
        printf($i)  
    }  
    printf("\n")  
} 

用awk去除C语言注释。
https://blog.51cto.com/onlyzq/546456

@vieyahn2017
Copy link
Owner Author

简单点的

调用预处理器 cpp 来过滤

sed 's/^#/@/' main.c | cpp | sed -e '/#/d' -e 's/@/#/' 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant