How to resolve the algorithm Strip block comments step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Strip block comments step by step in the AWK programming language
Table of Contents
Problem Statement
A block comment begins with a beginning delimiter and ends with a ending delimiter, including the delimiters. These delimiters are often multi-character sequences.
Strip block comments from program text (of a programming language much like classic C).
Your demos should at least handle simple, non-nested and multi-line block comment delimiters.
The block comment delimiters are the two-character sequences:
Sample text for stripping: Ensure that the stripping code is not hard-coded to the particular delimiters described above, but instead allows the caller to specify them. (If your language supports them, optional parameters may be useful for this.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Strip block comments step by step in the AWK programming language
Source code in the awk programming language
# syntax: GAWK -f STRIP_BLOCK_COMMENTS.AWK filename
# source: https://www.gnu.org/software/gawk/manual/gawk.html#Plain-Getline
# Remove text between /* and */, inclusive
{ while ((start = index($0,"/*")) != 0) {
out = substr($0,1,start-1) # leading part of the string
rest = substr($0,start+2) # ... */ ...
while ((end = index(rest,"*/")) == 0) { # is */ in trailing part?
if (getline <= 0) { # get more text
printf("unexpected EOF or error: %s\n",ERRNO) >"/dev/stderr"
exit
}
rest = rest $0 # build up the line using string concatenation
}
rest = substr(rest,end+2) # remove comment
$0 = out rest # build up the output line using string concatenation
}
printf("%s\n",$0)
}
END {
exit(0)
}
You may also check:How to resolve the algorithm Palindrome dates step by step in the Lua programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Frink programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the CLU programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the Swift programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the ALGOL W programming language