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

Compiler Design Lab2 Report

Uploaded by

MD Abu Rayhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Compiler Design Lab2 Report

Uploaded by

MD Abu Rayhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bangladesh University of Business & Technology

Lab Report : 02
Course Title : Compiler Design Lab
Course Code : CSE-324

Submitted By Submitted To
Name : MD ABU RAYHAN Name : Md. Saddam Hossain.
ID : 22235103446 Assistant Professor
Intake : 51 Department of CSE BUBT
Section : 10
Department of CSE

Date : Signature of the instructor

……………………………………….
Problem no 01 : Write a program that reads lines of code from the console and identifies:
• Single-line comments (//)
• Multi-line comments (/* ... */)
• Lines with no comments

Software Requirements : Vs Code

Source Code :

#include<bits/stdc++.h>
using namespace std;

void multiline(){
string s;
while (getline(cin,s))
{
for(int i=0;i<s.size()-1;i++){
if(s[i]=='*' && s[i+1]=='/'){
cout<<"Multiline Comment"<<endl;
return;
}
}
}

}
int main(){
string s;
while (1)
{
getline(cin,s);
int cnt=0;
for(int i=0;i<s.size()-1;i++){
if(s[i]=='/' && s[i+1]=='/'){
cout<<"Single Line Comment"<<endl;
cnt=1;
break;
}
else if(s[i]=='/' && s[i+1]=='*'){
multiline();
cnt=1;
break;
}
}
if(cnt==0) cout<<"No Comments"<<endl;
}

Output :

Conclusion : In this problem, we’ve learned how to identify whether a line is a single-line comment, a multi-
line comment, or has no comments.
Problem no 02 : Write a program that reads code from a input text le, identi es and counts single-line and
multi-line comments, and writes the results to a output text le.

Software Requirements : Vs Code

Source Code :

#include <bits/stdc++.h>
using namespace std;

int main() {
ifstream n("input.txt");
ofstream fout("output.txt");

string s;
int single_count = 0, multi_count = 0;

while (getline( n, s)) {


for (int i = 0; i < s.size() - 1; i++) {
if (s[i] == '/' && s[i + 1] == '/') {
single_count++;
break;
} else if (s[i] == '/' && s[i + 1] == '*') {
multi_count++;
break;
}
}
}

fout << "Single Line Comments: " << single_count << endl;
fout << "Multi Line Comments: " << multi_count << endl;

n.close();
fout.close();

return 0;
}

Input : Output:

Conclusion : In this problem, we’ve learned how to read code from an input text file, identify and count
single-line and multi-line comments, and write the results to an output text file.
fi
fi
fi
fi
fi
fi

You might also like