0% found this document useful (0 votes)
11 views

CompilerConsLab Pranjal

Uploaded by

kedop48916
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)
11 views

CompilerConsLab Pranjal

Uploaded by

kedop48916
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
You are on page 1/ 11

1. Program Writing in C/C++.

Sol:-

#include<stdio.h>

#include<ctype.h>

void lexicalAnalysis(char* code) {

int i = 0;

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

if (isalpha(code[i])) {

printf("Identifier: %c\n", code[i]);

} else if (isdigit(code[i])) {

printf("Number: %c\n", code[i]);

} else if (code[i] == '+' || code[i] == '=') {

printf("Operator: %c\n", code[i]);

} else {

printf("Unknown symbol: %c\n", code[i]);

i++;

int main() {

char code[] = "a = 5 + 9";

printf("Lexical Analysis Output:\n");

lexicalAnalysis(code)

return 0;

}
Lexical Analysis Output:
Identifier: a
Operator: =
Number: 5
Operator: +
Number: 9
2.Write a program to identify macro definitions in an assembly language program. Extend the
above program to implement simple and recursive macro expansion.
Sol:-
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void identifyMacroDefinitions(const vector<string>& program) {


bool inMacro = false;
string macroName;
for (const auto& line : program) {
if (line.find("MACRO") != string::npos) {
inMacro = true;
macroName = line.substr(6); // Extract the macro name
cout << "Macro Definition Found: " << macroName << endl;
cout << "Macro Body: " << endl;
}
else if (line.find("MEND") != string::npos) {
inMacro = false;
cout << "End of Macro: " << macroName << endl;
}
else if (inMacro) {
// Inside macro, print the macro body
cout << line << endl;
}
}
}

int main() {
// Sample assembly language program with macro definitions
vector<string> program = {
"MACRO INCR",
"ADD 1",
"MEND",
"MACRO DOUBLE",
"INCR",
"INCR",
"MEND",
"START",
"INCR",
"DOUBLE",
"END"
};
identifyMacroDefinitions(program);
return 0;
}
Macro Definition Found: INCR
Macro Body:
ADD 1
End of Macro: INCR
Macro Definition Found: DOUBLE
Macro Body:
INCR
INCR
End of Macro: DOUBLE
3.Write a program to process 'include' and 'define' macros in C language. Write a program to parse
source code strings of C-language and identify tokens in terms of Keywords and identifiers.

Sol:-

#include <iostream>

#include <string>

#include <map>

#include <vector>

using namespace std;

// Map to store defined macros

map<string, string> defineTable;

// Function to process the include and define macros

void processMacros(const vector<string>& sourceCode) {

for (const auto& line : sourceCode) {

if (line.find("#include") != string::npos) {

// Simulate processing an include file

string headerFile = line.substr(9); // Get the header file name

cout << "Processing include file: " << headerFile << endl;

} else if (line.find("#define") != string::npos) {

// Process a #define macro

size_t pos = line.find(" ");

string macroName = line.substr(pos + 1, line.find(" ", pos + 1) - pos - 1);

string macroValue = line.substr(line.find(macroName) + macroName.length() + 1);

defineTable[macroName] = macroValue;

cout << "Macro defined: " << macroName << " -> " << macroValue << endl;

}
int main() {

// Simulated source code with #include and #define macros

vector<string> sourceCode = {

"#include <stdio.h>",

"#define PI 3.14",

"#define MAX 100"

};

// Process macros

processMacros(sourceCode);

return 0;

}
Processing include file: <stdio.h>
Macro defined: PI -> 3.14
Macro defined: MAX -> 100
4. Write a program to process 'include' and 'define' macros in C language. Write a program to parse source
code strings of C-language and identify tokens in terms of Keywords and identifiers.

Sol:-

#include <iostream>

#include <string>

#include <stack>

using namespace std;

struct Node {

string value;

Node* left;

Node* right;

Node(string val) : value(val), left(nullptr), right(nullptr) {}

};

// Function to create a new node

Node* createNode(string val) {

return new Node(val);

// Function to print the parse tree in an inorder traversal (for visualizing the structure)

void printInOrder(Node* root) {

if (root == nullptr) return;

// Print left subtree

if (root->left) cout << "(";

printInOrder(root->left);

// Print current node

cout << root->value;

// Print right subtree

printInOrder(root->right);

if (root->right) cout << ")";

// Function to construct a parse tree from an arithmetic expression

Node* constructParseTree(string expression) {

stack<Node*> operands; // Stack for operands (numbers/identifiers)


stack<char> operators; // Stack for operators (+, *, etc.)

for (char ch : expression) {

if (isspace(ch)) continue; // Ignore whitespace

// If the character is an operand (identifier or number)

if (isalnum(ch)) {

operands.push(createNode(string(1, ch)));

// If the character is an operator

else if (ch == '+' || ch == '*' || ch == '=') {

while (!operators.empty() &&

((ch == '+' && (operators.top() == '*' || operators.top() == '+')) ||

(ch == '*' && operators.top() == '*'))) {

// Pop the operator and create a new node

char op = operators.top();

operators.pop();

// Pop two operands

Node* right = operands.top(); operands.pop();

Node* left = operands.top(); operands.pop();

// Create a new node with the operator and the two operands

Node* operatorNode = createNode(string(1, op));

operatorNode->left = left;

operatorNode->right = right;

// Push the new subtree back to the operand stack

operands.push(operatorNode);

// Push the current operator onto the operator stack

operators.push(ch);

// Process the remaining operators in the stack

while (!operators.empty()) {

char op = operators.top();

operators.pop();
Node* right = operands.top(); operands.pop();

Node* left = operands.top(); operands.pop();

Node* operatorNode = createNode(string(1, op));

operatorNode->left = left;

operatorNode->right = right;

operands.push(operatorNode);

return operands.top();

int main() {

string expression = "a=b+c*d";

Node* root = constructParseTree(expression);

// Print the parse tree using inorder traversal

cout << "Parse Tree (In-Order Traversal): ";

printInOrder(root);

cout << endl;

return 0;

}
Parse Tree (In-Order Traversal):

/\

a +

/\

b *

/\

c d

You might also like