Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Lab Record

of

Compiler Design
(CSF401)

Submitted to: Submitted by:


Ms. Simran, Name: Ankush Negi
Associate Prof, Roll no : 200102302
School of Computing SAP ID : 1000015073
DIT University Section: E(P1)

Session 2023-24

Ankush Negi 1000015073 E-P1


Index
S. No Title of Experiment/Objective Signature
of Faculty

1. Write a program in C to count number of spaces in a line.

2. Write a program in C to count number of characters, spaces and digits


in a line.

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

4. Write a C program to recognize strings ‘a*’, ‘abb’, and ‘a*b+’.

5. Write a program in C language to test weather a given identifier is valid


or not.
6. Write a program in C language to test whether a given operator is
valid or not.
7. Install Flex for windows. Write a program to print whether the word is a
collection of lowercase or
upper case.
8. Write a program using Lex to print any arithmetic expression in the
form of tokens.
e.g. 2+4*3
9. Write a program using Lex to print any arithmetic expression in the
form of tokens.
e.g. a=b+c
10. Write a program using Lex to identify whether a letter is consonant or
vowel.
11. Design a simple calculator using Lex and Yacc.

Ankush Negi 1000015073 E-P1


Experiment – 1

Write a C program to count number of spaces in a line.

Code:

Ankush Negi 1000015073 E-P1


Experiment – 2

Write a C program to count number of character, space and digits in a line.

Code:

Ankush Negi 1000015073 E-P1


Experiment – 3
Write a program to identify whether any given line is a comment or not.

Code:

Outputs:

Ankush Negi 1000015073 E-P1


Experiment – 4
Write a C program to recognize strings under 'a*', 'a*b+', 'abb'.
CODE:

#include <stdio.h>

#include <string.h>

int matchAStar(char* input)

