lexical analyzer (Tokens)???
im having a problem solving the 6 errors i have will someone help me with this im stuck???!
lexical analyzer.cpp
lexical analyzer.cpp(36) : error C2065: 'token' : undeclared identifier
lexical analyzer.cpp(96) : error C2601: 'add_token' : local function definitions are illegal
lexical analyzer.cpp(113) : error C2143: syntax error : missing ';' before '{'
lexical analyzer.cpp(125) : error C2601: 'display' : local function definitions are illegal
lexical analyzer.cpp(136) : error C2601: 'add_premitives' : local function definitions are illegal
lexical analyzer.cpp(173) : error C2601: 'main' : local function definitions are illegal
Error executing cl.exe.
lexical analyzer.obj - 6 error(s), 0 warning(s) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include<iostream>
#include<process.h>
#include<conio.h>
#include<string>
using namespace std;
// we are using singly linked list, because the symbol table DO NOT traverse backwardly.
// for simplisity, we are creating a symbol table for a premitive language which do no include procedures and functions.
// Its so limited according to the control structure. Thus, we did not include except the if, for statements.
// And its limited too that it does not take into consinderation the values of variables, types. because this is a lexical analayzer implementation project.
struct node
{
string data;
node* next;
};
class source_code
{
node* head;
public:
source_code()
{
head = NULL;
}
void display();
void add_lexeme(string);
~source_code();
};
void source_code::add_lexeme(string lexeme)
{
node* p = new node;
p->next = NULL;
p->data = token;
if (head = NULL)
head = p;
else
{
node* q = NULL;
for (q=head ; q->next != NULL ; q = q->next);
q->next = p;
p->next = NULL;
}
}
source_code::~source_code()
{
node* p;
while (head != NULL)
{
p = head;
head = head->next;
delete p;
}
head = NULL;
}
void source_code::display()
{
cout<<" ";
for (node* p = head ; p != NULL ; p = p->next)
{
cout << p->data;
cout << endl;
}
cout << endl;
}
class symbol_table
{
node* head;
public:
symbol_table()
{
head = NULL;
}
void display();
void add_token(string);
symbol_table add_premitives(symbol_table &);
bool lexical_analysis(string input);
~symbol_table();
};
bool symbol_table::lexical_analysis(string input)
{
source_code sc;
void symbol_table::add_token(string token)
{
node* p = new node;
p->next = NULL;
p->data = token;
if (head = NULL)
head = p;
else
{
node* q = NULL;
for (q=head ; q->next != NULL ; q = q->next);
q->next = p;
p->next = NULL;
}
}
symbol_table::~symbol_table()
{
node* p;
while (head != NULL)
{
p = head;
head = head->next;
delete p;
}
head = NULL;
}
void symbol_table::display()
{
cout<<" ";
for (node* p = head ; p != NULL ; p = p->next)
{
cout << p->data;
cout << endl;
}
cout << endl;
}
symbol_table symbol_table::add_premitives(symbol_table &st)
{
symbol_table::add_token("int DT");
symbol_table::add_token("char DT");
symbol_table::add_token("float DT");
symbol_table::add_token("double DT");
symbol_table::add_token("bool DT");
symbol_table::add_token("+ MOP");
symbol_table::add_token("- MOP");
symbol_table::add_token("* MOP");
symbol_table::add_token("/ MOP");
symbol_table::add_token("< ROP");
symbol_table::add_token("> ROP");
symbol_table::add_token("== ROP");
symbol_table::add_token("!= ROP");
symbol_table::add_token("<= ROP");
symbol_table::add_token(">= ROP");
symbol_table::add_token("&& LOP");
symbol_table::add_token("|| LOP");
symbol_table::add_token("if CSS");
symbol_table::add_token("for CSS");
symbol_table::add_token("0 const");
symbol_table::add_token("1 const");
symbol_table::add_token("2 const");
symbol_table::add_token("3 const");
symbol_table::add_token("4 const");
symbol_table::add_token("5 const");
symbol_table::add_token("6 const");
symbol_table::add_token("7 const");
symbol_table::add_token("8 const");
symbol_table::add_token("9 const");
return st;
}
void main()
{
symbol_table st;
st.display();
//st = st.add_premitives(st);
cout <<"Done successfully";
string input;
cin >> input;
while ( input != "exit")
{
// checking
cin >> input;
}
}
}
| |
The messages are telling you everything that you need to know. Hint: In the following function, where is token defined?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
void source_code::add_lexeme(string lexeme)
{
node* p = new node;
p->next = NULL;
p->data = token;
if (head = NULL)
head = p;
else
{
node* q = NULL;
for (q=head ; q->next != NULL ; q = q->next);
q->next = p;
p->next = NULL;
}
}
| |
Topic archived. No new replies allowed.