cc lab 11 complete (1).docx - Bahria University Lahore Campus Department of Computer Sciences Lab Journal 11(Fall 2018 Course Compiler Construction Lab

cc lab 11 complete (1).docx - Bahria University Lahore...

This preview shows page 1 - 4 out of 10 pages.

Bahria University, Lahore Campus Department of Computer Sciences Lab Journal 11 (Fall 2018) Course: Compiler Construction Lab Date: Course Code: CSL-323 Max Marks: 20 Faculty’s Name: Nadeem Server Lab Engineer : Atta ur rehman Name: Muhammad Usman Khalid Enroll No: 03-134161-024 Lab Tasks: Solution: Task 1 : typedef enum { ifK, AssignK, doK, whileK }StmtKind; typedef enum { OpK = 1, ConstK, IdK } ExpKind; struct treeNode { StmtKind stmt; ExpKind exp; tokentype tokn; token opr; int val; treeNode * leftchild; treeNode * rightchild; treeNode * sibling; }; treeNode *factor(){ treeNode *t=NULL , *p=NULL; token tkn; tkn = gettoken(); if (tkn.entryno == "DIG" || tkn.entryno == "UID") {t = new treeNode(); t->leftchild = NULL; t->rightchild = NULL;
Image of page 1
t->opr = tkn; if (tkn.entryno=="UID") {t->exp = IdK; t->tokn = UID;} if (tkn.entryno == "DIG") {t->exp = ConstK; t->val = tkn.num;} return t;} else if (tkn.entryno == "LPRN") {p = expr(); if (p != NULL) {tkn = gettoken(); if (tkn.entryno == "RPRN") {return p;} else {return NULL;} } } else {ugt(); return NULL;} } treeNode *term() {token tkn; treeNode *t=NULL, *p=NULL; bool flag = 0; do{ t = factor(); if (flag == 1) {flag = 0; p->rightchild = t; t = p;} tkn = gettoken(); if (tkn.entryno == "MUL" || tkn.entryno == "DIV") {flag = 1; p = new treeNode(); p->exp = OpK; p->opr = tkn; p->leftchild = t;} else {ugt(); return t;} } while (tkn.entryno == "MUL" || tkn.entryno == "DIV"); } treeNode *expr() {token tkn; treeNode *t=NULL, *p=NULL; bool flag = 0; do {t = term(); if (flag == 1) {flag = 0; p->rightchild = t; t = p;} if (flag == 0) {tkn = gettoken(); if (tkn.entryno == "PLUS" || tkn.entryno == "MINUS") {flag = 1; p = new treeNode();
Image of page 2
p->exp = OpK; p->opr = tkn; p->leftchild = t;} else {ugt(); return t;}}} while (tkn.entryno == "PLUS" || tkn.entryno == "MINUS");}
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 10 pages?

  • Fall '18
  • Zia Ul Rehman
  • ASCII, Void type, return p, return t, tokentype tkn

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture