You are on page 1of 7

COMPILER DESIGN LAB

EXPERIMENT 2

NAME: SURIYAA C

S REG NO:

21BAI1621 DATE:

22.02.2024

COURSE CODE: BCSE307P

COURSE NAME: COMPILER

DESIGN FACULTY NAME: INDIRA B


QUESTION 1:

USE LEX TOOL


IMPLEMENTATION OF LEXIAL ANALYZER TO RECOGNIZE
THE PATTERNS FOR THE GIVEN INPUT USING LEX TOOL

CODE:
%{
#include<stdio.h>
%}

/* Rules Section */
%%
[\t ]+ ;
if|else|while|int|switch|for|char|printf|scanf|return {printf("\n%s is KEYWORD",
yytext);}
[+]|[*]|[-]|[/]|[>]|[<]|[=] {printf("\n%s is OPERATOR", yytext);}
[0-9]+|[0-9]*\.[0-9]+ { printf("\n%s is NUMBER", yytext);}
[(]|[)]|[[]|[]] { printf("\n%s is Symbol", yytext);}
#.* { printf("\n%s is COMMENT", yytext);}
[a-z]|[A-Z]|[a-z]* { printf("\n%s is IDENTIFIER", yytext);}
\"[^ \"\n]*\" { printf("\n%s is STRING", yytext);}
\n { ECHO;}
%%[]'

/* User Subroutine section */


int main()
{
while( yylex());
}

int yywrap( )
{
return 1;
}
OUTPUT:
QUESTION 2:
CONSTRUCTION OF SYNTAX ANALYZER
IMPLEMENT A C PROGRAM FOR LL(1) TOP DOWN PARSER FOR ANY
GIVEN LL(1) GRAMMER

CODE:

//21BAI1621 SURIYAA CS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NON_TERMINALS 10
#define MAX_TERMINALS 10
#define MAX_PRODUCTIONS 10

int numNonTerminals = 0;
int numTerminals = 0;
int numProductions = 0;

char nonTerminals[MAX_NON_TERMINALS];
char terminals[MAX_TERMINALS];
char productions[MAX_PRODUCTIONS][20];

void computeFirst(char nonTerminal);


void computeFollow(char nonTerminal);

int main() {
printf("Enter the number of non-terminals: ");
scanf("%d", &numNonTerminals);

printf("Enter the non-terminals: ");


for (int i = 0; i < numNonTerminals; i++)
{ scanf(" %c", &nonTerminals[i]);
}

printf("Enter the number of terminals: ");


scanf("%d", &numTerminals);

printf("Enter the terminals: ");


for (int i = 0; i < numTerminals; i++)
{ scanf(" %c", &terminals[i]);
}

printf("Enter the number of productions: ");


scanf("%d", &numProductions);

printf("Enter the productions (in the form A->alpha): \n");


for (int i = 0; i < numProductions; i++) {
scanf(" %s", productions[i]);
}

printf("\nFirst sets:\n");
for (int i = 0; i < numNonTerminals; i++) {
computeFirst(nonTerminals[i]);
}

printf("\nFollow sets:\n");
computeFollow(nonTerminals[0]);

return 0;
}

void computeFirst(char nonTerminal)


{ printf("First(%c) = { ", nonTerminal);

for (int i = 0; i < numProductions; i++)


{ if (productions[i][0] ==
nonTerminal) {
if (productions[i][3] == '#')
{ printf("# ");
} else if (productions[i][3] >= 'A' && productions[i][3] <= 'Z') {
computeFirst(productions[i][3]);
} else {
printf("%c ", productions[i][3]);
}
}
}

printf("}\n");
}

void computeFollow(char nonTerminal)


{ printf("Follow(%c) = { ",
nonTerminal);

if (nonTerminal == productions[0][0]) {
printf("$ ");
}

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


char *pos = strchr(productions[i], nonTerminal);
if (pos != NULL) {
int index = pos - productions[i] +
1; if (productions[i][index] == '\0') {
if (productions[i][0] != nonTerminal) {
computeFollow(productions[i][0]);
}
} else if (productions[i][index] >= 'A' && productions[i][index] <= 'Z') {
computeFirst(productions[i][index]);
} else {
printf("%c ", productions[i][index]);
}
}
}

printf("}\n");
}
OUTPUT:

You might also like