calc -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include <ctype.h> #include <stack> #include <cmath> #include <iostream> using namespace std; /*** Enums and Print Functions for Terminals and Non-Terminals **********************/ #define MAX_SYMBOL_NAME_SIZE 25 //all of the terminals in the language typedef enum { T_eof = 0, // 0: end of file T_num, // 1: numbers T_plus, // 2: + T_minus, // 3: - T_times, // 4: * T_mod, // 5: mod T_semicolon, // 6: ; T_bar, // 7: | T_openparen, // 8: ( T_closeparen // 9: ) } token_type; // This function returns a string for the token. It is used in the parsetree_t // class to actually dump the parsetree to a dot file (which can then be turned // into a picture). Note that the return char* is a reference to a local copy // and it needs to be duplicated if you are a going require multiple instances // simultaniously. // // No need to do anything here char* token_to_string(token_type c) { static char buffer[MAX_SYMBOL_NAME_SIZE]; switch( c ) { case T_eof: strncpy(buffer,"eof",MAX_SYMBOL_NAME_SIZE); break; case T_num: strncpy(buffer,"num",MAX_SYMBOL_NAME_SIZE); break; case T_plus: strncpy(buffer,"+",MAX_SYMBOL_NAME_SIZE); break; case T_minus: strncpy(buffer,"-",MAX_SYMBOL_NAME_SIZE); break; case T_times: strncpy(buffer,"*",MAX_SYMBOL_NAME_SIZE); break; case T_mod: strncpy(buffer,"mod",MAX_SYMBOL_NAME_SIZE); break; case T_semicolon: strncpy(buffer,";",MAX_SYMBOL_NAME_SIZE); break; case T_bar: strncpy(buffer,"|",MAX_SYMBOL_NAME_SIZE); break; case T_openparen: strncpy(buffer,"(",MAX_SYMBOL_NAME_SIZE); break; case T_closeparen: strncpy(buffer,")",MAX_SYMBOL_NAME_SIZE); break; default: strncpy(buffer,"unknown_token",MAX_SYMBOL_NAME_SIZE); break; } return buffer; } // All of the non-terminals in the grammar. You need to add these in // according to your grammar. These are used for printing the thing out, so // please follow the convention we set up so that we can tell what the heck // you are doing when I grade. typedef enum { epsilon = 100, NT_List,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
NT_Expr, // WRITEME: add symbolic names for you non-terminals here NT_ListP, NT_ExprP, NT_Term, NT_TermP, NT_Factor } nonterm_type; // This function returns a string for the non-terminals. It is used in the // parsetree_t class to actually dump the parsetree to a dot file (which can // then be turned into a picture). Note that the return char* is a reference // to a local copy and it needs to be duplicated if you are a going require // multiple instances simultaniously. // // Add entries for all the non-terminals of your grammar following the same // convention. char* nonterm_to_string(nonterm_type nt) { static char buffer[MAX_SYMBOL_NAME_SIZE]; switch( nt ) { case epsilon: strncpy(buffer,"e",MAX_SYMBOL_NAME_SIZE); break; case NT_List: strncpy(buffer,"List",MAX_SYMBOL_NAME_SIZE); break; // WRITEME: add the other nonterminals you need here case NT_Expr: strncpy(buffer,"Expr",MAX_SYMBOL_NAME_SIZE); break; case NT_ListP: strncpy(buffer,"ListP",MAX_SYMBOL_NAME_SIZE);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 12

calc -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online