LP Practical File 21dit044
LP Practical File 21dit044
PRACTICAL: 1
AIM:
Write a ‘C’ program and generate the following codes for the program.
1. Preprocessed code
2. Assembly Code
3. Object Code
4. Executable Code
THEORY:
1. The C preprocessor modifies the code by expanding macros and including headers before
compilation.
2. Assembly code converts high-level code into machine-specific instructions using mnemonics
like MOV and ADD.
4. Executable code integrates object files and libraries into a program that can run independently
on the system.
5. Each code stage offers insights: preprocessed code simplifies, assembly optimizes, and
executables finalize software usability.
CODE:
Writing a simple C code program:
// File: example.c
#include <stdio.h>
int main() {
int num = 5;
DEPSTAR-IT Page 1
21DIT044 IT451: LP
return 0;
./ example
OUTPUT :
Preprocessed Code :
DEPSTAR-IT Page 2
21DIT044 IT451: LP
Assembly Code :
DEPSTAR-IT Page 3
21DIT044 IT451: LP
Object Code :
Executable Code :
DEPSTAR-IT Page 4
21DIT044 IT451: LP
LATEST APPLICATIONS:
Turbo c++, Codeblocks.
LEARNING OUTCOME:
Understanding the flow from preprocessed code to executables helps develop, debug, and optimize
programs, ensuring efficient and correct compilation of C code.
REFERENCE:
1. https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/
PRACTICAL: 2
DEPSTAR-IT Page 5
21DIT044 IT451: LP
AIM:
Use Macro features of C language and demonstrate the following types of macro with example.
THEORY:
1. Simple macros define constants or simple text replacements using the #define directive without
parameters.
2. Macros with arguments accept inputs, similar to functions, providing flexibility in code
replacement.
3. Nested macros are macros within other macros, creating complex substitutions during
preprocessing.
4. Macros offer improved performance by reducing function call overhead, but can increase
debugging difficulty.
5. Proper use of macros enhances code readability and reuse but requires careful handling to avoid
side effects.
Code:
1) Simple Macro
#include <stdio.h>
int main() {
return 0;
#include <stdio.h>
DEPSTAR-IT Page 6
21DIT044 IT451: LP
int main() {
int num = 5;
return 0;
3) Nested Macro
#include <stdio.h>
int main() {
int a = 3, b = 4;
return 0;
OUTPUT:
1) Simple Macro
DEPSTAR-IT Page 7
21DIT044 IT451: LP
3) Nested Macro
LATEST APPLICATIONS:
CodeBlocks, Turbo C++, Visual Studio Code.
LEARNING OUTCOME:
This exercise strengthens understanding of how macros work, demonstrating simple, parameterized,
and nested macros to enhance code modularity and efficiency.
Reference :
https://www.geeksforgeeks.org/macros-and-its-types-in-c-cpp/
DEPSTAR-IT Page 8
21DIT044 IT451: LP
PRACTICAL: 3
AIM:
Write a Lexical Analyzer using Lex or Flex utility of UNIX for following:
6. A lexer to do word count function of wc command in UNIX.It prints the number of lines, words and
characters in a file.
THEORY:
1. Lexical analyzers scan input text and break it into meaningful tokens for further processing.
2. Lex/Flex utilities generate C programs from lexer specifications using regular expressions for
pattern matching.
3. A lexer can classify tokens into categories such as words, numbers, or symbols based on the
input rules.
4. Lexers are useful for counting and tracking characters, words, lines, and even extracting specific
elements like comments.
CODE:
%{
#include <stdio.h>
%}
%option noyywrap
DEPSTAR-IT Page 9
21DIT044 IT451: LP
%%
%%
int main() {
yylex();
return 0;
%{
#include <stdio.h>
%}
%%
%%
int main() {
yylex();
return 0;
%{
DEPSTAR-IT Page 10
21DIT044 IT451: LP
#include <stdio.h>
%}
%option noyywrap
%%
[aeiouAEIOU] { vowels++; }
[a-zA-Z] { consonants++; }
%%
int main() {
yylex();
return 0;
%{
#include <stdio.h>
int line_num = 1;
%}
%option noyywrap
%%
%%
DEPSTAR-IT Page 11
21DIT044 IT451: LP
int main() {
yylex();
return 0;
%{
#include <stdio.h>
%}
%option noyywrap
%%
%%
int main() {
yylex();
return 0;
6. A lexer to do word count function of wc command in UNIX.It prints the number of lines, words and
characters in a file.
%{
DEPSTAR-IT Page 12
21DIT044 IT451: LP
#include <stdio.h>
%}
%option noyywrap
%%
\n { lines++; characters++; }
. { characters++; }
%%
int main() {
yylex();
return 0;
OUTPUT:
DEPSTAR-IT Page 13
21DIT044 IT451: LP
DEPSTAR-IT Page 14
21DIT044 IT451: LP
6. A lexer to do word count function of wc command in UNIX.It prints the number of lines, words and
characters in a file.
DEPSTAR-IT Page 15
21DIT044 IT451: LP
Latest Application:
Flex, bison,Codeblocks.
Learning Outcome:
Implementing lexical analyzers using Lex/Flex enhances understanding of token classification, text
processing, and pattern matching in UNIX-based utilities.
Reference:
https://www.geeksforgeeks.org/flex-fast-lexical-analyzer-generator/
PRACTICAL: 4
DEPSTAR-IT Page 16
21DIT044 IT451: LP
AIM:
THEORY:
1. A lexical analyzer scans C code to break it into tokens like keywords, identifiers, numbers, and
symbols.
2. Lex utility uses pattern rules to generate a C program that performs lexical analysis.
4. Lex automatically discards spaces and newlines unless specified for tokenization.
5. The generated lexer simplifies subsequent parsing stages by classifying tokens efficiently.
CODE:
%{
#include <stdio.h>
#include <stdlib.h>
%}
/* Define tokens */
%option noyywrap
%%
DEPSTAR-IT Page 17
21DIT044 IT451: LP
%%
return 0;
int main() {
int num = 5;
return 0;
DEPSTAR-IT Page 18
21DIT044 IT451: LP
nested macro
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b)) // Macro with Argument
int main() {
int x = 4, y = 7;
printf("The square of the larger number between %d and %d is: %d\n", x, y, MAX_SQUARE(x, y));
return 0;
OUTPUT:
Simple Macro
DEPSTAR-IT Page 19
21DIT044 IT451: LP
Latest Application:
Flex, bison,Codeblocks.
Learning Outcome:
This task deepens understanding of Lex-based lexical analysis for C, helping process tokens like
keywords, numbers, and symbols in a systematic way.
Reference:
https://www.geeksforgeeks.org/introduction-of-lexical-analysis/
DEPSTAR-IT Page 20
21DIT044 IT451: LP
PRACTICAL: 5
AIM:
Write a program which enters Transition Table, accepting state and input string as input and checks
whether the given string is accepted or not.
Theory:
1. The transition table implementation is a practical exercise in finite automata theory, where
users can input a state, transitions, and an input string.
2. The program checks whether the given string is accepted based on the defined transition
rules.
3. Each transition is defined by a combination of a current state, an input symbol, and the
next state.
4. This allows the simulation of a finite state machine, providing insights into automata
theory and the principles of language recognition.
CODE:
#include <stdio.h>
#include <string.h>
#define MAX_STATES 10
#define MAX_SYMBOLS 10
typedef struct {
DEPSTAR-IT Page 21
21DIT044 IT451: LP
} DFA;
dfa->num_states = 0;
dfa->num_symbols = 0;
dfa->num_accept_states = 0;
strcpy(dfa->state[dfa->num_states], state);
if (is_accept) {
dfa->accept_states[dfa->num_accept_states++] = dfa->num_states;
dfa->num_states++;
void setTransition(DFA *dfa, const char *state, char symbol, const char *next_state) {
DEPSTAR-IT Page 22
21DIT044 IT451: LP
if (strcmp(dfa->state[i], state) == 0) {
state_index = i;
if (strcmp(dfa->state[i], next_state) == 0) {
next_state_index = i;
if (symbol < 'a' || symbol > 'z') { // Check for valid symbols
if (current_state == -1) {
DEPSTAR-IT Page 23
21DIT044 IT451: LP
if (current_state == dfa->accept_states[i]) {
return 1; // Accepted
int main() {
DFA dfa;
initDFA(&dfa);
dfa.transition[i][j] = -1;
DEPSTAR-IT Page 24
21DIT044 IT451: LP
// Starting state
dfa.start_state = 0;
char input_string[100];
scanf("%s", input_string);
if (isAccepted(&dfa, input_string)) {
printf("Accepted\n");
} else {
printf("Rejected\n");
return 0;
DEPSTAR-IT Page 25
21DIT044 IT451: LP
%%
return 0;
int main() {
int num = 5;
return 0;
nested macro
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b)) // Macro with Argument
DEPSTAR-IT Page 26
21DIT044 IT451: LP
int main() {
int x = 4, y = 7;
printf("The square of the larger number between %d and %d is: %d\n", x, y, MAX_SQUARE(x, y));
return 0;
OUTPUT:
Latest Application:
Flex, bison,Codeblocks.
Learning Outcome:
Implementing transition tables aids in understanding how finite automata validate input strings based
on states and transitions.
Reference :
https://www.geeksforgeeks.org/transition-table-in-automata/
DEPSTAR-IT Page 27
21DIT044 IT451: LP
PRACTICAL: 6
AIM:
Explore the JFLAP Tool to demonstrate the deterministic finite automata for following also find its
Regular expression.
Theory:
1. JFLAP is a tool for visualizing and testing automata, grammars, and formal languages.
2. Deterministic Finite Automata (DFA) has one possible transition per input for each state.
3. For odd 0’s or even 1’s, DFA tracks counts of both 0’s and 1’s in different states.
4. For odd 0’s and even 1’s, the DFA ensures both conditions simultaneously through appropriate
state transitions.
5. Regular expressions describe the language accepted by automata, using operators like *, |, and
concatenation.
DEPSTAR-IT Page 28
21DIT044 IT451: LP
Learning Outcome:
DEPSTAR-IT Page 29
21DIT044 IT451: LP
Using JFLAP to create DFA provides hands-on experience with state transitions and constructing
equivalent regular expressions for formal languages.
Latest Application:
Juypiter Notebook.
Reference:
https://www.geeksforgeeks.org/dfa-machines-accepting-odd-number-of-0s-or-and-even-
number-of-1s/
PRACTICAL: 7
AIM:
Create Google Form for the student registration with student ID, Name, Email Address, Mobile number
etc. Provide Regular expression in every field of the Google Form.
Theory:
1.
CODE:
function createStudentRegistrationForm() {
studentIDItem.setValidation(
DEPSTAR-IT Page 30
21DIT044 IT451: LP
FormApp.createTextValidation()
.requireTextMatchesPattern('^\\d{7}$')
.build()
);
nameItem.setValidation(
FormApp.createTextValidation()
.requireTextMatchesPattern('^[A-Za-z\\s]+$')
.build()
);
emailItem.setValidation(
FormApp.createTextValidation()
.requireTextMatchesPattern('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$')
.build()
);
DEPSTAR-IT Page 31
21DIT044 IT451: LP
mobileItem.setValidation(
FormApp.createTextValidation()
.requireTextMatchesPattern('^\\d{10}$')
.build()
);
OUTPUT:
DEPSTAR-IT Page 32
21DIT044 IT451: LP
Learning Outcome:
This exercise enhances skills in using Google Forms and regex for input validation, ensuring structured
and error-free student data collection.
Latest Application:
Google Form.
DEPSTAR-IT Page 33
21DIT044 IT451: LP
Reference:
https://docs.google.com/forms/d/1Wn0auIzqSvvAxsc6OHAd-c_KWoK5T_o0DB971nu0pTA/edit
PRACTICAL: 8
AIM:
Write Generation Of Three Address Code in C Programming.
Theory:
1. Three-address code (TAC) divides complex statements into simpler instructions with a
maximum of three operands.
2. TAC facilitates efficient code generation, using temporary variables for intermediate results.
4. This code serves as an intermediate representation for optimization during the compilation
process.
5. C programming can implement TAC by translating complex expressions step by step into
smaller instructions.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
DEPSTAR-IT Page 34
21DIT044 IT451: LP
#include <ctype.h>
int tempVarCount = 0;
char* newTemp() {
return temp;
strcpy(op1, stack[top--]);
DEPSTAR-IT Page 35
21DIT044 IT451: LP
strcpy(stack[++top], tempVar);
// Final result
int main() {
char expression[100];
return 0;
OUTPUT:
DEPSTAR-IT Page 36
21DIT044 IT451: LP
Learning Outcome:
Latest Application:
CodeBlocks.
Reference:
https://www.geeksforgeeks.org/three-address-code-compiler/
DEPSTAR-IT Page 37
21DIT044 IT451: LP
PRACTICAL: 9
AIM:
Write program to implement Recursive Descent Parser for the given grammar.
E -> T + E | T
T -> F * T | F
F -> id
Theory:
1. A recursive descent parser uses a set of recursive procedures to process input based on
grammar rules.
2. The parser matches tokens from the input string with the grammar rules, building a parse tree.
3. Each non-terminal in the grammar has a corresponding function that attempts to match its
production.
DEPSTAR-IT Page 38
21DIT044 IT451: LP
5. This parsing technique is straightforward but may struggle with left recursion and ambiguity in
grammar.
CODE:
#include <stdio.h>
#include <string.h>
#define SUCCESS 1
#define FAILED 0
// Function prototypes
char string[64];
int main() {
cursor = string;
puts("");
puts("Input Action");
puts("--------------------------------");
if (E() && *cursor == '\0') { // If parsing is successful and the cursor has reached the end
puts("--------------------------------");
DEPSTAR-IT Page 39
21DIT044 IT451: LP
return 0;
else {
puts("--------------------------------");
return 1;
int E() {
return SUCCESS;
else
return FAILED;
else
return FAILED;
int Edash() {
if (*cursor == '+') {
cursor++;
DEPSTAR-IT Page 40
21DIT044 IT451: LP
return SUCCESS;
else
return FAILED;
else
return FAILED;
else {
return SUCCESS;
int T() {
return SUCCESS;
else
return FAILED;
else
return FAILED;
DEPSTAR-IT Page 41
21DIT044 IT451: LP
int Tdash() {
if (*cursor == '*') {
cursor++;
return SUCCESS;
else
return FAILED;
else
return FAILED;
else {
return SUCCESS;
int F() {
if (*cursor == '(') {
cursor++;
if (*cursor == ')') {
cursor++;
return SUCCESS;
DEPSTAR-IT Page 42
21DIT044 IT451: LP
else
return FAILED;
else
return FAILED;
cursor++;
return SUCCESS;
else
return FAILED;
OUTPUT:
DEPSTAR-IT Page 43
21DIT044 IT451: LP
Learning Outcome:
Latest Application:
CodeBlocks.
Reference:
https://www.geeksforgeeks.org/recursive-descent-parser/
PRACTICAL: 10
DEPSTAR-IT Page 44
21DIT044 IT451: LP
AIM:
Demonstrate the parsing technique using ANTLR Tool.
Theory:
1. ANTLR (Another Tool for Language Recognition) is a powerful parser generator that uses a
grammar file to define language syntax.
2. ANTLR generates parsers, lexers, and tree walkers from defined grammars, streamlining
language processing tasks.
3. It supports various output languages, including Java, C#, Python, and JavaScript, making it
versatile for different applications.
4. ANTLR's ability to handle complex grammars allows for efficient parsing of programming
languages and domain-specific languages.
5. The tool generates Abstract Syntax Trees (ASTs), enabling further processing and analysis of
parsed code.
DEPSTAR-IT Page 45
21DIT044 IT451: LP
Learning Outcome:
Latest Application:
ANTLR.
Reference:
https://tomassetti.me/antlr-mega-tutorial/
DEPSTAR-IT Page 46
21DIT044 IT451: LP
PRACTICAL: 11
AIM:
Write a Parser using YACC or utility of UNIX for following: Write a Program for a simple desk
calculator using YACC Specification.
Theory:
1. YACC (Yet Another Compiler Compiler) generates parsers based on a specified grammar for
language processing tasks.
2. A simple desk calculator can be designed using YACC to evaluate arithmetic expressions and
manage operator precedence.
3. YACC uses production rules to define grammar, with actions specified in C to perform
calculations.
4. Lexical analysis can be integrated with YACC for tokenizing input using regular expressions.
5. Error handling is essential to manage invalid expressions gracefully, providing user feedback.
CODE:
#include "y.tab.h"
%}
%%
// Define tokens
DEPSTAR-IT Page 47
21DIT044 IT451: LP
\n { return NEWLINE; }
%%
// Error handling
int yywrap() {
return 1;
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int yylex();
%}
%token NUMBER
%token NEWLINE
%%
DEPSTAR-IT Page 48
21DIT044 IT451: LP
calculation:
| calculation line
line:
expression NEWLINE {
expression:
$$ = $1 + $3;
$$ = $1 - $3;
| term {
$$ = $1;
term:
$$ = $1 * $3;
$$ = $1 / $3;
DEPSTAR-IT Page 49
21DIT044 IT451: LP
| factor {
$$ = $1;
factor:
NUMBER {
$$ = $1;
%%
int main() {
return yyparse();
DEPSTAR-IT Page 50
21DIT044 IT451: LP
OUTPUT:
Learning Outcome:
Latest Application:
CodeBlocks.
Reference:
https://www.geeksforgeeks.org/introduction-to-yacc/
DEPSTAR-IT Page 51