// This code made available to his students by // Prof. Ronald Moore // https://fbi.h-da.de/personen/ronald-moore/ // mailto:ronald.moore@h-da.de // with no warranties whatsoever! #pragma once #include // for isspace #include // for strtod #include #include // for std::pair #include // new C++17 feature! Like unions, only better! // =================== // LEXICAL ANALYSIS // The following are taken to be tokens: // left and right parenthesis, the plus and minus characters, // as well as asterisk and forward slash -- and numbers. // In the script, substraction and division are not supported, // but it seems like time to add them. // Preliminaries and Utilities // ============================ namespace lex { // Utility Types typedef double numberType; // feel fee to change this to something else like int or float or bigint.... // Tokens -- are a pair of a tag and a value, where the value can be // various things - a char or a numberType at present, but names and // multicharacter operators could be added later typedef enum { tok_number = 'n', tok_lparen = '(', tok_rparen = ')', tok_plus = '+', tok_minus = '-', tok_times = '*', tok_div = '/', tok_eof = 'E', bad_tok = 'X' } TokenTag; typedef std::variant< char, numberType > TokenValue; typedef std::pair< TokenTag, TokenValue > Token; extern Token next_token; // again with the global variables... { // Functons (or methods, if you prefer) void printInputLocation( ); void printErrorMsg( const std::string Error ); Token gettok( ); // // DRY -- this line is repeated so often, it deserves its own function inline void advance_token( ) { next_token = gettok(); } // eats current token! void openInputSource( std::string filename ); } // end namespace lex