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

Compiler Design Task1

The document outlines two practical tasks involving C programming. The first task is to design a lexical analyzer that ignores redundant spaces, tabs, new lines, and comments, while identifying keywords and numbers in a given C program. The second task is to write a program that determines if a given line of code is a comment, either single-line or multi-line.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Compiler Design Task1

The document outlines two practical tasks involving C programming. The first task is to design a lexical analyzer that ignores redundant spaces, tabs, new lines, and comments, while identifying keywords and numbers in a given C program. The second task is to write a program that determines if a given line of code is a comment, either single-line or multi-line.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Index:

Sr.no. Task Page no. Remarks


Practical 1
Aim: Design a lexical analyzer for given language and the lexical analyzer should ignore redundant
spaces, tabs and new lines. It should also ignore comments. Although the syntax specification states
that identifiers can be arbitrarily long, you may restrict the length to some reasonable value.
Simulates the same in C language.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void keyword(char str[10]) {


if (strcmp("for", str) == 0 || strcmp("while", str) == 0 || strcmp("do", str) == 0 ||
strcmp("int", str) == 0 || strcmp("float", str) == 0 || strcmp("char", str) == 0 ||
strcmp("double", str) == 0 || strcmp("static", str) == 0 || strcmp("switch", str) == 0 ||
strcmp("case", str) == 0 || strcmp("return", str) == 0 || strcmp("void", str) == 0)
{ printf("\n%s is a keyword", str);
} else {
printf("\n%s is an identifier", str);
}
}

int main() {
char code[1000], str[10], c;
int num[100], lineno = 1, tokenvalue = 0, i = 0, j = 0, k = 0;

printf("Enter the C program (End with #):\n");

// Read input from user


int index = 0;
while ((c = getchar()) != '#') { // Using '#' as EOF (since online compilers don't support Ctrl+Z)
code[index++] = c;
}
code[index] = '\0'; // Null-terminate the string printf("\nProcessing...\

n");

for (int pos = 0; code[pos] != '\0'; pos++) {


c = code[pos];

// Handling numbers
if (isdigit(c)) {
tokenvalue = c - '0';
while (isdigit(code[++pos])) {
tokenvalue = tokenvalue * 10 + (code[pos] - '0');
Page | 1
}
num[i++] = tokenvalue;
pos--; // Step back to process next character
}

// Handling identifiers and keywords


else if (isalpha(c)) {
k = 0;
while (isalnum(c) || c == '_' || c == '$')
{ str[k++] = c;
c = code[++pos];
}
str[k] = '\0'; // Null terminate the token
keyword(str);
pos--; // Step back
}

// Handling line count


else if (c == '\n') {
lineno++;
}
}

// Output results
printf("\nThe numbers in the program are: ");
for (j = 0; j < i; j++)
printf("%d ", num[j]);

printf("\nTotal number of lines: %d\n", lineno);

return 0;
}

Page | 2
Output:

Page | 3
Practical 2
Aim: WAP to identify whether the given line is comment or not.

#include <stdio.h>
#include <string.h>
int main() {
char line[200];

printf("Enter a line of code: ");


fgets(line, sizeof(line), stdin);

// Checking for single-line comment


if (line[0] == '/' && line[1] == '/') {
printf("It is a single-line comment.\n");
}
// Checking for multi-line comment
else if (line[0] == '/' && line[1] == '*') {
if (strstr(line, "*/") != NULL) {
printf("It is a multi-line comment.\n");
} else {
printf("It is an incomplete multi-line comment.\n");
}
}
else {
printf("It is NOT a comment.\n");
}

return 0;
}

Output:

Page | 4

You might also like