















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A compiler construction project where the author extends an existing lab code and writes a recursive descent parser for given operators. The code includes a lexical analyzer and various parser functions such as OREXP, ANDEXP, EQUALEXP, COMPEXP, SHFTEXP, exp, term, and fact. The document also includes a MakeEntry function for creating table entries and a Search function for finding table entries.
Typology: Assignments
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Tasks: Extend precious lab code and write recursive descent parser grammar and functions of operators given in Table 1. Code: #include
cout << "Syntax is correct.\n"; } else { cout << "Syntax is incorrect.\n"; } } } file.close(); } void MakeEntry(TokenType token, string stvalue) { Table[TableSize].tokenval = token; Table[TableSize].stringval = stvalue; TableSize++; } int Search(string ser) { for (int i = 0; i <= TableSize; i++) { if (ser == Table[i].stringval) { return i; } } TRecord.tokenval = ID; TRecord.stringval = ser; MakeEntry(TRecord.tokenval, TRecord.stringval); return TableSize - 1; }
TokenRecord LexicalAnalyzer() { while (!file.eof()) { ch = file.get(); if (ch == '/' || ch == ' ' || ch == '\n' || ch == '\t') { if (ch == '/') { ch = file.get(); if (ch == '/') { while (ch != '\n') { ch = file.get(); if (file.eof()) { return TRecord; } } file.seekg(-1, ios::cur); } else if (ch == '*') { while (ch != '\n') { ch = file.get(); if (file.eof()) { return TRecord; }
TRecord.stringval == "cout" || TRecord.stringval == "for" || TRecord.stringval == "switch" || TRecord.stringval == "default" || TRecord.stringval == "return") { if (TRecord.stringval == "struct") { TRecord.tokenval = STRUCT; } else if (TRecord.stringval == "void") { TRecord.tokenval = VOID; } else if (TRecord.stringval == "int") { TRecord.tokenval = INT; } else if (TRecord.stringval == "float") { TRecord.tokenval = FLOAT; } else if (TRecord.stringval == "double") { TRecord.tokenval = DOUBLE; } else if (TRecord.stringval == "char") { TRecord.tokenval = CHAR; } else if (TRecord.stringval == "enum") { TRecord.tokenval = ENUM;
else if (TRecord.stringval == "long") { TRecord.tokenval = LONG; } else if (TRecord.stringval == "main") { TRecord.tokenval = MAIN; } else if (TRecord.stringval == "if") { TRecord.tokenval = IF; } else if (TRecord.stringval == "else") { TRecord.tokenval = ELSE; } else if (TRecord.stringval == "bool") { TRecord.tokenval = BOOL; } else if (TRecord.stringval == "true") { TRecord.tokenval = TRUE; } else if (TRecord.stringval == "false") { TRecord.tokenval = FALSE; } else if (TRecord.stringval == "while") {
TRecord.tokenval = ID; Search(TRecord.stringval); return TRecord; //cout << "<" << "ID," << Search(TRecord.stringval) << "> "; } } else if (isdigit(ch)) { TRecord.tokenval = NUM; TRecord.stringval = ch; while (isdigit(ch)) { ch = file.get(); if (!isdigit(ch)) { break; } TRecord.stringval = TRecord.stringval + ch; } file.seekg(-1, ios::cur); return TRecord; //cout << "<" << "NUM," << TRecord.stringval << "> "; } else if (ch == '.' || ch == ',' || ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '=' || ch == '+' || ch == '-' || ch == '*' || ch == '%' || ch == '<' || ch == '>' || ch == '!' || ch == ';' || ch == '%' || ch == '%=' || ch == '&' || ch == '&&' || ch == '|' || ch == '||') { if (ch == '.') { TRecord.tokenval = DOT; TRecord.stringval = ".";
else if (ch == ',') { TRecord.tokenval = COMMA; TRecord.stringval = ","; } else if (ch == '(') { TRecord.tokenval = LRB; TRecord.stringval = "("; } else if (ch == ')') { TRecord.tokenval = RRB; TRecord.stringval = ")"; } else if (ch == '{') { TRecord.tokenval = LCB; TRecord.stringval = "{"; } else if (ch == '}') { TRecord.tokenval = RCB; TRecord.stringval = "}"; } else if (ch == '[') { TRecord.tokenval = LSB; TRecord.stringval = "["; }
TRecord.tokenval = PLUSPLUS; TRecord.stringval = "++"; } else if (ch == '=') { TRecord.tokenval = PLUSEQUAL; TRecord.stringval = "+="; } else { file.seekg(-1, ios::cur); TRecord.tokenval = PLUS; TRecord.stringval = "+"; } } else if (ch == '-') { ch = file.get(); if (ch == '-') { TRecord.tokenval = DECREMENT; TRecord.stringval = "--"; } else if (ch == '=') { TRecord.tokenval = MINUSEQUAL; TRecord.stringval = "-="; } else { file.seekg(-1, ios::cur);
TRecord.tokenval = MINUS; TRecord.stringval = "-"; } } else if (ch == '') { ch = file.get(); if (ch == '=') { TRecord.tokenval = MULEQUAL; TRecord.stringval = "="; } else { file.seekg(-1, ios::cur); TRecord.tokenval = MUL; TRecord.stringval = "*"; } } else if (ch == '<') { ch = file.get(); if (ch == '=') { TRecord.tokenval = LESSEQ; TRecord.stringval = "<="; } else if (ch == '<') { TRecord.tokenval = LESSER; TRecord.stringval = "<<";
if (ch == '=') { TRecord.tokenval = MODEQUAL; TRecord.stringval = "%="; } else { file.seekg(-1, ios::cur); TRecord.tokenval = MOD; TRecord.stringval = "%"; } } else if (ch == '!') { ch = file.get(); if (ch == '=') { TRecord.tokenval = NOTEQ; TRecord.stringval = "!="; } else { file.seekg(-1, ios::cur); TRecord.tokenval = NOT; TRecord.stringval = "!"; } } else if (ch == '&') { ch = file.get(); if (ch == '&')
TRecord.tokenval = COMPAND; TRecord.stringval = "&&"; } else { file.seekg(-1, ios::cur); TRecord.tokenval = AND; TRecord.stringval = "&"; } } else if (ch == '|') { ch = file.get(); if (ch == '|') { TRecord.tokenval = COMPOR; TRecord.stringval = "||"; } else { file.seekg(-1, ios::cur); TRecord.tokenval = OR; TRecord.stringval = "|"; } } //return TRecord; //cout << "<" << TRecord.stringval << "> "; } } }
flag == true; } bool OREXP() { do { if (!ANDEXP()) { return false; } t = getT(); } while (t.tokenval == COMPOR); putT(); return true; } bool ANDEXP() { do { if (!EQUALEXP()) { return false; } t = getT(); } while (t.tokenval == COMPAND); putT(); return true; } bool EQUALEXP() {
do { if (!COMPEXP()) { return false; } t = getT(); } while (t.tokenval == ASSIGN || t.tokenval == NOTEQ); putT(); return true; } bool COMPEXP() { do { if (!SHFTEXP()) return false; t = getT(); } while (t.tokenval == GREATEREQ || t.tokenval == LESSEQ || t.tokenval == LESS || t.tokenval == GREATER); putT(); return true; } bool SHFTEXP() { do { if (!exp()) { return false; }