Untitled
unknown
plain_text
2 years ago
8.8 kB
17
Indexable
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char type[20];
char value[256];
int section;
int position; // Position of the identifier in the section
int count;
} Token;
Token currToken;
int section_count = 0;
int token_position = 0;
// Structure to store identifiers and their positions in each section
typedef struct {
char identifier[256];
int section;
int position;
int count;
} IdentifierInfo;
IdentifierInfo identifiers[1000];
int identifier_count = 0;
int compareIdentifiers(const void *a, const void *b) {
int countDiff = ((IdentifierInfo*)b)->count - ((IdentifierInfo*)a)->count;
if(countDiff != 0) {
return countDiff;
}
// If the counts are same, order alphabetically
return strcmp(((IdentifierInfo *)a)->identifier, ((IdentifierInfo *)b)->identifier);
}
// Function to sort an array of identifiers section-wise and alphanumeric
void sortIdentifiers(IdentifierInfo *identifiers, int identifier_count) {
// Use the compareIdentifiers function to sort the array
qsort(identifiers, identifier_count, sizeof(IdentifierInfo), compareIdentifiers);
}
// Function to add an identifier to the list
void addIdentifier(const char *identifier, int section, int position) {
strcpy(identifiers[identifier_count].identifier, identifier);
identifiers[identifier_count].section = section;
identifiers[identifier_count].position = position;
identifier_count++;
}
void increaseIdentifierCount(int existingIndex) {
identifiers[existingIndex].count++;
}
// Function to check if an identifier already exists in the list
int identifierExists(const char *identifier) {
for (int i = 0; i < identifier_count; i++) {
if (strcmp(identifiers[i].identifier, identifier) == 0) {
return i; // Return the index if found
}
}
return -1; // Return -1 if not found
}
// Function to skip the rest of the current line
static int input(void);
void skipRestOfLine() {
int c;
while ((c = input()) != '\n' && c != EOF);
}
%}
%option noyywrap
%option yylineno
%%
Section1 {
if (section_count == 0) {
token_position = 1;
section_count++;
} else {
fprintf(stderr, "Error at Line %d: Unexpected Section1. Expected Section%d.\n", yylineno, section_count + 1);
return -1; // Return -1 to indicate an error
}
}
Section2 {
if (section_count == 1) {
token_position = 1;
section_count++;
} else {
fprintf(stderr, "Error at Line %d: Unexpected Section2. Expected Section%d.\n", yylineno, section_count + 1);
return -1; // Return -1 to indicate an error
}
}
Section3 {
if (section_count == 2) {
token_position = 1;
section_count++;
} else {
fprintf(stderr, "Error at Line %d: Unexpected Section3. Expected end of file.\n", yylineno);
// skipRestOfLine();
return -1; // Return -1 to indicate an error
}
}
[-]?[0-9]+[.][0-9]+ {
if(section_count == 0) {
fprintf(stderr, "Outside Sections!!\n");
return -1;
}
strcpy(currToken.type, "Float");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
}
[-]?[0-9]+ {
if(section_count == 0) {
fprintf(stderr, "Outside Sections!!\n");
return -1;
}
strcpy(currToken.type, "Number");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
}
[A-Za-z_][A-Za-z0-9_]* {
if(section_count == 0) {
fprintf(stderr, "Outside Sections!!\n");
}
int existingIndex = identifierExists(yytext);
if (existingIndex == -1 && section_count != 1) {
fprintf(stderr, "Error at Line %d: Identifier '%s' occurred afresh in Section%d.\n", yylineno, yytext, section_count);
addIdentifier(yytext, section_count, currToken.position);
existingIndex = identifierExists(yytext);
increaseIdentifierCount(existingIndex);
// return 1; // Return -1 to indicate an error
} else if(existingIndex == -1) {
addIdentifier(yytext, section_count, currToken.position);
existingIndex = identifierExists(yytext);
increaseIdentifierCount(existingIndex);
} else {
increaseIdentifierCount(existingIndex);
}
strcpy(currToken.type, "Identifier");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
currToken.count = identifiers[existingIndex].count;
return 1; // Return 1 to indicate a token was matched
}
[+\-*/=()] {
if(section_count == 0) {
fprintf(stderr, "Outside Sections!!\n");
// return -1;
}
strcpy(currToken.type, "Operator");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
}
[\t\n ]+ {
// Ignore whitespace
return 1; // Return 1 to indicate a token was matched
}
\[ {
if (section_count == 2) {
strcpy(currToken.type, "Separator");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
} else {
fprintf(stderr, "Error at Line %d: Unexpected '[' outside Section2.\n", yylineno);
// skipRestOfLine();
return -1; // Return -1 to indicate an error
}
}
\] {
if(section_count == 0) {
fprintf(stderr, "Outside Sections!!\n");
return -1;
}
if (section_count == 2) {
strcpy(currToken.type, "Separator");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
} else {
fprintf(stderr, "Error at Line %d: Unexpected ']' outside Section2.\n", yylineno);
// skipRestOfLine();
return -1; // Return -1 to indicate an error
}
}
\{ {
if (section_count == 2) {
strcpy(currToken.type, "Separator");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
} else {
fprintf(stderr, "Error at Line %d: Unexpected '{' outside Section2.\n", yylineno);
// skipRestOfLine();
return -1; // Return -1 to indicate an error
}
}
\} {
if (section_count == 2) {
strcpy(currToken.type, "Separator");
strcpy(currToken.value, yytext);
currToken.section = section_count;
currToken.position = token_position++;
return 1; // Return 1 to indicate a token was matched
} else {
fprintf(stderr, "Error at Line %d: Unexpected '}' outside Section2.\n", yylineno);
// skipRestOfLine();
return -1; // Return -1 to indicate an error
}
}
. {
fprintf(stderr, "Error at Line %d: Unexpected character '%s' in Section%d.\n", yylineno, yytext, section_count);
// skipRestOfLine();
return -1; // Return 1 to indicate a token was matched
}
%%
int main() {
int token;
int last = -1;
while ((token = yylex()) != 0) {
// Continue scanning until end of file
if (token == 1) {
// Print the current token information
if(last != currToken.position) {
printf("Type: %s, Value: %s, Section: %d, Position: %d, Count: %d\n", currToken.type, currToken.value, currToken.section, currToken.position, currToken.count);
}
last = currToken.position;
}
else if(token == -2) {
fprintf(stderr, "Section Order Not Maintained!!\n");
break;
}
}
if (section_count != 3) {
fprintf(stderr, "Error: Expected Section%d.\n", section_count + 1);
// exit(EXIT_FAILURE);
}
int maxFreq = 0;
for(int i = 0; i < identifier_count; i++) {
if(identifiers[i].count > maxFreq) {
maxFreq = identifiers[i].count;
}
}
// Sort identifiers and print them according to count
sortIdentifiers(identifiers, identifier_count);
printf("\nIdentifiers:\n");
for (int i = 0; i < identifier_count; i++) {
if(identifiers[i].count == maxFreq) {
printf("Identifier: %s, Section: %d, Position: %d, Count: %d\n", identifiers[i].identifier, identifiers[i].section, identifiers[i].position, identifiers[i].count);
}
}
return 0;
}
Editor is loading...
Leave a Comment