CD Assessment 1
CD Assessment 1
do if static while
Variables are the names used to represent variables, array, and functions in the input program.
Literals are strings use a double quote to store multiple characters.
Operators: {+,-,*,/,%,<,>,=,==,!=, >=, =<}.
Direct numerical values can be considered as Constants.
Special symbols or delimiters includes the symbols , ; ( ) { }
Test case 1
Sample
Input:
Enter Program $ for termination:
void main()
{
int a=20;
printf(“%d”,a);
}
Sample Output:
Lexeme of variables: a
Lexeme of literals: “%d”
Lexeme of operator : =
Lexeme of constants : 20
Lexeme of keywords : int printf void main
Lexeme of special symbols or delimiters: , ; ( ) { }
Test case 2
Sample
Input:
Enter Program $ for termination:
void main()
{
int a[3],t1,t2;
t1=2; a[0]=1; a[1]=2; a[t1]=3;
t2=-(a[2]+t1*6)/(a[2]-t1);
if t2>5 then
print(t2);
else {
int t3;
t3=99;
t2=-25;
print(-t1+t2*t3); /* this is a comment on 2 lines */
} endif
}$
Sample Output:
Lexeme of variables: a[3] t1 t2 t3
Lexeme of literals: “Lexical Analysis of a compiler”
Lexeme of operator : - + * / > =
Lexeme of constants : 2 1 3 6 5 99 -25
Lexeme of keywords : int printf if then else endif void main
Lexeme of special symbols or delimiters: , ; ( ) { }
Code:
#include <bits/stdc++.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
return flag;
}
int main()
{
char ch, buffer[15], b[30], logical_op[] = "><", math_op[] = "+-*/=",
numer[] = ".0123456789", other[] = ",;\(){}[]'':";
ifstream fin("input.txt");
int mark[1000] = {0};
int i, j = 0, kc = 0, ic = 0, lc = 0, mc = 0, nc = 0, oc = 0, aaa = 0;
vector<string> k;
vector<char> id;
vector<char> lo;
vector<char> ma;
vector<string> nu;
vector<char> ot;
if (!fin.is_open())
{
cout << "error while opening the file\n";
exit(0);
}
while (!fin.eof())
{
ch = fin.get();
for (i = 0; i < 12; ++i)
{
if (ch == other[i])
{
int aa = ch;
if (mark[aa] != 1)
{
ot.push_back(ch);
mark[aa] = 1;
++oc;
}
}
}
if (isalnum(ch))
buffer[j++] = ch;
else if ((ch == ' ' || ch == '\n') && (j != 0))
{
buffer[j] = '\0';
j = 0;
if (isKeyword(buffer) == 1)
{
k.push_back(buffer);
++kc;
}
else
{
if (buffer[0] >= 97 && buffer[0] <= 122)
{
if (mark[buffer[0] - 'a'] != 1)
{
id.push_back(buffer[0]);
++ic;
mark[buffer[0] - 'a'] = 1;
}
}
}
}
}
fin.close();
printf("Lexeme of keywords: ");
for (int f = 0; f < kc; ++f)
{
if (f == kc - 1)
cout << k[f] << "\n";
else
cout << k[f] << ", ";
}
printf("Lexeme of variables: ");
for (int f = 0; f < ic; ++f)
{
if (f == ic - 1)
cout << id[f] << "\n";
else
cout << id[f] << ", ";
}
printf("Lexeme of operators: ");
for (int f = 0; f < mc; ++f)
{
if (f == mc - 1)
cout << ma[f] << ", ";
else
cout << ma[f] << ", ";
}
return 0;
}
Output:
Aim : To write a C++ Program to create symbol table
a int 4 2024
Test case 2
Sample Input:
Sample Output:
Name Type Size Address
Code:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_map>
#include <cwctype>
#include <regex>
struct Symbol
{
string name;
string type;
int size;
int address;
};
int main()
{
string program;
cout << "Enter Program $ for termination: \n";
getline(cin, program, '$');
char a[][10] =
{"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", "printf", "main", "endif", "then"};