Sie sind auf Seite 1von 9

**Program for Pass-1 of compiler**

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

// Constants

#define MAX_CODE_LENGTH 100

#define MAX_SYMBOL_TABLE_SIZE 50

#define MAX_LITERAL_TABLE_SIZE 50

// Token types

typedef enum {

TOKEN_IDENTIFIER,

TOKEN_LITERAL,

TOKEN_OPERATOR,

TOKEN_DELIMITER,

TOKEN_ERROR

} TokenType;

// Token structure

typedef struct {

TokenType type;

char lexeme[20];
} Token;

// Symbol table entry structure

typedef struct {

char name[20];

int address;

} SymbolEntry;

// Literal table entry structure

typedef struct {

char value[20];

int address;

} LiteralEntry;

// Pool table entry structure

typedef struct {

char value[20];

int address;

} PoolEntry;

// Function prototypes

void addToSymbolTable(char *lexeme, int address);

void addToLiteralTable(char *value, int address);

void addToPoolTable(char *value, int address);

void printSymbolTable();
void printLiteralTable();

void printPoolTable();

// Global variables

SymbolEntry symbolTable[MAX_SYMBOL_TABLE_SIZE];

int symbolTableSize = 0;

LiteralEntry literalTable[MAX_LITERAL_TABLE_SIZE];

int literalTableSize = 0;

PoolEntry poolTable[MAX_LITERAL_TABLE_SIZE];

int poolTableSize = 0;

int main() {

char code[MAX_CODE_LENGTH];

printf("Enter the source code: ");

fgets(code, MAX_CODE_LENGTH, stdin);

// Tokenize the source code

char *token = strtok(code, " \t\n");

while (token != NULL) {

Token currentToken;

if (isalpha(token[0])) {

currentToken.type = TOKEN_IDENTIFIER;

} else if (isdigit(token[0])) {
currentToken.type = TOKEN_LITERAL;

} else if (strchr("+-*/=", token[0]) != NULL) {

currentToken.type = TOKEN_OPERATOR;

} else if (strchr("();,", token[0]) != NULL) {

currentToken.type = TOKEN_DELIMITER;

} else {

currentToken.type = TOKEN_ERROR;

strcpy(currentToken.lexeme, token);

// Process token and update tables

switch (currentToken.type) {

case TOKEN_IDENTIFIER:

addToSymbolTable(currentToken.lexeme, 1000 + symbolTableSize); // Dummy address for


demonstration

break;

case TOKEN_LITERAL:

addToLiteralTable(currentToken.lexeme, 2000 + literalTableSize); // Dummy address for


demonstration

addToPoolTable(currentToken.lexeme, 3000 + poolTableSize); // Dummy address for


demonstration

break;

default:

break;

token = strtok(NULL, " \t\n");


}

// Print symbol, literal, and pool tables

printf("\nSymbol Table:\n");

printSymbolTable();

printf("\nLiteral Table:\n");

printLiteralTable();

printf("\nPool Table:\n");

printPoolTable();

return 0;

// Function to add an entry to the symbol table

void addToSymbolTable(char *lexeme, int address) {

if (symbolTableSize < MAX_SYMBOL_TABLE_SIZE) {

SymbolEntry entry;

strcpy(entry.name, lexeme);

entry.address = address;

symbolTable[symbolTableSize++] = entry;

} else {

printf("Symbol table full. Cannot add more entries.\n");

}
// Function to add an entry to the literal table

void addToLiteralTable(char *value, int address) {

if (literalTableSize < MAX_LITERAL_TABLE_SIZE) {

LiteralEntry entry;

strcpy(entry.value, value);

entry.address = address;

literalTable[literalTableSize++] = entry;

} else {

printf("Literal table full. Cannot add more entries.\n");

// Function to add an entry to the pool table

void addToPoolTable(char *value, int address) {

// Check if the value already exists in the pool table

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

if (strcmp(poolTable[i].value, value) == 0) {

return; // Value already exists, no need to add

// Add the value to the pool table

if (poolTableSize < MAX_LITERAL_TABLE_SIZE) {

PoolEntry entry;

strcpy(entry.value, value);
entry.address = address;

poolTable[poolTableSize++] = entry;

} else {

printf("Pool table full. Cannot add more entries.\n");

// Function to print the symbol table

void printSymbolTable() {

printf("Name\tAddress\n");

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

printf("%s\t%d\n", symbolTable[i].name, symbolTable[i].address);

// Function to print the literal table

void printLiteralTable() {

printf("Value\tAddress\n");

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

printf("%s\t%d\n", literalTable[i].value, literalTable[i].address);

// Function to print the pool table

void printPoolTable() {
printf("Value\tAddress\n");

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

printf("%s\t%d\n", poolTable[i].value, poolTable[i].address);

**Yogita Gajul - output**

Enter the source code: START 100 L1 READ A SUB AREG =5 BC GT L1 STOP A DS 1

Symbol Table:

Name Address

START 1000

L1 1001

READ 1002

A 1003

SUB 1004

AREG 1005

BC 1006

GT 1007

L1 1008

STOP 1009

A 1010

DS 1011

Literal Table:
Value Address

100 2000

1 2001

Pool Table:

Address

3001

3002

PS D:\c Language>

Das könnte Ihnen auch gefallen