Compilers || 3rd stage Shahad Ali
Compilers Lab - Lecture 3
Comments
#include <iostream>
#include <string>
using namespace std;
int main() {
string code = "int main() { // This is a single-line comment\n"
" /* This is a multi-line comment\n"
" that spans multiple lines */ return 0; }";
string comment;
for (int i = 0; i < code.length(); ++i) {
if (code[i] == '/') {
comment += code[i];
if (i + 1 < code.length() && code[i + 1] == '/') {
comment += code[i+1];
while (i < code.length() && code[i] != '\n') {
comment += code[++i];
}
cout << comment << "\n";
}
else if (i + 1 < code.length() && code[i + 1] == '*') {
comment += code[i + 1];
i++;
while (i < code.length() && !(code[i] == '*' && i + 1 <
code.length() && code[i + 1] == '/')) {
comment += code[i++];
}
comment += "*/";
cout << comment << "\n";
}
else {
comment.clear();
}
}
}
return 0;
}