Posts

Showing posts from December, 2023

Practice of LEX / YACC in compiler writing.

Image
 // Write a program to print “Compiler” when input is “Hi” else print “Wrong Input” %{ #include <stdio.h> %} %% ^Hi$ { printf("Compiler\n"); } .|\n { printf("Wrong Input\n"); } %% int main() {  yylex(); // Start lexical analysis  return 0; } int yywrap(){  return 0; } // Write a program to check whether a number is even or odd. %{ #include <stdio.h> %} %% [0-9]+ {  if (atoi(yytext) % 2 == 0)  printf("Even\n");  else  printf("Odd\n");} .* { printf("Wrong Input\n"); } %% int main() {  yylex(); // Start lexical analysis  return 0;} int yywrap(){  return 0; } // Write a program to find greatest of two numbers. %{ #include <stdio.h> int a, b, c = 0; %} %% [0-9]+ {  if (c == 0)  a = atoi(yytext);  else  b = atoi(yytext); } \n {  if (a > b)  printf("%d is greater\n", a);  else if (a < b)  printf("%d is greater\n", b);  else  printf("Both are equal\n");  c = 0; }...

Write a program to check whether a grammar is operator precedent.

Image
#include <iostream> #include <vector> #include <unordered_set> using namespace std; // Data structure to represent a production rule struct Production {     string left;     string right; }; // Function declarations unordered_set<string> collectTerminals(const vector<Production> &productions); bool checkOverlappingSymbols(const unordered_set<string> &terminals, const unordered_set<string> &operators); bool checkAdjacentNonTerminals(const vector<Production> &productions); // Function to check if the grammar is operator precedence bool isOperatorPrecedence(const vector<Production> &productions) {     unordered_set<string> terminals = collectTerminals(productions);     unordered_set<string> operators;     // Collect operators     for (const Production &production : productions)     {         for (char symbol : pr...

Write a program to find out the FIRST of the Non‐terminals in a grammar.

Image
 #include <iostream> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; // Data structure to represent a production rule struct Production {     string nonTerminal;     string expression; }; // Function to calculate the FIRST set for a non-terminal unordered_set<char> calculateFirst(const string &nonTerminal, const vector<Production> &productions, unordered_map<string, unordered_set<char>> &firstCache) {     // If FIRST set for the non-terminal is already calculated, return it     if (firstCache.find(nonTerminal) != firstCache.end())     {         return firstCache[nonTerminal];     }     unordered_set<char> firstSet;     for (const Production &production : productions)     {         if (production.nonTerminal == nonTerminal)       ...

Write a program to Implement Shift Reduce parsing for a String. 

Image
 #include <iostream> #include <stack> #include <vector> #include <unordered_map> using namespace std; vector<string> productions = {"E->E+T", "E->T", "T->T*F", "T->F", "F->(E)", "F->id"}; unordered_map<string, unordered_map<string, string>> parsingTable = {     {"E", {{"(", "shift T"}, {"id", "shift T"}}},     {"T", {{"(", "shift F"}, {"id", "shift F"}}},     {"F", {{"(", "shift (E)"}, {"id", "shift id"}}} }; void shift(stack<string> &parseStack, vector<string> &input) {     parseStack.push(input[0]);     input.erase(input.begin()); } void reduce(stack<string> &parseStack, const string &nonTerminal) {     if (parsingTable.find(nonTerminal) != parsingTable.end()) {         string producti...

Write a program to find out the leading of the non-terminals in a grammar. 

Image
 #include <iostream> #include <vector> #include <unordered_map> #include <set> #include <sstream> using namespace std; void findLeading(const vector<string>& productions, unordered_map<char, set<char>>& leading) {     bool changed = true;     for (const string& production : productions) {         char head = production[0];         leading[head] = set<char>();     }     while (changed) {         changed = false;         for (const string& production : productions) {             char head = production[0];             const string& body = production.substr(3);             if (body == "$") {                 if (leading[head].find('$') == leading[head].end()) {   ...

Write a program to show all the operations of a stack

Image
 #include <iostream> #include <vector> using namespace std; class Stack { private:     vector<int> stack; // Using a vector to implement the stack public:     void push(int element)     {         stack.push_back(element);         cout << "Pushed: " << element << endl;     }     void pop()     {         if (!stack.empty())         {             int poppedElement = stack.back();             stack.pop_back();             cout << "Popped: " << poppedElement << endl;         }         else         {             cout << "Stack underflow: Cannot pop from an empty stack." << endl;         } ...

Write a program to perform left factoring on a grammer.

Image
#include <stdio.h> #include <string.h> void leftFactor(char part1[], char part2[]) {     char modifiedGram[20], newGram[20];     int i, j = 0, k = 0, pos;     for (i = 0; part1[i] != '\0' && part2[i] != '\0'; i++) {         if (part1[i] == part2[i]) {             modifiedGram[k] = part1[i];             k++;             pos = i + 1;         }     }     modifiedGram[k] = 'X';     modifiedGram[++k] = '\0';     for (i = pos, j = 0; part1[i] != '\0'; i++, j++) {         newGram[j] = part1[i];     }     newGram[j++] = '|';     for (i = pos, j = 0; part2[i] != '\0'; i++, j++) {         newGram[j] = part2[i];     }     newGram[j] = '\0';     printf("\nGrammar Without Left Factorin...

Write a program to remove left recursion from a grammar.

Image
 #include <bits/stdc++.h> using namespace std; vector<string> g; class NonTerminal {     string name;     vector<string> productionRules; public:     NonTerminal(string name) {         this->name = name;     }     string getName() {         return name;     }     void setRules(vector<string> rules) {         productionRules.clear();         for (auto rule : rules) {             productionRules.push_back(rule);         }     }     vector<string> getRules() {         return productionRules;     }     void addRule(string rule) {         productionRules.push_back(rule);     }     void printRule() {         string toPrint = "";   ...

Aim: Write a program to check whether a string include Keyword or not.

Image
 #include <bits/stdc++.h> using namespace std; int countkeywords = 0; unordered_map<string, int> m; void LPSArray(string pattern, int M, int* lps) {     int len = 0;     lps[0] = 0;     int i = 1;     while (i < M) {         if (pattern[i] == pattern[len]) {             len++;             lps[i] = len;             i++;         } else {             if (len != 0) {                 len = lps[len - 1];             } else {                 lps[i] = 0;                 i++;             }         }     } } void KMPSearch(string pattern, string txt) {     int M = ...

Aim: Write a program to check wether a string belongs to the grammer or not.

Image
  #include <iostream> using namespace std; bool checkGrammar1(string str) {     if (str[0] != 'a' || str[str.length() - 1] != 'b') {         return false;     }     for (int i = 1; i < str.length(); i++) {         if (str[i - 1] == 'b' && str[i] == 'a') {             return false;         }     }     return true; } bool checkGrammar2(string str) {     int n = str.length();     if (n % 2 == 0) {         return false;     }     for (int i = 0; i < n / 2; i++) {         if ((str[i] != 'a' && str[i] != 'b') || (str[n - 1 - i] != 'a' && str[n - 1 - i] != 'b')) {             return false;         }         if (str[i] != str[n - 1 - i]) {         ...