Expl Prgrms
Expl Prgrms
h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char keywords[32][10] = {
"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else",
"enum",
"extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed",
};
break;
int main() {
char ch, buffer[15], operators[] = "+-*/%=", specialch[] = ",;[]{}", num[] = "1234567890",
buf[10];
printf("error while opening file\n"); // Print error if file could not be opened
// Loop through each character in file until end of file (EOF) is reached
else {
void main() {
char c, str[10]; // `c` stores each character, `str` stores sequences of letters/numbers for
keywords/identifiers
int lineno = 1, num = 0, i = 0; // `lineno` for line number count, `num` for numeric values,
`i` for indexing in `str`
// Loop through each character in the file until End Of File (EOF) is reached
ungetc(c, f1); // Push last character back to stream for next processing
strcmp("case", str) == 0) {
} else {
ungetc(c, f1); // Push the last character back to stream for next processing
else
printf("\nTotal no of lines are: %d\n", lineno - 1); // Print the total number of lines in the
file
char ip_sym[MAX], op[MAX], tmp[MAX]; // `ip_sym` stores input, `op` stores production state,
`tmp` is a temporary buffer
void e_prime() {
int i, n = 0;
for (i = 0; i <= strlen(op); i++) // Loop to remove 'e' characters from `op`
int i = n + 2;
do { op[i + 2] = op[i]; i--; } while (i >= len); // Shift contents to insert "+TE'"
op[n++] = '+'; op[n++] = 'T'; op[n++] = 'E'; op[n++] = 39; // Add "+TE'"
t(); // Parse T
} else {
void t() {
int i, n = 0;
i = n + 1;
do { op[i + 2] = op[i]; i++; } while (i < len); // Shift to make space for "FT'"
f(); // Parse F
void t_prime() {
int i, n = 0;
int i = n + 2;
op[n++] = '*'; op[n++] = 'F'; op[n++] = 'T'; op[n++] = 39; // Add "*FT'"
f(); // Parse F
} else {
void f() {
int i, n = 0, l;
strcpy(op, tmp);
l = strlen(op);
} else {
advance();
advance();
i = n + 2;
} else {
void advance() {
ip_ptr++;
}
int main() {
} else {
#include<stdio.h> // Include the standard input/output library for functions like printf.
int z=0,i=0,j=0,c=0; // Declare global integer variables: z, i, j, and c for indexing and control.
char a[16],ac[2],stk[15],act[10]; // Declare character arrays a, ac, stk, and act to store input
string, action, stack, and action description.
printf("\n$%s\t%s$\t",stk,a); // Print the current state of the stack and the remaining
input.
for(z=0;z<c-2;z++) // Loop through the stack to check for the pattern '2E2' (used for the
production E->2E2).
for(z=0;z<c-2;z++) // Loop through the stack to check for the pattern '3E3' (used for the
production E->3E3).
{
printf("%s",act); // Print the current action (SHIFT).
a[j]=' '; // Remove the current input symbol from the input string.
check(); // Call the check function to see if any reduction can occur.
check(); // Call check function once more to ensure all reductions are done.
else
#include<stdio.h> // Include the standard input/output library for using printf and scanf.
#include<string.h> // Include the string manipulation library for functions like strlen.
#include<math.h> // Include the math library for using pow() to compute powers of numbers.
int ninputs; // Declare a global variable for the number of input symbols.
int state[10000] = {0}; // Array to keep track of visited states in the DFA.
int go[10000][2] = {0}; // Array to store transitions for each state and input symbol.
int main() {
int st, fin, in; int f[10]; // Declare variables for the number of states, final states, initial state,
and an array for final states.
int i, j = 3, s = 0, final = 0, flag = 0, curr1, curr2, k, I; // Declare counters and state variables.
// Prompt the user to follow one-based indexing and input number of states.
printf("\nGive state numbers from 0 to %d", st - 1); // Ask user to provide state numbers.
// Prompt for and read the number of final states and their values.
scanf("%d", &fin);
// Prompt for the number of transition rules and their input format.
scanf("%d%d%d", &p, &q, &r); // Read NFA transition rules (initial state, input symbol,
final state).
if (q == 0)
else
scanf("%d", &in);
i = 0;
int x = 0;
for (j = 0; j < 2; j++) { // Loop over each input symbol (0 and 1).
if (dfa[i][j][k] == 1) // If there is a transition from state `i` on input `j` to state `k`.
stf = stf + pow(2, k); // Update the new state based on binary representation.
go[(int)(pow(2, i))][i] = stf; // Store the new state in the transition table.
printf("%d-%d-->%d\n", (int)(pow(2, i)), j, stf); // Print the transition.
for (j = 0; j < 2; j++) { // Loop over each input symbol (0 and 1).
if (new == 0)
new = go[h][j]; // Set the new state for the input symbol.
printf("STATe 0 1\n");
int y = 0;
if (i == 0)
else
int x = 1 << j;
if (x & i) {
printf("%d %d", go[y][0], go[y][1]); // Print the transitions for the state.
printf("\n");
printf("\nEnter string::");
curr1 = go[curr1][str[i] - '0']; // Update the current state based on input symbol.
printf("\nFinal state-%d\n", curr1); // Print the final state after processing the string.
for (i = 0; i < fin; i++) { // Check if the final state is an accepting state.
if (curr1 & (1 << f[i])) { // If the final state is in the set of final states.
break;
if (flag)
else
#include<stdio.h> // Include the standard input/output library for using printf and scanf
functions.
#include<string.h> // Include the string manipulation library for functions like strcmp,
strcpy, etc.
#include<stdlib.h> // Include the standard library for functions like atoi (to convert strings
to integers).
#include<ctype.h> // Include the ctype library for functions like isdigit (to check if a
character is a digit).
void change(int p, char *res); // Declare the change function to replace variable values in
expressions.
void output(); // Declare the output function to display the final optimized code.
char op[2], op1[5], op2[5], res[5]; // Define fields for the operator and operands.
int flag; // Flag to indicate whether the expression has been evaluated or not.
int main() { // Define the main function to control the flow of the program.
input(); // Call the input function to get the list of expressions from the user.
void input() { // Define the input function to read expressions from the user.
printf("\n\nEnter the maximum number of expressions: "); // Prompt the user to input the
number of expressions.
scanf("%d", &n); // Read the number of expressions into n.
printf("\nEnter the input: \n"); // Prompt the user to enter the expressions.
arr[i].flag = 0; // Set the flag to 0, indicating that the expression hasn't been evaluated
yet.
int op1, op2, res; // Declare variables for the operands and result of the expression.
char op, res1[5]; // Declare a variable for the operator and the result as a string.
break;
break;
case '*': // If the operator is '*'.
break;
break;
break;
change(i, res1); // Call the change function to update other expressions with the
result.
void output() { // Define the output function to display the optimized code.
printf("\nOptimized code is: "); // Print the header for optimized code.
for(i = 0; i < n - 1; i++) { // Loop through the expressions (excluding the last one).
}
void change(int p, char *res) { // Define the change function to update the expressions.
for(i = p + 1; i < n; i++) { // Loop through the expressions after the one being updated.
strcpy(arr[i].op1, res); // Replace the first operand with the new result.
strcpy(arr[i].op2, res); // Replace the second operand with the new result.