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

Compiler Design Lab

The document provides code examples for writing programs in C, Lex, and Yacc to perform various text processing tasks like removing comments, counting lines, characters, punctuation, validating strings and expressions. These include programs to remove comment lines from a file, count lines, punctuation and spaces in a file, count vowels in a sentence, count lines, spaces and characters in a text, validate if a string has equal number of A and B characters, and validate expressions.

Uploaded by

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

Compiler Design Lab

The document provides code examples for writing programs in C, Lex, and Yacc to perform various text processing tasks like removing comments, counting lines, characters, punctuation, validating strings and expressions. These include programs to remove comment lines from a file, count lines, punctuation and spaces in a file, count vowels in a sentence, count lines, spaces and characters in a text, validate if a string has equal number of A and B characters, and validate expressions.

Uploaded by

red sparrow
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.Write a program in c to remove comment line.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE_LENGTH 1000

void removeCommentLines(const char *inputFile, const char *outputFile);

int main() {

const char *inputFile = "input.txt";

const char *outputFile = "output.txt";

removeCommentLines(inputFile, outputFile);

printf("Comment lines removed successfully.\n");

return 0;

void removeCommentLines(const char *inputFile, const char *outputFile) {

FILE *input = fopen(inputFile, "r");

FILE *output = fopen(outputFile, "w");

if (input == NULL || output == NULL) {

printf("Error opening file.\n");

exit(1);

}
char line[MAX_LINE_LENGTH];

while (fgets(line, sizeof(line), input) != NULL) {

// Check if the line starts with '//'

if (strncmp(line, "//", 2) != 0) {

// Line does not start with a comment, so write it to the output file

fputs(line, output);

fclose(input);

fclose(output);

2.Write a program in c to count the line no, number of punctuation symbol, no of space.

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

void countMetrics(const char *filename, int *lineCount, int *punctuationCount, int *spaceCount);

int main() {

const char *filename = "input.txt";

int lineCount = 0;

int punctuationCount = 0;

int spaceCount = 0;
countMetrics(filename, &lineCount, &punctuationCount, &spaceCount);

printf("Line Count: %d\n", lineCount);

printf("Punctuation Count: %d\n", punctuationCount);

printf("Space Count: %d\n", spaceCount);

return 0;

void countMetrics(const char *filename, int *lineCount, int *punctuationCount, int *spaceCount) {

FILE *file = fopen(filename, "r");

if (file == NULL) {

printf("Error opening file.\n");

exit(1);

int ch;

int isNewLine = 1;

while ((ch = fgetc(file)) != EOF) {

if (isNewLine) {

(*lineCount)++;

isNewLine = 0;

if (ispunct(ch)) {

(*punctuationCount)++;
}

if (isspace(ch)) {

(*spaceCount)++;

if (ch == '\n') {

isNewLine = 1;

fclose(file);

3.Write a program in lex to count vowel in a sentence.

%{

#include <stdio.h>

%}

%%

[aAeEiIoOuU] { yylval++; }

[ \t\n] ; /* Ignore whitespace characters */

. ; /* Ignore other characters */

%%
int main() {

yylex();

printf("Number of vowels: %d\n", yylval);

return 0;

Save the above code in a file named vowel_counter.l. Then, compile and run it using the Lex tool.

On Unix/Linux systems, you can use the following commands to compile and run the program:

lex vowel_counter.l

gcc lex.yy.c -o vowel_counter -ll

./vowel_counter

4.Write a program in lex to count line number, no of space, no of symbol, no of character.

%{

#include <stdio.h>

int lineCount = 0;

int spaceCount = 0;

int symbolCount = 0;

int charCount = 0;

%}

%option noyywrap

%%
\n { lineCount++; }

[] { spaceCount++; }

[a-zA-Z0-9] { charCount++; }

. { symbolCount++; charCount++; }

%%

int main() {

yylex();

printf("Line Count: %d\n", lineCount);

printf("Space Count: %d\n", spaceCount);

printf("Symbol Count: %d\n", symbolCount);

printf("Character Count: %d\n", charCount);

return 0;

int yywrap() {

return 1;

Save the above code in a file named text_metrics.l. Then, compile and run it using the Lex
tool.

On Unix/Linux systems, you can use the following commands to compile and run the
program:

lex text_metrics.l

gcc lex.yy.c -o text_metrics -ll

./text_metrics
5.Write a program in YACC to check the validity of the string consisting equal number of A and B.

%{

#include <stdio.h>

int countA = 0;

int countB = 0;

%}

%token A B

%start S

%%

S:ASB

|BSA

| /* Empty production */

A : 'A' { countA++; }

B : 'B' { countB++; }

%%
int main() {

yyparse();

if (countA == countB) {

printf("String is valid.\n");

} else {

printf("String is not valid.\n");

return 0;

int yyerror(const char* msg) {

printf("Error: %s\n", msg);

return 1;

int yylex() {

int c = getchar();

if (c == 'A')

return A;

else if (c == 'B')

return B;

else if (c == '\n' || c == EOF)

return 0;

else

yyerror("Invalid character");

return c;

}
Save the above code in a file named string_validity.y . Then, compile and run it using a C compiler
and the Yacc tool.

On Unix/Linux systems, you can use the following commands to compile and run the program:

yacc -d string_validity.y

gcc y.tab.c -o string_validity -ly -ll

./string_validity

6.Write a program in YACC to check the validity of expression.

%{

#include <stdio.h>

#include <stdlib.h>

%}

%token NUMBER

%left '+' '-'

%left '*' '/'

%%

expression : NUMBER

| expression '+' expression

| expression '-' expression

| expression '*' expression

| expression '/' expression

;
%%

int main() {

yyparse();

return 0;

int yylex() {

int c = getchar();

if (isdigit(c)) {

yylval = c - '0';

return NUMBER;

} else if (c == '\n' || c == EOF) {

return 0;

} else {

printf("Invalid character: '%c'\n", c);

exit(1);

int yyerror(const char* msg) {

printf("Error: %s\n", msg);

exit(1);

Save the above code in a file named expression_validity.y. Then, compile and run it using a C compiler
and the Yacc tool.
On Unix/Linux systems, you can use the following commands to compile and run the program:

Yacc -d expression_validity.y

Gcc y.tab.c -o expression_validity -ly -ll

./expression_validity

You might also like