Compiler Design Lab Report
Compiler Design Lab Report
TECHNOLOGY, MANIPUR
Date: - 08-11-2023
Name: -Mahi Chhawchharia
Roll no: -220103029
Subject: - CS3044: Compiler Design Lab
Lex is a program that generates lexical analyzer. It is used with YACC (Yet
Another Compiler Compiler) parser generator.
The lexical analyzer is a program that transforms an input stream into a
sequence of tokens.
It reads the input stream and produces the source code as output through
implementing the lexical analyzer in the C program.
%%
Rules section
%%
%%
[0-9]+ {i=atoi(yytext);
if(i%2==0)
printf("Even");
else
printf("Odd");}
%%
int yywrap(){}
int main()
{
yylex();
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-1
3. Write a lex program to count the no. of vowels and consonants in a given
string.
%{
#include<stdio.h>
int vowels=0;
int consonants=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {consonants++;}
%%
int yywrap(){}
void main()
{
printf("Enter the string:\n");
yylex();
printf("\nNumber of vowels=%d\n",vowels);
printf("Number of consonants=%d\n",consonants);
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-1
4. Write a lex program to check whether a given number is an Armstrong number
or not.
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void check(char* a);
extern char* yytext;
%}
%%
[0-9]+ check(yytext);
%%
int yywrap() {}
void check(char* a)
{
int num = atoi(a);
int p = strlen(a);
int y = 0, temp = num;
while (num > 0)
{
y += pow((num % 10), p);
num = num / 10;
}
if (y == temp)
printf("%d is an Armstrong number\n", temp);
else
printf("%d is not an Armstrong number\n", temp);
}
int main()
{
yylex();
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-2
1. Write a lex program to count the positive numbers, negative numbers, and
fractions.
%{
#include<stdio.h>
int pos=0, neg=0,frac=0;
%}
%%
[0-9]+ { pos++;}
-[0-9]+ { neg++;}
[0-9]*\.[0-9]+ { frac++; }
-[0-9]*\.[0-9]+ { frac++; }
%%
int yywrap(){}
int main()
{
printf("Enter the string:\n");
yylex();
printf("\nNumber of positive number = %d\n",pos);
printf("Number of negative number = %d\n",neg);
printf("Number of fraction number = %d\n",frac);
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-2
2. Write a lex program to count words that are less than 10 and greater than 5
from a given sentence.
%{
#include<stdio.h>
#include<string.h>
int l=0,c=0;
%}
%%
[a-zA-Z]+ {
l= strlen(yytext);
if(l<10 && l>5)
c++;
}
%%
int yywrap(){}
int main()
{
printf("Enter the string:\n");
yylex();
printf("\nNumber of words < 10 but > 5 = %d\n",c);
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-2
3. Write a lex program to count the no. of vowels and consonants in a given
string.
%{
#include<stdio.h>
int line_c=0, space_c=0,tab_c=0;
%}
%%
[\n] { line_c++;}
[ ] { space_c++;}
[\t] { tab_c++; }
[^\t] {}
%%
int yywrap(){}
int main()
{
printf("Enter the string:\n");
yylex();
printf("\nNumber of lines = %d\n",line_c);
printf("Number of spaces = %d\n",space_c);
printf("Number of tabs = %d\n",tab_c);
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-3
{OPERATORS} {
opCount++;
}
{KEYWORDS} {
keyCount++;
}
[a-zA-Z_][a-zA-Z0-9_]* {
idenCount++;
}
{SPECIAL_SYMBOLS} {
ssCount++;
}
. {}
\n {}
%%
int main()
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-3
{
printf("Write the C program:\n");
yylex();
token = opCount+idenCount+keyCount+ssCount;
printf("Number of Tokens: %d\n", token);
return 0;
}
int identifiersCount = 0;
int keywordsCount = 0;
int num = 0;
%}
KEYWORDS
(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|
for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|
typedef|union|unsigned|void|volatile|while)
%%
[\t]+ ;
{KEYWORDS} {
keywordsCount++;
}
[a-zA-Z_][a-zA-Z0-9_]* {
identifiersCount++;
}
[0-9]*[eE][+-]?[0-9]+ { num++; }
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-3
[0-9]*\.[0-9]+ { num++; }
[0-9]+ { num++;}
. {}
\n {}
%%
int main()
{
printf("Write the C program:\n");
yylex();
1. Write a lex program to identify the REAL PRECISION of the given number.
%{
#include<stdio.h>
int pre=0;
%}
F1 [0-9]*\.[0-9]+
%%
{F1} {
char *d = yytext;
int flag =0;
pre = 0;
while(*d != '\0')
{
if(*d == '.')
{
flag =1;
d++;
}
if(flag == 1)
{
pre++;
}
d++;
}
printf("Precision : %d \n ",pre);
}
[0-9]+ {printf("Precision : 0\n ");}
. {}
[\t\n]+ {}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 1;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-4
OUTPUT:
class NonTerminal {
string name; // Stores the Head of production rule
vector<string> productionRules; // Stores the body of production rules
public:
NonTerminal(string name) {
this->name = name;
}
vector<string> getRules() {
return productionRules;
}
toPrint.pop_back();
cout << toPrint << endl;
}
};
class Grammar {
vector<NonTerminal> nonTerminals;
public:
// Add rules to the grammar
void addRule(string rule) {
bool nt = 0;
string parse = "";
void inputData() {
string rule="";
cout << "Enter production rule : ";
getline(cin, rule);
addRule(rule);
if (!betas.size())
newRulesA.push_back(newName);
int main(){
//freopen("output.txt", "w+", stdout);
Grammar grammar;
grammar.inputData();
grammar.applyAlgorithm();
grammar.printRules();
return 0;
}
OUTPUT:
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-5
1. Write a program in any language to implement the recursive descent parser.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[32];
char *ptr;
int E(), E_prime(), T(), T_prime(), F();
int F()
{
if( *ptr == '(')
{
ptr++;
if(E())
{
if( *ptr == ')')
{
ptr++;
return 1;
}
else return 0;
}
else return 0;
}
else if (*ptr == 'i' && *(ptr+1) == 'd')
{
ptr++;
return 1;
}
else
{
return 0;
}
}
int T_prime()
{
if( *ptr == '*')
{
ptr++;
if(F())
{
if(T_prime())
return 1;
else
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-5
return 0;
}
else
{
return 0;
}
}
}
int E_prime()
{
if( *ptr == '+')
{
ptr++;
if(T())
{
if(E_prime())
return 1;
else
return 0;
}
else
{
return 0;
}
}
}
int T()
{
if(F())
{
if(T_prime())
return 1;
else
return 0;
}
else
{
return 0;
}
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-5
int E()
{
if(T())
{
if(E_prime())
return 1;
else
return 0;
}
else
{
return 0;
}
}
int main()
{
printf("E -> TE'\nE' -> +TE' | ε\nT -> FT'\nT' -> *FT' | ε\nF ->(E) |
id\n");
int ch =1;
do{
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-5
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-6
#include <iostream>
#include <string>
#include <map>
#include <stack>
// Grammar
const string prod[] = {"E->T A", "A->+ T A | e", "T->F B", "B->* F B | e",
"F->(E) | i"};
const string first[] = {"i", "+, e", "i", "*, e", "i"};
const string follow[] = {"$, )", "$, )", "+, $, )", "+, $, )", "*, +, $, )"};
table["A"]['('] = "EMPTY";
table["A"]['i'] = "EMPTY";
table["A"]['+'] = "A->+ T A";
table["A"]['*'] = "EMPTY";
table["A"][')'] = "e";
table["A"]['$'] = "e";
table["T"]['*'] = "EMPTY";
table["T"][')'] = "EMPTY";
table["T"]['$'] = "EMPTY";
table["B"]['('] = "EMPTY";
table["B"]['i'] = "EMPTY";
table["B"]['+'] = "e";
table["B"]['*'] = "B->* F B";
table["B"][')'] = "e";
table["B"]['$'] = "e";
table["F"]['('] = "F->(E)";
table["F"]['i'] = "F->i";
table["F"]['+'] = "EMPTY";
table["F"]['*'] = "EMPTY";
table["F"][')'] = "EMPTY";
table["F"]['$'] = "EMPTY";
}
void display(stack<char> s) {
stack<char> temp = s;
while (!temp.empty()) {
cout << temp.top();
temp.pop();
}
}
void printGrammarAndTable() {
cout << "Grammar:\n";
for (const string& p : prod) {
cout << p << endl;
}
}
cout << endl;
}
cout << "-------------------------------------\n";
}
int main() {
string input;
cout << "Enter the input string terminated with $ to parse: ";
cin >> input;
if (input.back() != '$') {
cout << "Input String Entered Without End Marker $" << endl;
return 1;
}
stack<char> s;
s.push('$');
s.push('E');
int i = 0;
cout << "\nStack\t Input\tAction" << endl;
cout <<
"-------------------------------------------------------------------" << endl;
initializeTable();
printGrammarAndTable();
if (stackTop == inputChar) {
cout << "\tMatched " << inputChar << endl;
s.pop();
i++;
} else {
string stackTopStr(1, stackTop);
string inputCharStr(1, inputChar);
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-6
display(s);
cout << "\t\t" << input.substr(i) << "\t";
cout <<
"\n-------------------------------------------------------------------" <<
endl;
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-6
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-7
1. Write a YACC program to implement a calculator and recognize a valid
arithmetic expression
lex.l
%{
#include "y.tab.h"
%}
%%
[0-9]+ { yylval = atoi(yytext); return TERM; }
[\t ] { }
\n { return '\n'; }
\+ { return '+'; }
\- { return '-'; }
\* { return '*'; }
\/ { return '/'; }
\( { return '('; }
\) { return ')'; }
. { return yytext[0]; }
%%
int yywrap() {
return 1;
}
calculator.y
%{
#include<stdio.h>
#include<stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}
%token TERM
%%
calc:
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-7
expr '\n' { printf("Output for the arithmetic expression is : %d\n",
$1); }
| calc expr '\n' { printf("Output for the arithmetic expression is : %d\n",
$2); }
;
expr:
expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { if ($3 == 0) yyerror("Division by 0 - Mathematical
Error"); else $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
| TERM { $$ = $1; }
;
%%
int main() {
printf("Enter the valid arithmetic expression :\n");
yyparse();
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-7
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-8
lex.l
%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
extern int yylval;
%}
%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
.|\n { return *yytext; }
%%
int yywrap() {
return 1;
}
postfix.y
%{
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int yylex(void);
void yyerror(char *s);
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%right UMINUS
%right '^'
%%
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-8
lines:
| lines E '\n' { printf("\nAnswer: %d\n", $2);
printf("\nEnter the infix expression: ");}
;
%%
int main(void) {
printf("\nEnter the infix expression: ");
yyparse();
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-8
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-9
1. Write a YACC program to accept a sequence of 0s and 1s which end and start
either with 0 or 1.
lex.l
%{
#include<stdio.h>
#include<stdlib.h>
#include "y.tab.h"
extern int yylval;
%}
%%
0 { yylval = 0; return Z; }
1 { yylval = 1; return O; }
[ \t] { ; }
\n { return 0; }
. { return yytext[0]; }
%%
int yywrap()
{
return 1;
}
start-end.y
%{
#include <stdlib.h>
#include <stdio.h>
int yylex();
void yyerror(char *s);
%}
%token Z O
%%
r : s { printf("\nGrammar ACCEPTS the sequence\n"); }
s : n { $$ = $1; }
| Z a { $$ = $2; }
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-9
| O b { $$ = $2; }
;
a : n a { $$ = $1; }
| Z { $$ = $1; }
;
b : n b { $$ = $1; }
| O { $$ = $1; }
;
n : Z { $$ = $1; }
| O { $$ = $1; }
;
%%
int main()
{
printf("Enter the sequence: \n");
yyparse();
return 0;
}
#include <stdio.h>
#include <string.h>
int stack[MAX];
int top = -1;
void pop() {
if (top >= 0) {
top--;
} else {
printf("Stack underflow!\n");
}
}
int peek() {
if (top >= 0) {
return stack[top];
}
return -1;
}
const char* rules[2] = { "S->aA", "A->b" };
int actionTable[4][3] = {
{1, -1, -1},
{-1, 2, -1},
{-1, -1, 0},
{-1, -1, -1}
};
int i = 0;
char symbol = input[i];
push(0);
while (1) {
int state = peek();
if (symbol == 'a') {
if (actionTable[state][0] == 1) {
printf("Shift: a\n");
push(1);
symbol = input[++i];
} else {
printf("Error: Unexpected symbol 'a'\n");
break;
}
} else if (symbol == 'b') {
if (actionTable[state][1] == 2) {
printf("Shift: b\n");
push(2);
symbol = input[++i];
} else if (actionTable[state][1] == -1 && state == 1) {
printf("Reduce: A -> b\n");
pop();
pop();
push(2);
} else {
printf("Error: Unexpected symbol 'b'\n");
break;
}
} else if (symbol == '$') {
if (actionTable[state][2] == 0) {
printf("Input accepted.\n");
break;
} else {
printf("Error: Input not accepted.\n");
break;
}
} else {
printf("Error: Invalid symbol '%c'\n", symbol);
break;
}
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-10
}
int main() {
char input[MAX];
printf("Grammar:\nS → aA\nA->b\n");
printf("Enter a string to parse (end with $): ");
scanf("%s", input);
if (input[strlen(input) - 1] != '$') {
printf("Error: Input must end with '$' to mark the end of the
string.\n");
return 1;
}
// code in C for a = b * -c + b * -c
#include <stdio.h>
#include <string.h>
typedef struct {
char op[3];
char arg1[10];
char arg2[10];
char result[10];
} TAC;
int temp_count = 0;
newTemp(tac[*index].result);
strcpy(tac[*index].op, "-");
strcpy(tac[*index].arg1, temp3);
strcpy(tac[*index].arg2, "");
(*index)++;
char negTemp[10];
strcpy(negTemp, tac[*index - 1].result);
newTemp(tac[*index].result);
strcpy(tac[*index].op, "*");
strcpy(tac[*index].arg1, temp2);
strcpy(tac[*index].arg2, negTemp);
(*index)++;
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-11
char mulTemp1[10];
strcpy(mulTemp1, tac[*index - 1].result);
newTemp(tac[*index].result);
strcpy(tac[*index].op, "-");
strcpy(tac[*index].arg1, temp3);
strcpy(tac[*index].arg2, "");
(*index)++;
newTemp(tac[*index].result);
strcpy(tac[*index].op, "*");
strcpy(tac[*index].arg1, temp2);
strcpy(tac[*index].arg2, negTemp);
(*index)++;
char mulTemp2[10];
strcpy(mulTemp2, tac[*index - 1].result);
newTemp(tac[*index].result);
strcpy(tac[*index].op, "+");
strcpy(tac[*index].arg1, mulTemp1);
strcpy(tac[*index].arg2, mulTemp2);
(*index)++;
char addTemp[10];
strcpy(addTemp, tac[*index - 1].result);
strcpy(tac[*index].op, "=");
strcpy(tac[*index].arg1, addTemp);
strcpy(tac[*index].arg2, "");
strcpy(tac[*index].result, temp1);
(*index)++;
}
int main() {
char expr[50] = "a = b * -c + b * -c";
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-11
TAC tac[10];
int index = 0;
printf("Three-Address Code:\n");
for (int i = 0; i < index; i++) {
if (strcmp(tac[i].op, "=") == 0) {
printf("%s = %s\n", tac[i].result, tac[i].arg1);
} else {
printf("%s = %s %s %s\n", tac[i].result, tac[i].arg1, tac[i].op,
tac[i].arg2);
}
}
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-11
//Code in cpp generalised version not fully correct but just attempt at it
#include <iostream>
#include <stack>
#include <sstream>
#include <vector>
#include <cctype>
struct TAC {
string op;
string arg1;
string arg2;
string result;
};
vector<TAC> threeAddressCode;
string newTemp() {
static int tempCount = 0;
return "t" + to_string(tempCount++);
}
return 0;
}
while (!operators.empty()) {
string op = string(1, operators.top());
operators.pop();
void printTAC() {
for (const auto& tac : threeAddressCode) {
cout << tac.result << " = " << tac.arg1 << " " << tac.op << " " << tac.arg2 << endl;
}
}
int main() {
string expression;
cout<< "u for unary minus\n";
cout << "Enter an expression: ";
getline(cin, expression);
infixToTAC(expression);
cout << "\nThree Address Code:\n";
printTAC();
return 0;
}
MAHI CHHAWCHHARIA
220103029
LAB ASSIGNMENT-11