Compiler Construction: Extending Precious Lab Code and Writing Recursive Descent Parser, Assignments of Compiler Design

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

2020/2021

Uploaded on 05/30/2021

danial-5
danial-5 🇦🇺

1 document

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Compiler Construction
Journal # 8
Submitted By: DANYAL HAIDER
ENROLLMENT: 01-134191-011
Submitted To: Mrs Anmol Hamid
Class and Section: BS-CS-5A
Date: 6/05/2021
Department of Computer Sciences
BAHRIA UNIVERSITY, ISLAMABAD
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Compiler Construction: Extending Precious Lab Code and Writing Recursive Descent Parser and more Assignments Compiler Design in PDF only on Docsity!

Compiler Construction

Journal # 8

Submitted By: DANYAL HAIDER

ENROLLMENT: 01-134191-

Submitted To: Mrs Anmol Hamid

Class and Section: BS-CS-5A

Date: 6 / 05 /202 1

Department of Computer Sciences

BAHRIA UNIVERSITY, ISLAMABAD

Tasks: Extend precious lab code and write recursive descent parser grammar and functions of operators given in Table 1. Code: #include #include #include #include using namespace std; enum TokenType { DOT = '.', COMMA = ',', LRB = '(', RRB = ')', LCB = '{', RCB = '}', LSB = '[', RSB = ']', SEMICOLON = ';', EQUAL = '=', ASSIGN = '==', MODEQUAL = '%=', PLUSEQUAL = '+=', PLUS = '+', MINUS = '-', MOD = '%', DECREMENT = '--', PLUSPLUS = '++', MINUSEQUAL = '-=', MUL = '', DIV = '/', MULEQUAL = '=', LESS = '<', LESSEQ = '<=', LESSER = '<<', GREATERT = '>>', GREATER = '>', GREATEREQ = '>=', NOTEQ = '!=', NOT = '!', AND = '&', COMPAND = '&&', OR = '|', COMPOR = '||', STRUCT, VOID, INT, FLOAT, DOUBLE, CHAR, ENUM, LONG, MAIN, IF, ELSE, BOOL, TRUE, FALSE, WHILE, DO, COUT, FOR, SWITCH, DEFAULT, RETURN, ID, NUM }; struct TokenRecord { TokenType tokenval; string stringval; int numval; }TRecord; fstream file("C:\Users\Daddy\Desktop\danyal.txt", ios::in); char ch; int TableSize = 0; TokenRecord Table[50], ct, t; bool flag = false;

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; }