Professional Documents
Culture Documents
EXPERIMENT 2
NAME: SURIYAA C
S REG NO:
21BAI1621 DATE:
22.02.2024
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;}
%%[]'
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];
int main() {
printf("Enter the number of non-terminals: ");
scanf("%d", &numNonTerminals);
printf("\nFirst sets:\n");
for (int i = 0; i < numNonTerminals; i++) {
computeFirst(nonTerminals[i]);
}
printf("\nFollow sets:\n");
computeFollow(nonTerminals[0]);
return 0;
}
printf("}\n");
}
if (nonTerminal == productions[0][0]) {
printf("$ ");
}
printf("}\n");
}
OUTPUT: