0% found this document useful (0 votes)
44 views

LL (1) Parsing Source Code

The program reads a grammar from a file and constructs the LL(1) parsing table. It calculates the FIRST, FOLLOW, and FIRST_RHS sets and uses these to populate the table. The table is then printed along with the grammar productions and derived sets.

Uploaded by

adam adam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

LL (1) Parsing Source Code

The program reads a grammar from a file and constructs the LL(1) parsing table. It calculates the FIRST, FOLLOW, and FIRST_RHS sets and uses these to populate the table. The table is then printed along with the grammar productions and derived sets.

Uploaded by

adam adam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LL(1) Parsing Table Program in C

The program reads grammar from a file “text.txt”.

“^” is used to represent epsilon.

The program assumes that the input grammar is LL(1).

text.txt

E->TA

A->+TA|^

T->FB

B->*FB|^

F->t|(E)

#include<stdio.h>

#include<string.h>

#define TSIZE 128

// table[i][j] stores

// the index of production that must be applied on

// ith varible if the input is

// jth nonterminal

int table[100][TSIZE];

// stores all list of terminals

// the ASCII value if use to index terminals

// terminal[i] = 1 means the character with

// ASCII value is a terminal


char terminal[TSIZE];

// stores all list of terminals

// only Upper case letters from 'A' to 'Z'

// can be nonterminals

// nonterminal[i] means ith alphabet is present as

// nonterminal is the grammar

char nonterminal[26];

// structure to hold each production

// str[] stores the production

// len is the length of production

struct product {

char str[100];

int len;

}pro[20];

// no of productions in form A->ß

int no_pro;

char first[26][TSIZE];

char follow[26][TSIZE];

// stores first of each production in form A->ß

char first_rhs[100][TSIZE];

// check if the symbol is nonterminal

