How to remove comments of *c* programming language using javascript and php with Regex?
I am a front end developer, DSA solver
Regex is a language which is used to validate the syntax of particular match of characters and text using patterns.
JavaScript is a scripting language used as part of frontend functionality.
php is langauge used as backend server scripting language.
The code that we gonna be formatting with some good and bad commenting styles written in C language.
/*
* C Program to Detect the Cycle in a Linked List
*/
#include <stdio.h>
#include <stdlib.h>
/* structure for node / */
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void makecycle(struct node **);
void release(struct node **);
int detectcycle(struct node *);
/*
* main program */
*/
int main()
{
struct node *p = NULL;
int result;
printf("Enter data into the list\n");
create(&p);
makecycle(&p); //comment it to avoid cycle creation
printf("Identifying if a cycle exists.\n");
result = detectcycle(p);
if (result)
{
printf("Cycle detected in the linked list.\n");
}
else
{
printf("No cycle detected in the linked list.\n");
}
release (&p);
return 0;
}
/ Make cycles */
void makecycle(struct node **p)
{
struct node *rear, *front;
int n, count = 0, i;
front = rear = *p;
while (rear->next != NULL)
{
rear = rear->next;
count++;
}
if (count)
{
n = rand() % count;
}
else
{
n = 1;
}
for (i = 0; i < n - 1; i++)
{
front = front->next;
}
rear->next = front;
/*At this point a cycle is generated in the list*/
}
/* Detect cycle **
int detectcycle(struct node *head)
{
int flag = 1, count = 1, i;
struct node *p, *q;
p = q = head;
q = q->next;
while (1)
{
q = q->next;
if (flag)
{
p = p->next;
}
if (q == p)
{
/*Deleting the loop to deallocate the list*/
q = q->next;
while (q != p)
{
count++;
q = q->next;
}
q = p = head;
for (i = 0; i < count; i++)
{
q = q->next;
}
while (p != q)
{
p = p->next;
q = q->next;
}
q->next = NULL;
return 1;
}
else if (q->next == NULL)
{
return 0;
}
flag = !flag;
}
}
/* Creat the linked list
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
// Free the memory spae of the list
void release(struct node **head)
{
struct node *temp = *head;
temp = temp->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}
php code
<?php
// proper comments
$singleComment = "/(?:^|\s)\/\/(.+?)$/sm";// remove proper single line comments
$multilineComments = "/\/\*[^\/]+\*\//sm" ; // remove porper multiline comments
$patterns = array(
$singleComment=>'',
$multilineComments=>'',
);
// the code snippets where error has to be present
$no_end_slash = "/\/\*[\w\s]+\*{2}/s"; // remove comments ended with **
$text = "/\/\*(\s\w+)*/s"; // remove single line comment unended with * or /
$endRemover = "/\s\*{0,2}\//s"; // remove comment end statement with space **/
$multiComment = "/\/[^\/]+\*\//sm";
$errors = array(
$no_end_slash,
$text,
$endRemover,
$multiComment
);
// read the input/output file name in the present directory from user cmd line
$inputFileName = readLine("Enter a input file name:\t");
$outputFileName = readLine("Enter a output file name:\t");
$inputFileSize = filesize($inputFileName);
$fileContent = file_get_contents($inputFileName);
foreach ($patterns as $pattern => $replacement) {
$fileContent = preg_replace($pattern, $replacement, $fileContent);
}
for ($i = 0; $i < count($errors); $i++) {
preg_match_all($errors[$i], $fileContent, $matches);
echo "Errors matching Pattern " . ($i + 1) . ":\n";
// Iterate over the matches and echo them
foreach ($matches[0] as $match) {
echo $match . "\n";
}
}
file_put_contents($outputFileName,$fileContent);
?>
javascript code
const fs = require('fs');
// install prompt package using node
// node install prompt-sync
const prompt = require('prompt-sync')();
// field to take input
var inputFileName = prompt("Enter input file name in the present directory:\t");
var oututFileName = prompt("Enter output file name with extension:\t");
// Find invalid comments
function findInvalidComments(code, regexes) {
const invalidComments = [];
regexes.forEach(regex => {
const matches = code.match(regex) || [];
matches.forEach(comment => {
invalidComments.push(comment);
code = code.replace(regex, '')
});
});
return invalidComments;
}
function RemoveComments(code) {
const singleComment = /(?:^|\s)\/\/(.+?)$/gsm // remove proper single line comments
const multilineComments = /\/\*[^\/]+\*\//gsm// remove porper multiline comments
// replacing comments with blank
code = code.replace(singleComment, '')
code = code.replace(multilineComments, '');
return code
}
// read file with the content
fs.readFile(inputFileName, 'utf-8', (err, data) => {
if (err) {
throw err;
}
// clean the code
const cleanedCode = RemoveComments(data);
const multilineComments = /\/[^\/]+\*\//gsm // remove imporper multiline comments
const no_end_slash = /\/\*[\w\s]+\*{2}/gsm // remove comments ended with **
const text = /\/\*(\s\w+)*/gsm // remove single line comment unended with * or /
const endRemover = /\s\*{0,2}\//gsm // remove comment end statement with space **/
// invalid comment test
const regexes = [multilineComments, no_end_slash, text, endRemover];
const invalidComments = findInvalidComments(cleanedCode, regexes);
// Report invalid comments
if (invalidComments.length > 0) {
console.log("Invalid comments found:");
invalidComments.forEach(comment => {
console.log(comment);
});
} else {
console.log("No invalid comments found.");
}
// write the clean code to the file
fs.writeFile(oututFileName, cleanedCode, 'utf8', err => {
if (err) {
console.error('Error writing to file:', err);
return;
}
console.log('File updated successfully.');
});
})
At the end of run of program you will see that the comments from the c code both valid and invalid are removed.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void makecycle(struct node **);
void release(struct node **);
int detectcycle(struct node *);
int main()
{
struct node *p = NULL;
int result;
printf("Enter data into the list\n");
create(&p);
makecycle(&p);
printf("Identifying if a cycle exists.\n");
result = detectcycle(p);
if (result)
{
printf("Cycle detected in the linked list.\n");
}
else
{
printf("No cycle detected in the linked list.\n");
}
release (&p);
return 0;
}
void makecycle(struct node **p)
{
struct node *rear, *front;
int n, count = 0, i;
front = rear = *p;
while (rear->next != NULL)
{
rear = rear->next;
count++;
}
if (count)
{
n = rand() % count;
}
else
{
n = 1;
}
for (i = 0; i < n - 1; i++)
{
front = front->next;
}
rear->next = front;
}
int detectcycle(struct node *head)
{
int flag = 1, count = 1, i;
struct node *p, *q;
p = q = head;
q = q->next;
while (1)
{
q = q->next;
if (flag)
{
p = p->next;
}
if (q == p)
{
q = q->next;
while (q != p)
{
count++;
q = q->next;
}
q = p = head;
for (i = 0; i < count; i++)
{
q = q->next;
}
while (p != q)
{
p = p->next;
q = q->next;
}
q->next = NULL;
return 1;
}
else if (q->next == NULL)
{
return 0;
}
flag = !flag;
}
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void release(struct node **head)
{
struct node *temp = *head;
temp = temp->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}



