0% found this document useful (0 votes)
3 views

Compilers Lab - Lecture 3

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Compilers Lab - Lecture 3

Uploaded by

shahadali.123987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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;
}

You might also like