int isNT(char c) {

return c >= 'A' && c <= 'Z';

// reading data from the file


void readFromFile() {

FILE* fptr;

fptr = fopen("text.txt", "r");

char buffer[255];

int i;

int j;

while (fgets(buffer, sizeof(buffer), fptr)) {

printf("%s", buffer);

j = 0;

nonterminal[buffer[0] - 'A'] = 1;

for (i = 0; i < strlen(buffer) - 1; ++i) {

if (buffer[i] == '|') {

++no_pro;

pro[no_pro - 1].str[j] = '\0';

pro[no_pro - 1].len = j;

pro[no_pro].str[0] = pro[no_pro - 1].str[0];

pro[no_pro].str[1] = pro[no_pro - 1].str[1];

pro[no_pro].str[2] = pro[no_pro - 1].str[2];

j = 3;

else {

pro[no_pro].str[j] = buffer[i];

++j;

if (!isNT(buffer[i]) && buffer[i] != '-' && buffer[i] != '>') {

terminal[buffer[i]] = 1;
}

pro[no_pro].len = j;

++no_pro;

void add_FIRST_A_to_FOLLOW_B(char A, char B) {

int i;

for (i = 0; i < TSIZE; ++i) {

if (i != '^')

follow[B - 'A'][i] = follow[B - 'A'][i] || first[A - 'A'][i];

void add_FOLLOW_A_to_FOLLOW_B(char A, char B) {

int i;

for (i = 0; i < TSIZE; ++i) {

if (i != '^')

follow[B - 'A'][i] = follow[B - 'A'][i] || follow[A - 'A'][i];

void FOLLOW() {

int t = 0;

int i, j, k, x;

while (t++ < no_pro) {


for (k = 0; k < 26; ++k) {

if (!nonterminal[k]) continue;

char nt = k + 'A';

for (i = 0; i < no_pro; ++i) {

for (j = 3; j < pro[i].len; ++j) {

if (nt == pro[i].str[j]) {

for (x = j + 1; x < pro[i].len; ++x) {

char sc = pro[i].str[x];

if (isNT(sc)) {

add_FIRST_A_to_FOLLOW_B(sc, nt);

if (first[sc - 'A']['^'])

continue;

else {

follow[nt - 'A'][sc] = 1;

break;

if (x == pro[i].len)

add_FOLLOW_A_to_FOLLOW_B(pro[i].str[0], nt);

}
}

void add_FIRST_A_to_FIRST_B(char A, char B) {

int i;

for (i = 0; i < TSIZE; ++i) {

if (i != '^') {

first[B - 'A'][i] = first[A - 'A'][i] || first[B - 'A'][i];

void FIRST() {

int i, j;

int t = 0;

while (t < no_pro) {

for (i = 0; i < no_pro; ++i) {

for (j = 3; j < pro[i].len; ++j) {

char sc = pro[i].str[j];

if (isNT(sc)) {

add_FIRST_A_to_FIRST_B(sc, pro[i].str[0]);

if (first[sc - 'A']['^'])

continue;

else {

first[pro[i].str[0] - 'A'][sc] = 1;

break;
}

if (j == pro[i].len)

first[pro[i].str[0] - 'A']['^'] = 1;

++t;

void add_FIRST_A_to_FIRST_RHS__B(char A, int B) {

int i;

for (i = 0; i < TSIZE; ++i) {

if (i != '^')

first_rhs[B][i] = first[A - 'A'][i] || first_rhs[B][i];

// Calculates FIRST(ß) for each A->ß

void FIRST_RHS() {

int i, j;

int t = 0;

while (t < no_pro) {

for (i = 0; i < no_pro; ++i) {

for (j = 3; j < pro[i].len; ++j) {

char sc = pro[i].str[j];

if (isNT(sc)) {

add_FIRST_A_to_FIRST_RHS__B(sc, i);

if (first[sc - 'A']['^'])
continue;

else {

first_rhs[i][sc] = 1;

break;

if (j == pro[i].len)

first_rhs[i]['^'] = 1;

++t;

int main() {

readFromFile();

follow[pro[0].str[0] - 'A']['$'] = 1;

FIRST();

FOLLOW();

FIRST_RHS();

int i, j, k;

// display first of each variable

printf("\n");

for (i = 0; i < no_pro; ++i) {

if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {


char c = pro[i].str[0];

printf("FIRST OF %c: ", c);

for (j = 0; j < TSIZE; ++j) {

if (first[c - 'A'][j]) {

printf("%c ", j);

printf("\n");

// display follow of each variable

printf("\n");

for (i = 0; i < no_pro; ++i) {

if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {

char c = pro[i].str[0];

printf("FOLLOW OF %c: ", c);

for (j = 0; j < TSIZE; ++j) {

if (follow[c - 'A'][j]) {

printf("%c ", j);

printf("\n");

}
// display first of each variable ß

// in form A->ß

printf("\n");

for (i = 0; i < no_pro; ++i) {

printf("FIRST OF %s: ", pro[i].str);

for (j = 0; j < TSIZE; ++j) {

if (first_rhs[i][j]) {

printf("%c ", j);

printf("\n");

// the parse table contains '$'

// set terminal['$'] = 1

// to include '$' in the parse table

terminal['$'] = 1;

// the parse table do not read '^'

// as input

// so we set terminal['^'] = 0

// to remove '^' from terminals

terminal['^'] = 0;

// printing parse table


printf("\n");

printf("\n\t**************** LL(1) PARSING TABLE *******************\n");

printf("\t--------------------------------------------------------\n");

printf("%-10s", "");

for (i = 0; i < TSIZE; ++i) {

if (terminal[i]) printf("%-10c", i);

printf("\n");

int p = 0;

for (i = 0; i < no_pro; ++i) {

if (i != 0 && (pro[i].str[0] != pro[i - 1].str[0]))

p = p + 1;

for (j = 0; j < TSIZE; ++j) {

if (first_rhs[i][j] && j != '^') {

table[p][j] = i + 1;

else if (first_rhs[i]['^']) {

for (k = 0; k < TSIZE; ++k) {

if (follow[pro[i].str[0] - 'A'][k]) {

table[p][k] = i + 1;

}
k = 0;

for (i = 0; i < no_pro; ++i) {

if (i == 0 || (pro[i - 1].str[0] != pro[i].str[0])) {

printf("%-10c", pro[i].str[0]);

for (j = 0; j < TSIZE; ++j) {

if (table[k][j]) {

printf("%-10s", pro[table[k][j] - 1].str);

else if (terminal[j]) {

printf("%-10s", "");

++k;

printf("\n");

You might also like