Compiler Design Lab
Compiler Design Lab
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
removeCommentLines(inputFile, outputFile);
return 0;
exit(1);
}
char line[MAX_LINE_LENGTH];
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() {
int lineCount = 0;
int punctuationCount = 0;
int spaceCount = 0;
countMetrics(filename, &lineCount, &punctuationCount, &spaceCount);
return 0;
void countMetrics(const char *filename, int *lineCount, int *punctuationCount, int *spaceCount) {
if (file == NULL) {
exit(1);
int ch;
int isNewLine = 1;
if (isNewLine) {
(*lineCount)++;
isNewLine = 0;
if (ispunct(ch)) {
(*punctuationCount)++;
}
if (isspace(ch)) {
(*spaceCount)++;
if (ch == '\n') {
isNewLine = 1;
fclose(file);
%{
#include <stdio.h>
%}
%%
[aAeEiIoOuU] { yylval++; }
%%
int main() {
yylex();
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
./vowel_counter
%{
#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();
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
./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 {
return 0;
return 1;
int yylex() {
int c = getchar();
if (c == 'A')
return A;
else if (c == 'B')
return B;
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
./string_validity
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NUMBER
%%
expression : NUMBER
;
%%
int main() {
yyparse();
return 0;
int yylex() {
int c = getchar();
if (isdigit(c)) {
yylval = c - '0';
return NUMBER;
return 0;
} else {
exit(1);
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
./expression_validity