cd-lab-ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1st and 2nd Program

#include <stdio.h>
#include <ctype.h>

void analyze(char *code) {


char *keywords[] = {"if", "else", "while", "for", "return"};
char buffer[50];
int i = 0, j = 0;

while (code[i] != '\0') {


if (isspace(code[i])) {
i++;
continue;
}

if (isalpha(code[i])) {
// Collect keyword or identifier
j = 0;
while (isalnum(code[i])) buffer[j++] = code[i++];
buffer[j] = '\0';
int isKeyword = 0;
for (int k = 0; k < 5; k++) {
if (strcmp(buffer, keywords[k]) == 0) {
printf("Keyword: %s\n", buffer);
isKeyword = 1;
break;
}
}
if (!isKeyword) printf("Identifier: %s\n", buffer);
} else if (isdigit(code[i])) {
// Collect number
j = 0;
while (isdigit(code[i])) buffer[j++] = code[i++];
buffer[j] = '\0';
printf("Number: %s\n", buffer);
} else if (strchr("+-*/=;", code[i])) {
// Operators and symbols
printf("Operator/Symbol: %c\n", code[i++]);
} else {
// Unknown character
printf("Unknown: %c\n", code[i++]);
}
}
}

int main() {
char code[256];
printf("Enter code: ");
fgets(code, 256, stdin);
analyze(code);
return 0;
3rd a)Program

#include <stdio.h>
#include <ctype.h>

int isValidExpression(const char* expr) {


for (int i = 0; expr[i] != '\0'; i++) {
if (!isdigit(expr[i]) && expr[i] != '+' && expr[i] != '-' &&
expr[i] != '*' && expr[i] != '/' && !isspace(expr[i])) {
return 0; // Invalid character found.
}
}
return 1; // All characters are valid.
}

int main() {
char expression[100];
printf("Enter an arithmetic expression: ");
fgets(expression, sizeof(expression), stdin);

if (isValidExpression(expression)) {
printf("The expression is valid.\n");
} else {
printf("The expression is invalid.\n");
}
return 0;
}

3rd b)

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int isValidVariable(char *name) {


// Check if the first character is valid (alphabet or underscore)
if (!isalpha(name[0]) && name[0] != '_')
return 0;

// Check the rest of the characters


for (int i = 1; name[i] != '\0'; i++) {
if (!isalnum(name[i]) && name[i] != '_')
return 0;
}
return 1;
}

int main() {
char variable[100];

printf("Enter a variable name: ");


scanf("%s", variable);
if (isValidVariable(variable))
printf("\"%s\" is a valid variable name.\n", variable);
else
printf("\"%s\" is not a valid variable name.\n", variable);

return 0;
}

3rd c)

#include <stdio.h>
#include <string.h>

int isValidIf(char *line) {


return strstr(line, "if (") != NULL && strstr(line, ") {") != NULL;
}

int isValidFor(char *line) {


return strstr(line, "for (") != NULL && strstr(line, ") {") != NULL;
}

int isValidWhile(char *line) {


return strstr(line, "while (") != NULL && strstr(line, ") {") != NULL;
}

void removeNewline(char *line) {


int len = strlen(line);
if (len > 0 && line[len - 1] == '\n') {
line[len - 1] = '\0';
}
}

int main() {
char input[100];

printf("Enter a line of code: ");


gets(input); // Use gets() for compatibility with Turbo C++
removeNewline(input);

if (isValidIf(input)) {
printf("Valid 'if' syntax.\n");
} else if (isValidFor(input)) {
printf("Valid 'for' syntax.\n");
} else if (isValidWhile(input)) {
printf("Valid 'while' syntax.\n");
} else {
printf("Invalid control structure syntax.\n");
}

return 0;
}
3rd d)Program

#include <stdio.h>

int main() {
char operator;
float num1, num2;

// User input for operation


printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);

// User input for numbers


printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);

// Switch statement to perform the calculation


switch (operator) {
case '+':
printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
break;
case '/':
// Check for division by zero
if (num2 != 0)
printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default:
printf("Invalid operator\n");
}

return 0;
}

4th Program
#include <stdio.h>

int main() {
int a = 5, b = 10, c, d;

// Simple expression: d = a + b * 2
printf("Generating TAC for the expression: d = a + b * 2\n");

// Step 1: Generate TAC


// t1 = b * 2
// t2 = a + t1
// d = t2

// Generate and print the three-address code


printf("t1 = b * 2\n"); // t1 = b * 2
printf("t2 = a + t1\n"); // t2 = a + t1
printf("d = t2\n"); // d = t2

// Execute the expression in C


c = b * 2; // t1 = b * 2
d = a + c; // t2 = a + t1, d = t2

// Output the result of the expression


printf("Result of the expression d = a + b * 2 is: d = %d\n", d);

return 0;
}

5th Program

#include <stdio.h>

int main() {
int num;
float decimal;
char letter;

// Asking user for input


printf("Enter an integer: ");
scanf("%d", &num);

printf("Enter a floating-point number: ");


scanf("%f", &decimal);

printf("Enter a character: ");


scanf(" %c", &letter); // Notice the space before %c to avoid newline character issues

// Checking the type using sizeof


printf("\nChecking the types of entered values:\n");

// Type checking
printf("The type of num is: %s\n", (sizeof(num) == sizeof(int)) ? "int" : "unknown");
printf("The type of decimal is: %s\n", (sizeof(decimal) == sizeof(float)) ? "float" : "unknown");
printf("The type of letter is: %s\n", (sizeof(letter) == sizeof(char)) ? "char" : "unknown");

return 0;
}

6th Program

check
lab manual is easy, brothers
7th Program

#include <stdio.h>
#include <string.h>

// Function to convert three-address code to 8086 assembly language


void generateAssembly(char* tac) {
char op, var1, var2, result;
int num1, num2;

// Parse TAC in the format: result = var1 op var2


if (sscanf(tac, "%c = %c %c %c", &result, &var1, &op, &var2) == 4) {
// Generate 8086 assembly code
switch(op) {
case '+':
printf("MOV AL, %c\n", var1); // Move var1 into AL
printf("ADD AL, %c\n", var2); // Add var2 to AL
printf("MOV %c, AL\n", result); // Store result in 'result'
break;
case '-':
printf("MOV AL, %c\n", var1); // Move var1 into AL
printf("SUB AL, %c\n", var2); // Subtract var2 from AL
printf("MOV %c, AL\n", result); // Store result in 'result'
break;
case '*':
printf("MOV AL, %c\n", var1); // Move var1 into AL
printf("MUL %c\n", var2); // Multiply AL by var2
printf("MOV %c, AL\n", result); // Store result in 'result'
break;
case '/':
printf("MOV AL, %c\n", var1); // Move var1 into AL
printf("DIV %c\n", var2); // Divide AL by var2
printf("MOV %c, AL\n", result); // Store result in 'result'
break;
default:
printf("Invalid operator!\n");
}
} else {
printf("Invalid TAC format!\n");
}
}

int main() {
char tac[100];

// Input TAC (Three-Address Code) example: "A = B + C"


printf("Enter the Three-Address Code (e.g., A = B + C):\n");
fgets(tac, sizeof(tac), stdin); // Read input TAC

// Generate corresponding 8086 assembly


generateAssembly(tac);
return 0;
}

You might also like