CD C Programs
CD C Programs
lexical analyzer:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char input[MAX];
printf("Enter a line of code: ");
fgets(input, MAX, stdin);
analyzeLine(input);
return 0;
}
Key Improvements:
Sample Run:
Input:
Output:
Keyword: int
Identifier: a
Operator: =
Constant: 10
Comment: // Initialize
This version retains functionality while being shorter and more readable.
1b.
Here's a simplified C program for the implementation of a Symbol Table. This program uses an
array to store symbols and their associated information, and allows basic operations like insertion
and display.
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50];
char type[20];
int size;
} Symbol;
Symbol symbolTable[MAX];
int symbolCount = 0;
int main() {
int choice;
char name[50], type[20];
int size;
do {
printf("\n1. Insert Symbol\n2. Display Symbol Table\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter symbol name: ");
scanf("%s", name);
printf("Enter symbol type: ");
scanf("%s", type);
printf("Enter symbol size: ");
scanf("%d", &size);
insertSymbol(name, type, size);
break;
case 2:
displaySymbolTable();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
}
Explanation:
Sample Run:
Input:
1
Enter symbol name: x
Enter symbol type: int
Enter symbol size: 4
1
Enter symbol name: y
Enter symbol type: float
Enter symbol size: 8
2
Output:
Symbol Table:
Name Type Size
-----------------------
x int 4
y float 8
This implementation is simple and demonstrates the concept of a symbol table effectively.
Here’s a simplified and short C program for a Symbol Table that supports basic operations like
Insert, Search, and Display.
Short C Program:
#include <stdio.h>
#include <string.h>
typedef struct {
char name[50], type[20];
int size;
} Symbol;
Symbol symbolTable[MAX];
int symbolCount = 0;
int main() {
char name[50], type[20];
int size, choice;
while (1) {
printf("\n1. Insert Symbol\n2. Search Symbol\n3. Display Symbol
Table\n4. Exit\nChoice: ");
scanf("%d", &choice);
if (choice == 4) break;
if (choice == 1) {
printf("Enter name, type, size: ");
scanf("%s %s %d", name, type, &size);
insertSymbol(name, type, size);
} else if (choice == 2) {
printf("Enter symbol name to search: ");
scanf("%s", name);
searchSymbol(name);
} else if (choice == 3) {
displaySymbolTable();
} else {
printf("Invalid choice!\n");
}
}
return 0;
}
Features:
1. Insert:
o Adds a new symbol if it doesn't exist.
o Checks for duplicate entries.
2. Search:
o Looks up a symbol by name and displays its details if found.
3. Display:
o Shows all symbols in a tabular format.
4. Menu-Driven Interface:
o Allows user to select operations interactively.
Sample Run:
Input:
1
x int 4
1
y float 8
2
x
3
4
Output:
Symbol Table:
Name Type Size
-----------------------
x int 4
y float 8
This short program effectively demonstrates multiple symbol table operations while keeping the
implementation simple and concise.
Here’s a very short and simplified C program to construct an LL(1) Parser. This example
assumes a basic grammar, such as:
S -> aA
A -> b | ε
char input[100];
int pos = 0;
// Function prototypes
void S();
void A();
void S() {
if (input[pos] == 'a') {
pos++;
A();
} else {
printf("Error: Expected 'a'\n");
}
}
void A() {
if (input[pos] == 'b') {
pos++;
} else if (input[pos] == '\0') {
// ε (epsilon) production, do nothing
return;
} else {
printf("Error: Expected 'b' or ε\n");
}
}
int main() {
printf("Enter input string: ");
scanf("%s", input);
if (input[pos] == '\0') {
printf("Input string is accepted.\n");
} else {
printf("Error: Unexpected characters at end of input.\n");
}
return 0;
}
Explanation:
1. Grammar:
o The grammar used is:
o S -> aA
o A -> b | ε
o It accepts strings like a, ab.
2. Structure:
o S() and A() functions represent the non-terminals S and A in the grammar.
o The pos variable tracks the current position in the input string.
3. Parser Workflow:
o The S() function ensures the input starts with a and then calls A().
o The A() function checks for b or epsilon (represented by the end of the string).
4. Error Handling:
o Prints an error message if the input doesn't match the expected production.
5. Acceptance:
o If the parser finishes and reaches the end of the string (\0), the input is accepted.
Sample Run:
Input:
ab
Output:
Input:
aa
Output:
This simple program demonstrates an LL(1) parser for a basic grammar in a compact way.
Program:
#include <stdio.h>
void constantFolding() {
int x = 5 * 10; // Constant Folding
printf("Constant Folding Result: %d\n", x);
}
void deadCodeElimination() {
int a = 10, b = 20;
if (a < b) {
printf("Dead Code Eliminated: a < b\n");
}
// The following code is unreachable and removed during optimization
// printf("This line is dead code\n");
}
void strengthReduction() {
int x = 8;
int y = x * 2; // Before Optimization
int z = x << 1; // After Optimization (Strength Reduction)
printf("Strength Reduction: %d (Before: %d, Optimized: %d)\n", x, y, z);
}
int main() {
constantFolding();
deadCodeElimination();
strengthReduction();
return 0;
}
Key Techniques:
1. Constant Folding:
o Evaluate constant expressions at compile time.
o Example: 5 * 10 is precomputed to 50.
2. Dead Code Elimination:
o Remove code that will never be executed.
o Example: Unreachable printf statement removed.
3. Strength Reduction:
o Replace expensive operations (e.g., multiplication) with cheaper ones (e.g., bit
shifts).
o Example: x * 2 optimized to x << 1.
Output:
Constant Folding Result: 50
Dead Code Eliminated: a < b
Strength Reduction: 8 (Before: 16, Optimized: 16)
This short program provides a simple illustration of common code optimization techniques in C.
C Program:
#include <stdio.h>
#include <string.h>
int main() {
char expression[20];
printf("Enter a simple expression (e.g., a = b + c): ");
scanf("%[^\n]%*c", expression);
generateAssembly(expression);
return 0;
}
Input Format:
Example Run:
Input:
a = b + c
Output:
Input:
x = y * z
Output:
1. Input Parsing:
o Assumes the input is in the form a = b op c, where op can be +, -, *, or /.
2. Assembly Generation:
o Uses MOV to load and store values.
o Uses basic instructions (ADD, SUB, MUL, DIV) for operations.
3. Output:
o The assembly-like code is printed for the given expression.
This program provides a simple way to demonstrate the concept of intermediate to assembly
code translation for arithmetic expressions.