0% found this document useful (0 votes)
3 views5 pages

CD Lab Programs

The document outlines two programming tasks: the first is to create a lexical analyzer using Flex to identify C language control structure keywords, while the second is to implement basic algebraic transformation optimization techniques for arithmetic expressions in C. The lexical analyzer program reads input, matches keywords, and prints them, while the optimization program evaluates expressions and applies rules to simplify them. Both programs include algorithms and example outputs demonstrating their functionality.

Uploaded by

keerthigav.cse
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)
3 views5 pages

CD Lab Programs

The document outlines two programming tasks: the first is to create a lexical analyzer using Flex to identify C language control structure keywords, while the second is to implement basic algebraic transformation optimization techniques for arithmetic expressions in C. The lexical analyzer program reads input, matches keywords, and prints them, while the optimization program evaluates expressions and applies rules to simplify them. Both programs include algorithms and example outputs demonstrating their functionality.

Uploaded by

keerthigav.cse
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

1.

Develop a program to recognize a valid control structures syntax of c language(for


loop, while loop, if else, if-else-if, switch-case etc)

Aim:
To develop a lexical analyzer using Flex that reads C code as input and identifies specific
keywords (if, else, while, for, switch, case, default), printing each keyword when detected.
Algorithm:
 Start the lexical analyzer.
 Read input character by character from the user or file.
 Check if the current input matches any of the predefined keywords:
 if
 else
 while
 for
 switch
 case
 default
 If a keyword is matched, print "Keyword: <keyword>".
 If the character is whitespace (space, tab, newline), ignore it and continue scanning.
 If the character is anything else, ignore it and continue scanning.
 Repeat steps 2 to 6 until the end of input (EOF) is reached.
 Stop the lexical analyzer.

Program:
File Name: control.l
%{
#include <stdio.h>
%}

%option noyywrap

%%

"if" { printf("Keyword: if\n"); }


"else" { printf("Keyword: else\n"); }
"while" { printf("Keyword: while\n"); }
"for" { printf("Keyword: for\n"); }
"switch" { printf("Keyword: switch\n"); }
"case" { printf("Keyword: case\n"); }
"default" { printf("Keyword: default\n"); }

[ \t\n]+ ; // Ignore whitespace


. ; // Ignore all other characters

%%

int main() {
printf("Enter C code (CTRL+D to end input):\n");
yylex();
return 0;
}

[2cs120@localhost ~]$ flex control.l


[2cs120@localhost ~]$ gcc [Link].c -o scanner -lfl
[2cs120@localhost ~]$ ./scanner
Enter C code (CTRL+D to end input):
if(x>0)
Keyword: if

while(y<10)
Keyword: while

for(int i=0;i<5;i++)
Keyword: for

switch case 1;

Keyword: switch
Keyword: case

[Link] code optimization techniques- algebraic transformation

Aim:
To develop a C program that reads a simple arithmetic expression and applies basic constant
expression optimizations.

Algorithm:
 Start the program.
 Prompt the user to enter an expression (e.g., x + 0).
 Read the input: operand1, operator, operand2.
 Check the operator:
 If it is +:
o If either operand is 0, output the other operand.
 If it is *:
o If either operand is 0, output 0.
o If either operand is 1, output the other operand.
 If no optimization rule matches, print: "No optimization applied".
 End the program.

Program:
File Name: algebra.c

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

int main() {
char var[20], op, val[20];

printf("Enter expression (e.g., x + 0):\n");

if (scanf("%s %c %s", var, &op, val) != 3) {


printf("Invalid input\n");
return 1;
}

if (op == '+') {
if (strcmp(val, "0") == 0) {
printf("Optimized: %s\n", var);
return 0;
}
if (strcmp(var, "0") == 0) {
printf("Optimized: %s\n", val);
return 0;
}
} else if (op == '*') {
if (strcmp(val, "1") == 0) {
printf("Optimized: %s\n", var);
return 0;
}
if (strcmp(var, "1") == 0) {
printf("Optimized: %s\n", val);
return 0;
}
if (strcmp(val, "0") == 0 || strcmp(var, "0") == 0) {
printf("Optimized: 0\n");
return 0;
}
}

printf("No optimization applied\n");


return 0;
}

How to run
 Compile: gcc algebra.c -o algebra

 Run: ./algebra

Output:

[2cs120@localhost ~]$ gcc algebra.c -o algebra

[2cs120@localhost ~]$ ./algebra

[2cs120@localhost ~]$ ./algebra

Enter expression (e.g., x + 0):

x+0

Optimized: x

[2cs120@localhost ~]$ ./algebra

Enter expression (e.g., x + 0):


x*0

Optimized: 0

You might also like