{ int i = 0;

while (input[i] == 'a')

{ i++;

return (input[i] == '\0');

int matchAStarBPlus(char* input)

{ int i = 0;

while (input[i] == 'a')

{ i++;

while (input[i] == 'b')

{ i++;

return (input[i] == '\0');

int matchAbb(char* input)

{ return (strcmp(input, "abb")


== 0);
}

int main() { char


input[100];
printf("Enter a string:
"); scanf("%s", input);

if (matchAStar(input)) {

Ankush Negi 1000015073 E-P1


printf("The string matches 'a*'.\n"); } else if
(matchAStarBPlus(input)) { printf("The string
matches 'a*b+'.\n");
} else if (matchAbb(input))
{ printf("The string matches 'abb'.\n");

} else {
printf("The string does not match any of the specified
patterns.\n"); }

return 0;
}

Outputs:

Ankush Negi 1000015073 E-P1


Experiment – 5

Write a C program to test whether a given identifier is valid or not.


CODE:
#include <stdio.h>

#include <stdbool.h>

#include <string.h>

#include <ctype.h>

bool isValidIdentifierChar(char ch)

{ return isalnum(ch) || ch == '_';

bool isValidIdentifier(const char* str)


{ if (strlen(str) == 0 || isdigit(str[0]))

{ return false;

}
for (int i = 0; str[i] != '\0'; i++)

{ if (!isValidIdentifierChar(str[i]))
{ return false;

char* keywords[] = {"auto", "break", "case", "char", "const", "continue", "default",

"do", "double", "else", "enum", "extern", "float", "for",

"goto", "if", "int", "long", "register", "return", "short",

"signed", "sizeof", "static", "struct", "switch", "typedef",

"union", "unsigned", "void", "volatile", "while"};

int numKeywords = sizeof(keywords) / sizeof(keywords[0]);

for (int i = 0; i < numKeywords; i++) {

if (strcmp(str, keywords[i]) == 0)

{ return false;

Ankush Negi 1000015073 E-P1


}

return true;

int main() {

char identifier[100];

printf("Enter an identifier:

"); scanf("%s", identifier);

if (isValidIdentifier(identifier)) {

printf("'%s' is a valid C identifier.\n", identifier);

} else {

printf("'%s' is not a valid C identifier.\n", identifier);

return 0;

Outputs:

Ankush Negi 1000015073 E-P1


Experiment – 6
Write a C program to test whether a given operator is valid or not.
Code:
#include <stdio.h>

#include <stdbool.h>

bool isValidOperator(char op) {

char validOperators[] = {'+', '-', '*', '/', '%', '=', '<', '>', '!', '&', '|', '^', '~', '?'};

for (int i = 0; i < sizeof(validOperators) / sizeof(validOperators[0]); ++i)

{ if (op == validOperators[i]) {

return true;

return false;

int main() {

char operator;

printf("Enter an operator:

"); scanf(" %c", &operator);

if (isValidOperator(operator)) {

printf("%c is a valid operator in C.\n", operator);

} else {

printf("%c is not a valid operator in C.\n", operator);

return 0;

Ankush Negi 1000015073 E-P1


Experiment – 7
Install Flex for windows. Write a program to print whether the word is a
collection of lowercase or upper case.
Code:
%option noyywrap
%{
#include<stdio.h>
int upper=0;
int lower=0;
%}
%%
[A-Z] {upper++;} {printf("Upper ");}
[a-z] {lower++;} {printf("Lower ");}
[\n] {printf("\nNumber of characters: %d %d",upper,lower);}
%%
int main()
{
printf("Enter Input:");
yylex();
return 0;
}

Output:

Ankush Negi 1000015073 E-P1


Experiment – 8
Write a program using Lex to print any arithmetic expression in the form
of tokens. e.g. 2+4*3
Code:
%{

#include <stdio.h>

%}

DIGIT [0-9]

WS [ \t\n]

OPERATOR

[+-/*]

%%

{DIGIT}+ { printf("NUMBER: %s\n", yytext); }

{OPERATOR} { printf("OPERATOR: %s\n", yytext); }

{WS} ; /* skip whitespace */

. { printf("INVALID CHARACTER: %s\n", yytext); }

%%

int main()

{ yylex();

return 0;

Output:

Ankush Negi 1000015073 E-P1


Experiment – 9
Write a program using Lex to print any arithmetic expression in the form
of tokens. e.g. a=b+c

Code:
%{

#include <stdio.h>

%}

%%

[ \t\n] ; /* skip whitespace */

[a-zA-Z][a-zA-Z0-9]* { printf("IDENTIFIER: %s\n", yytext); }

= { printf("ASSIGNMENT OPERATOR: %s\n", yytext);

} [0-9]+ { printf("NUMBER: %s\n", yytext); }

[+\-*/] { printf("OPERATOR: %s\n", yytext); }

. { printf("INVALID CHARACTER: %s\n", yytext); }

%%

int main()

{ yylex();

return 0;

Output:

Ankush Negi 1000015073 E-P1


Experiment – 10
Write a program using Lex to identify whether a letter is consonant or
vowel.
Code:
%{

#include <stdio.h>

%}

%%

[aeiouAEIOU] { printf("%s is a VOWEL\n", yytext); } [a-

zA-Z] { printf("%s is a CONSONANT\n", yytext);

. { printf("%s is not a valid letter\n", yytext); }

%%

int main()

{ yylex();

return 0;

Output:

Ankush Negi 1000015073 E-P1


Experiment – 11
Design a simple calculator using Lex and Yacc.
Code:
%{

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

float num;

int op;

%}

%%

digit [0-9]

op "+" | "-" | "*" | "/"

%%

{digit} {

num = atof(yytext);

if (op == 0) {

printf("%f ", num);

} else {

switch (op)

{ case '+':

num += atof(yytext);

break;

case '-':

num -=

atof(yytext); break;

case '*':

num *= atof(yytext);

break;

case '/':

Ankush Negi 1000015073 E-P1


if (atof(yytext) == 0) {

printf("Error: Division by zero.\

n"); exit(1);

} else {

num /= atof(yytext);

break;

default:

printf("Error: Invalid operator.\n");

exit(1);

{op} {

op = yytext[0];

.{

printf("\n");

Output:

Ankush Negi 1000015073 E-P1

You might also like