LL1 Parser Implementation in C
LL1 Parser Implementation in C
#include<stdio.h>
#include<string.h>
struct production
{
char variable;
char value[10][100];
};
int main()
{
char variable[10];
char c;
int j=0;
printf("Enter the variables");
for(int i=0;i<100;i++)
{
c=getchar();
if(c=='\n');
else if(c!=EOF)
{
variable[j]=c;
j++;
}
else
{
variable[j]=-1;
j++;
break;
}
}
char terminal[10];
printf("Enter the terminals");
j=0;
for(int i=0;i<100;i++)
{
c=getchar();
if(c=='\n');
else if(c!=EOF)
{
terminal[i]=c;
j++;
}
else
{
terminal[i]=-1;
j++;
break;
}
}
int i=0;
while(variable[i]!=-1)
{
int j=0;
int k=0;
int lr=0;
int cr=0;
while(j==0)
{
struct production prod;
prod.variable=variable[i];
printf("Enter the production of %c",variable[i]);
scanf("%s",&prod.value[k]);
if(prod.variable!=prod.value[k][0])
{
printf("%c->%s\n",prod.variable,prod.value[k]);
lr=1;
}
printf("Enter 0 to enter another production of %c",variable[i]);
scanf("%d",&j);
if(lr==1)
{
cr++;
}
if(cr!=0)
{
int k=0;
if(prod.variable==prod.value[k][0])
{
printf("%c->%sU'\n",prod.variable,prod.value[k+1]);
printf("U->");
int l=1;
int len=strlen(prod.value[k]);
while(l!=len)
{
printf("%c",prod.value[k][l]);
l++;
}
printf("|e");
}
}
k++;
}
i++;
}
return 0;
}
Write a program in c to implement nfa to dfa .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
char NFA_FILE[MAX_LEN];
char buffer[MAX_LEN];
int zz = 0;
struct DFA {
char *states;
int count;
} dfa;
int last_index = 0;
FILE *fp; int symbols;
void reset(int ar[], int size) {
int i;
for (i = 0; i < size; i++) {
ar[i] = 0;
}
}
void check(int ar[], char S[]) {
int i, j;
int len = strlen(S);
for (i = 0; i < len; i++) {
j = ((int)(S[i]) - 65);
ar[j]++;
}
}
void state(int ar[], int size, char S[]) {
int j, k = 0;
for (j = 0; j < size; j++) {
if (ar[j] != 0)
S[k++] = (char)(65 + j);
}
if (strcmp(&NFA_TABLE[i][symbols], "-") != 0) {
strcpy(buffer, &NFA_TABLE[i][symbols]);
check(closure_ar, buffer);
int z = closure(closure_ar, states);
while (z != 100)
{
if (strcmp(&NFA_TABLE[z][symbols], "-") != 0) {
strcpy(buffer, &NFA_TABLE[z][symbols]);
check(closure_ar, buffer);
}
closure_ar[z]++;
z = closure(closure_ar, states);
}
}
printf("\n e-Closure (%c) :\t", (char)(65 + i));
if (strcmp(temp, "-") != 0) {
sz = strlen(temp);
g = 0;