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

Compiler Lab

The document discusses three C programs - one to implement a lexical analyzer that ignores whitespace, comments and restricts identifier length, another to identify if a line is a comment, and a third to recognize strings matching regular expressions like 'a', 'a*b+', and 'abb'.

Uploaded by

Md Shahin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Compiler Lab

The document discusses three C programs - one to implement a lexical analyzer that ignores whitespace, comments and restricts identifier length, another to identify if a line is a comment, and a third to recognize strings matching regular expressions like 'a', 'a*b+', and 'abb'.

Uploaded by

Md Shahin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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. Simulate the same in C language.

Source Code 
main.cpp:
#include <stdio.h>
#include <ctype.h>

#define MAX_IDENTIFIER_LENGTH 50

// Function to check if a character is a valid identifier character


int is_valid_identifier_char(char c) {
return isalnum(c) || c == '_';
}

// Function to ignore comments until the end of line


void ignore_comment(FILE *file) {
int c;
while ((c = fgetc(file)) != EOF && c != '\n');
}

// Function to tokenize the input file


void tokenize(FILE *file) {
int c;
while ((c = fgetc(file)) != EOF) {
if (isspace(c)) { // Ignore spaces, tabs, and new lines
continue;
} else if (c == '/') {
int next_char = fgetc(file);
if (next_char == '/') {
ignore_comment(file); // Ignore single-line comments
continue;
} else if (next_char == '*') {
// Ignore multi-line comments
int prev_char = ' ';
while ((c = fgetc(file)) != EOF) {
if (prev_char == '*' && c == '/') {
break;
}
prev_char = c;
}
continue;
} else {
ungetc(next_char, file); // Not a comment, put the character back
}
} else if (is_valid_identifier_char(c)) {
char identifier[MAX_IDENTIFIER_LENGTH + 1] = {0};
int i = 0;
identifier[i++] = c;
while ((c = fgetc(file)) != EOF && i < MAX_IDENTIFIER_LENGTH && is_valid_identifier_char(c)) {
identifier[i++] = c;
}
printf("Identifier: %s\n", identifier);
continue;
} else {
// Handle other tokens as needed
}
}
}

iput.txt:
int main() {
FILE *file = fopen("input.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
tokenize(file);
fclose(file);
return 0;
}

Output 

2. Write a C program to identify whether a given line is a comment or not.

Source Code 
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isComment(char *line) {


// Check if line starts with "//" (for single-line comments) or "/*" (for multi-line comments)
return (strncmp(line, "//", 2) == 0 || strncmp(line, "/*", 2) == 0);
}
int main() {
char line[100];

printf("Enter a line: ");


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

if (isComment(line)) {
printf("The given line is a comment.\n");
} else {
printf("The given line is not a comment.\n");
}
return 0;
}

Output 

3. Write a C program to recognize strings under 'a', 'a*b+', 'abb'.

Source Code 
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool match_a(char *str) {


return (strlen(str) == 1 && str[0] == 'a');
}

bool match_ab_star(char *str) {


int len = strlen(str);
if (len == 0) return false;

// Check if the first character is 'a'


if (str[0] == 'a') {
// Check if the rest of the characters are 'b'
for (int i = 1; i < len; i++) {
if (str[i] != 'b') {
return false;
}
}
return true;
}
return false;
}

bool match_abb(char *str) {


return (strcmp(str, "abb") == 0);
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

if (match_a(str)) {
printf("The string matches 'a'.\n");
} else if (match_ab_star(str)) {
printf("The string matches 'a*b+'.\n");
} else if (match_abb(str)) {
printf("The string matches 'abb'.\n");
} else {
printf("The string does not match any of the given patterns.\n");
}
return 0;
}

Output 

You might also like