Untitled
unknown
plain_text
2 years ago
19 kB
0
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // for lex #define MAXLEN 256 int idx; // Token types typedef enum { UNKNOWN, END, ENDFILE, INT, ID, ADDSUB, MULDIV, ASSIGN, LPAREN, RPAREN, INCDEC, AND, OR, XOR } TokenSet; TokenSet getToken(void); TokenSet curToken = UNKNOWN; char lexeme[MAXLEN]; // Test if a token matches the current token int match(TokenSet token); // Get the next token void advance(void); // Get the lexeme of the current token char *getLexeme(void); // for parser #define TBLSIZE 64 // Set PRINTERR to 1 to print error message while calling error() // Make sure you set PRINTERR to 0 before you submit your code #define PRINTERR 1 // Call this macro to print error message and exit the program // This will also print where you called it in your program #define error(errorNum) { \ if (PRINTERR) \ fprintf(stderr, "error() called at %s:%d: ", __FILE__, __LINE__); \ err(errorNum); \ } // Error types typedef enum { UNDEFINED, MISPAREN, NOTNUMID, NOTFOUND, RUNOUT, NOTLVAL, DIVZERO, SYNTAXERR } ErrorType; // Structure of the symbol table typedef struct { int val; char name[MAXLEN]; } Symbol; // Structure of a tree node typedef struct _Node { TokenSet data; int val; int pos; char lexeme[MAXLEN]; struct _Node *left; struct _Node *right; } BTNode; int sbcount = 0; Symbol table[TBLSIZE]; // Initialize the symbol table with builtin variables void initTable(void); // Get the value of a variable int getval(char *str); // Set the value of a variable int setval(char *str, int val); // Make a new node according to token type and lexeme BTNode *makeNode(TokenSet tok, const char *lexe); // Free the syntax tree void freeTree(BTNode *root); BTNode *factor(void); BTNode *term(void); BTNode *term_tail(BTNode *left); BTNode *expr(void); BTNode *expr_tail(BTNode *left); BTNode *assign_expr(void); BTNode *or_expr(void); BTNode *or_expr_tail(BTNode *left); BTNode *xor_expr(void); BTNode *xor_expr_tail(BTNode *left); BTNode *and_expr(void); BTNode *and_expr_tail(BTNode *left); BTNode *addsub_expr(void); BTNode *addsub_expr_tail(BTNode *left); BTNode *muldiv_expr(void); BTNode *muldiv_expr_tail(BTNode *left); BTNode *unary_expr(void); void backtoletters(char* lex); void statement(void); // Print error message and exit the program void err(ErrorType errorNum); // for codeGen // Evaluate the syntax tree int evaluateTree(BTNode *root); // Print the syntax tree in prefix void printPrefix(BTNode *root); /*============================================================================================ lex implementation ============================================================================================*/ TokenSet getToken(void) { int i = 0; char c = '\0'; while ((c = fgetc(stdin)) == ' ' || c == '\t'); if (isdigit(c)) { lexeme[0] = c; c = fgetc(stdin); i = 1; while (isdigit(c) && i < MAXLEN) { lexeme[i] = c; ++i; c = fgetc(stdin); } ungetc(c, stdin); lexeme[i] = '\0'; return INT; } else if (c == '+' || c=='-') { lexeme[0] = c; c=fgetc(stdin); if(c=='+' || c=='-'){ lexeme[1]=c; lexeme[2]='\0'; return INCDEC; } else{ ungetc(c,stdin); lexeme[1]='\0'; return ADDSUB; } }else if (c == '*' || c == '/') { lexeme[0] = c; lexeme[1] = '\0'; return MULDIV; } else if (c == '&') { lexeme[0] = c; lexeme[1] = '\0'; return AND; } else if (c == '|') { lexeme[0] = c; lexeme[1] = '\0'; return OR; } else if (c == '^') { lexeme[0] = c; lexeme[1] = '\0'; return XOR; } else if (c == '\n') { lexeme[0] = c; lexeme[1] = '\0'; return END; } else if (c == '=') { strcpy(lexeme, "="); return ASSIGN; } else if (c == '(') { strcpy(lexeme, "("); return LPAREN; } else if (c == ')') { strcpy(lexeme, ")"); return RPAREN; } else if (isalpha(c) || c=='_') { lexeme[0]=c; c=fgetc(stdin); i=1; while ((isalpha(c) || c=='_') && i<MAXLEN){ lexeme[i] = c; ++i; c=fgetc(stdin); } ungetc(c,stdin); lexeme[i]='\0'; return ID; } else if (c == EOF) { return ENDFILE; } else { return UNKNOWN; } } void advance(void) { curToken = getToken(); } int match(TokenSet token) { if (curToken == UNKNOWN) advance(); return token == curToken; } char *getLexeme(void) { return lexeme; } /*============================================================================================ parser implementation ============================================================================================*/ void initTable(void) { strcpy(table[0].name, "x"); table[0].val = 0; strcpy(table[1].name, "y"); table[1].val = 0; strcpy(table[2].name, "z"); table[2].val = 0; sbcount = 3; } int getval(char *str) { int i = 0; int flag=0; for (i = 0; i < sbcount; i++) if (strcmp(str, table[i].name) == 0){ flag=1; return table[i].val; } if(!flag) error(NOTFOUND); if (sbcount >= TBLSIZE) error(RUNOUT); strcpy(table[sbcount].name, str); table[sbcount].val = 0; sbcount++; return 0; } int setval(char *str, int val) { int i = 0; for (i = 0; i < sbcount; i++) { if (strcmp(str, table[i].name) == 0) { table[i].val = val; return val; } } if (sbcount >= TBLSIZE) error(RUNOUT); strcpy(table[sbcount].name, str); table[sbcount].val = val; sbcount++; return val; } BTNode *makeNode(TokenSet tok, const char *lexe) { BTNode *node = (BTNode*)malloc(sizeof(BTNode)); strcpy(node->lexeme, lexe); node->data = tok; node->val = 0; node->left = NULL; node->right = NULL; return node; } void freeTree(BTNode *root) { if (root != NULL) { freeTree(root->left); freeTree(root->right); free(root); } } void backtoletters(char* lex){ int len=strlen(lex); for(int i=len-1;i>=0;i--){ ungetc(lex[i],stdin); } } //assign_expr := ID ASSIGN assign_expr | or_expr BTNode *assign_expr(void) { BTNode *left=NULL,*retp=NULL; if (match(ID)) { left = makeNode(ID, getLexeme()); advance(); if (!match(ASSIGN)) { backtoletters(getLexeme()); backtoletters(left->lexeme); advance(); return or_expr(); } else { retp = makeNode(ASSIGN, getLexeme()); advance(); retp->left = left; retp->right = assign_expr(); return retp; } } else return or_expr(); } //or_expr := xor_expr or_expr_tail //or_expr_tail := OR xor_expr or_expr_tail | NiL BTNode *or_expr(void) { BTNode *node = xor_expr(); return or_expr_tail(node); } BTNode* or_expr_tail(BTNode *left){ BTNode *node = NULL; if (match(OR)) { node = makeNode(OR, getLexeme()); advance(); node->left = left; node->right = xor_expr(); return or_expr_tail(node); } else { return left; } } //xor_expr := and_expr xor_expr_tail //xor_expr_tail := XOR and_expr xor_expr_tail | NiL BTNode *xor_expr(void) { BTNode *node = and_expr(); return xor_expr_tail(node); } BTNode* xor_expr_tail(BTNode *left){ BTNode *node = NULL; if (match(XOR)) { node = makeNode(XOR, getLexeme()); advance(); node->left = left; node->right = and_expr(); return xor_expr_tail(node); } else { return left; } } //and_expr := addsub_expr and_expr_tail //and_expr_tail := AND addsub_expr and_expr_tail | NiL BTNode *and_expr(void) { BTNode *node = addsub_expr(); return and_expr_tail(node); } BTNode *and_expr_tail(BTNode *left){ BTNode *node = NULL; if (match(AND)) { node = makeNode(AND, getLexeme()); advance(); node->left = left; node->right = addsub_expr(); return and_expr_tail(node); } else { return left; } } //addsub_expr := muldiv_expr addsub_expr_tail //addsub_expr_tail := ADDSUB muldiv_expr addsub_expr_tail | NiL BTNode *addsub_expr(void) { BTNode *node = muldiv_expr(); return addsub_expr_tail(node); } BTNode* addsub_expr_tail(BTNode *left){ BTNode *node = NULL; if (match(ADDSUB)) { node = makeNode(ADDSUB, getLexeme()); advance(); node->left = left; node->right = muldiv_expr(); return addsub_expr_tail(node); } else { return left; } } //muldiv_expr := unary_expr muldiv_expr_tail //muldiv_expr_tail := MULDIV unary_expr muldiv_expr_tail | NiL BTNode *muldiv_expr(void) { BTNode *node = unary_expr(); return muldiv_expr_tail(node); } BTNode* muldiv_expr_tail(BTNode *left){ BTNode *node = NULL; if (match(MULDIV)) { node = makeNode(MULDIV, getLexeme()); advance(); node->left = left; node->right = unary_expr(); return muldiv_expr_tail(node); } else { return left; } } //unary_expr := ADDSUB unary_expr | factor BTNode *unary_expr(void) { BTNode *retp=NULL,*left=NULL; if(match(ADDSUB)){ retp=makeNode(ADDSUB,getLexeme()); advance(); retp->left=makeNode(ADDSUB,"0"); retp->right=unary_expr(); }else retp=factor(); return retp; } //factor := INT | ID | INCDEC ID | LPAREN assign_expr RPAREN BTNode *factor(void) { BTNode *retp = NULL, *left = NULL; if (match(INT)) { retp = makeNode(INT, getLexeme()); advance(); } else if(match(ID)){ retp=makeNode(INT,getLexeme()); advance(); }else if(match(INCDEC)){ retp = makeNode(INCDEC,getLexeme()); retp->left=makeNode(INT,"1"); advance(); if (match(ID)) { retp->right=makeNode(ID,getLexeme()); advance(); } } else if (match(LPAREN)) { //remember to encode INCDEC advance(); retp = assign_expr(); if (match(RPAREN)) advance(); else error(MISPAREN); } else { error(NOTNUMID); } return retp; } // statement := ENDFILE | END | expr END void statement(void) { BTNode *retp = NULL; int lhs,temp; if (match(ENDFILE)) { printf("MOV r0 [0]\n"); printf("MOV r1 [4]\n"); printf("MOV r2 [8]\n"); printf("EXIT 0\n"); exit(0); } else if (match(END)) { //printf(">> "); advance(); } else { retp = assign_expr(); if (match(END)) { idx=0; temp=evaluateTree(retp); freeTree(retp); advance(); } else { error(SYNTAXERR); } } } void err(ErrorType errorNum) { printf("EXIT \n"); if (PRINTERR) { fprintf(stderr, "error: "); switch (errorNum) { case MISPAREN: fprintf(stderr, "mismatched parenthesis\n"); break; case NOTNUMID: fprintf(stderr, "number or identifier expected\n"); break; case NOTFOUND: fprintf(stderr, "variable not defined\n"); break; case RUNOUT: fprintf(stderr, "out of memory\n"); break; case NOTLVAL: fprintf(stderr, "lvalue required as an operand\n"); break; case DIVZERO: fprintf(stderr, "divide by constant zero\n"); break; case SYNTAXERR: fprintf(stderr, "syntax error\n"); break; default: fprintf(stderr, "undefined error\n"); break; } } exit(0); } /*============================================================================================ codeGen implementation ============================================================================================*/ int evaluateTree(BTNode *root) { int retval = 0, lv = 0, rv = 0; if (root != NULL) { switch (root->data) { case ID: retval = getval(root->lexeme); root->pos=idx; for(int i=0;i<sbcount;i++){ if(strcmp(root->lexeme,table[i].name)==0){ printf("MOV r%d [%d]\n",idx,4*i); break; } } idx++; break; case INT: retval = atoi(root->lexeme); root->pos=idx; printf("MOV r%d %d\n",idx,retval); idx++; break; case XOR: lv = evaluateTree(root->left); rv = evaluateTree(root->right); lv^rv; printf("XOR r%d r%d\n",root->left->pos,root->right->pos); root->pos=root->left->pos; idx--; break; case AND: lv = evaluateTree(root->left); rv = evaluateTree(root->right); lv&rv; printf("AND r%d r%d\n",root->left->pos,root->right->pos); root->pos=root->left->pos; break; case OR: lv = evaluateTree(root->left); rv = evaluateTree(root->right); lv|rv; printf("OR r%d r%d\n",root->left->pos,root->right->pos); root->pos=root->left->pos; break; case ASSIGN: rv = evaluateTree(root->right); retval = setval(root->left->lexeme, rv); for(int i=0;i<sbcount;i++){ if(strcmp(root->left->lexeme,table[i].name)==0){ printf("MOV [%d] r%d\n",4*i,root->right->pos); idx++; break; } } root->pos=root->right->pos; break; case ADDSUB: case INCDEC: case MULDIV: lv = evaluateTree(root->left); rv = evaluateTree(root->right); if (strcmp(root->lexeme, "++") == 0) { retval = lv + rv; setval(root->right->lexeme,rv+1); printf("ADD r%d r%d\n",root->right->pos,root->left->pos); for(int i=0;i<sbcount;i++){ if(strcmp(root->right->lexeme,table[i].name)==0){ printf("MOV [%d] r%d\n",4*i,root->right->pos); break; } } root->pos=root->left->pos; } else if (strcmp(root->lexeme, "--") == 0) { retval = lv - rv; setval(root->right->lexeme,rv-1); printf("SUB r%d r%d\n",root->right->pos,root->left->pos); for(int i=0;i<sbcount;i++){ if(strcmp(root->right->lexeme,table[i].name)==0){ printf("MOV [%d] r%d\n",4*i,root->right->pos); break; } } root->pos=root->left->pos; } else if (strcmp(root->lexeme, "*") == 0) { retval = lv * rv; printf("MUL r%d r%d\n",root->left->pos,root->right->pos); root->pos=root->left->pos; idx--; } else if (strcmp(root->lexeme, "/") == 0) { if (rv == 0) error(DIVZERO); retval = lv / rv; printf("DIV r%d r%d\n",root->left->pos,root->right->pos); root->pos=root->left->pos; idx--; } else if (strcmp(root->lexeme, "+") == 0){ retval = lv + rv; printf("ADD r%d r%d\n",root->right->pos,root->left->pos); root->pos=root->left->pos; idx--; } else if (strcmp(root->lexeme, "-") == 0){ retval = lv - rv; printf("SUB r%d r%d\n",root->right->pos,root->left->pos); root->pos=root->left->pos; idx--; }/*else if (strcmp(root->lexeme, "&") == 0) { retval = (lv & rv); } else if (strcmp(root->lexeme, "|") == 0) { retval = (lv | rv); } else if (strcmp(root->lexeme, "^") == 0) { retval = (lv ^ rv); }*/ break; default: retval = 0; } } return retval; } void printPrefix(BTNode *root) { if (root != NULL) { printf("%s ", root->lexeme); printPrefix(root->left); printPrefix(root->right); } } /*============================================================================================ main ============================================================================================*/ // This package is a calculator // It works like a Python interpretor // Example: // >> y = 2 // >> z = 2 // >> x = 3 * y + 4 / (2 * z) // It will print the answer of every line // You should turn it into an expression compiler // And print the assembly code according to the input // This is the grammar used in this package // You can modify it according to the spec and the slide // statement := ENDFILE | END | expr END // expr := term expr_tail // expr_tail := ADDSUB term expr_tail | NiL // term := factor term_tail // term_tail := MULDIV factor term_tail| NiL // factor := INT | ADDSUB INT | // ID | ADDSUB ID | // ID ASSIGN expr | // LPAREN expr RPAREN | // ADDSUB LPAREN expr RPAREN int main() { initTable(); printf(">> "); while (1) { statement(); } return 0; }