CD LAB MANUAL R23
1. Check the ouput of different compilers gcc, g++, clang, clang++, javac, python etc by
running
respective language programs with different flags. (purpose to understand preprocessor,
optimizations, linker)
C / C++: gcc, g++, clang, clang++
Sample C Program (hello.c)
#include <stdio.h>
int main()
printf("Hello, World!\n");
return 0;
Output:
Hello, World!
Sample C++ Program (hello.cpp)
#include <iostream>
int main()
std::cout << "Hello, C++ World!" << std::endl;
return 0;
Output:
Hello, C++ World!
1. Preprocessing Phase
This only expands macros, includes, etc.
Run:
gcc -E hello.c -o hello.i
g++ -E hello.cpp -o hello.i
clang -E hello.c -o hello.i
Output:
File hello.i contains the expanded code (e.g., full content of <stdio.h> included).
No compiling or linking occurs.
2. Compilation Phase
Run:
Convert preprocessed code into assembly.
gcc -S hello.c -o hello.s
g++ -S hello.cpp -o hello.s
Output:
File hello.s is assembly code.
Observe how main and printf translate to instructions.
3. Compilation + Optimization
Try optimization flags to see changes in output.
gcc -O0 -S hello.c -o hello_O0.s # No optimization
gcc -O2 -S hello.c -o hello_O2.s # Aggressive optimization
Compare:
Use diff hello_O0.s hello_O2.s
With -O2, you may see fewer instructions or inlined code.
4. Linking Phase
Combine object files into an executable.
gcc -c hello.c -o hello.o
# Only compile to object
gcc hello.o -o hello
Output:
# Link manually
Executable hello is created.
Java: javac, java
Sample Java Program (Hello.java)
public class Hello
public static void main(String[] args)
System.out.println("Hello, Java World!");
Output:
Hello, Java World!
Run:
javac Hello.java
java Hello
Observations:
javac compiles to Hello.class (bytecode).
No preprocessing or linking.
JVM performs runtime optimizations (JIT).
Python: python / python3
Sample Python Program (hello.py)
print("Hello, Python World!")
Output:
Hello, Python World!
Run:
python hello.py
Observations:
Interpreted line-by-line.
You can precompile using:
python -m py_compile hello.py
Produces .pyc file in __pycache__/ (bytecode).
2. The Language called TinyCStr is described as follows
a) Every TinyCStr program has one or more functions and syntax of function declaration and
function definition is similar to C, one function must be main.
#include <stdio.h>
// Function declaration
void greet();
int main() {
// Main function definition
printf("Welcome to TinyCStr!\n");
// Calling another function
greet();
return 0;
// Function definition
void greet() {
printf("Hello from the greet function!\n");
Output
Welcome to TinyCStr!
Hello from the greet function!
b) Every TinyCStr function has zero or statements
#include <stdio.h>
// Function to print a message (one statement)
void printMessage() {
printf("Welcome to TinyCStr!\n");
// Function to add two numbers (one statement)
int add(int a, int b) {
return a + b;
// Function to check if a number is even (one statement)
int isEven(int num) {
return num % 2 == 0;
// Main function (zero or one statement per function)
int main() {
printMessage(); // Call to printMessage (one statement)
int sum = add(5, 10); // Call to add (one statement)
printf("Sum: %d\n", sum); // Print the result (one statement)
printf("Is 10 even? %s\n", isEven(10) ? "Yes" : "No"); // Check even (one statement)
return 0; // Return statement (one statement)
Output
Welcome to TinyCStr!
Sum: 15
Is 10 even? Yes
c) The possible statements are declaration, assignment, conditional statements (if,else, for,
while)
except switch
#include <stdio.h>
int main() {
// Declaration and assignment
int a = 10, b = 20, sum = 0;
// If-else conditional statement
if (a > b) {
printf("a is greater than b\n");
} else {
printf("b is greater than or equal to a\n");
// For loop
for (int i = 1; i <= 5; i++) {
sum += i; // Assignment inside the loop
printf("Sum of first 5 natural numbers: %d\n", sum);
// While loop
int count = 5;
while (count > 0) {
printf("Countdown: %d\n", count);
count--; // Assignment inside the loop
printf("Program executed successfully!\n");
return 0;
} print(i); // Print the current value of i
i = i + 1; // Increment i by 1
// Main Function
int main() {
// Call the function to print numbers using while loop
printNumbersUsingWhile();
return 0; // Return 0 indicating successful execution
Output
b is greater than or equal to a
Sum of first 5 natural numbers: 15
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Program executed successfully!
d) TinyCStr supports primitive data types of C and a string datatype
i) Implement a lexical analyser for TinyCStr usingflex/lex
Lex File: tinycstr.l
%{
#include <stdio.h>
%}
%%
"int" { printf("Keyword: int\n"); }
"float" { printf("Keyword: float\n"); }
"char" { printf("Keyword: char\n"); }
"string" { printf("Keyword: string\n"); }
[0-9]+ { printf("Number: %s\n", yytext); }
[a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
"+"|"-"|"*"|"/" { printf("Operator: %s\n", yytext); }
"=" { printf("Assignment Operator: %s\n", yytext); }
";" { printf("Semicolon\n"); }
"(" { printf("Left Parenthesis\n"); }
")" { printf("Right Parenthesis\n"); }
"{" { printf("Left Brace\n"); }
"}" { printf("Right Brace\n"); }
[ \t\n]+ { /* Ignore whitespace */ }
. { printf("Unknown character: %s\n", yytext); }
%%
int main() {
printf("Enter TinyCStr code (Ctrl+D to end):\n");
yylex();
return 0;
int yywrap() {
return 1;
Steps to Run the Program
1. Install Flex:
o On Linux: Use sudo apt install flex or equivalent for your package manager.
o On Windows: Install Flex via MinGW or Cygwin.
2. Save the Lex File: Save the above code in a file named tinycstr.l.
3. Generate the Lexical Analyzer: Run the following command in your terminal:
flex tinycstr.l
This generates a lex.yy.c file.
4. Compile the Generated C File: Use a C compiler like GCC to compile the generated file:
gcc lex.yy.c -o tinycstr –lfl
The -lfl flag links the Flex library.
5. Run the Lexical Analyzer: Execute the compiled program:
./tinycstr
Enter your TinyCStr code as input, and the program will analyze it. Use Ctrl+D (Linux/Mac)
or Ctrl+Z (Windows) to signal the end of input.
Output:
Input:
int x = 10;
string name = "John";
float y = 3.14;
Output:
Keyword: int
Identifier: x
Assignment Operator: =
Number: 10
Semicolon
Keyword: string
Identifier: name
Assignment Operator: =
Unknown character: "
Identifier: John
Unknown character: "
Semicolon
Keyword: float
Identifier: y
Assignment Operator: =
Number: 3
Unknown character: .
Number: 14
Semicolon