# HG changeset patch # User John W. Eaton # Date 1558650395 14400 # Node ID d179b0bb85e47311b9df3ddfdfc7b575db2317aa # Parent 894be158b32d4ba5669d11a0052b7b3fdef2221e make lexer a member function in parser class and eliminate some more global variables diff -r 894be158b32d -r d179b0bb85e4 parser.h --- a/parser.h Thu May 23 17:57:20 2019 -0400 +++ b/parser.h Thu May 23 18:26:35 2019 -0400 @@ -29,11 +29,18 @@ private: + int lexer (double& token_value); + interpreter& m_interpreter; void *m_parser_state; bool m_beg_of_stmt; + + // Auxiliary variables for lexing. + size_t m_bufptr = 0; + size_t m_chunk_size = 0; + const char *m_buf; }; } diff -r 894be158b32d -r d179b0bb85e4 parser.yy --- a/parser.yy Thu May 23 17:57:20 2019 -0400 +++ b/parser.yy Thu May 23 18:26:35 2019 -0400 @@ -39,13 +39,6 @@ { m_interpreter.emit_result (value); } - - // For communication between the lexer and parser. - size_t bufptr = 0; - size_t chunk_size = 0; - const char *buf; - - static int yylex (YYSTYPE& token_value); } static void yyerror (calc::parser&, char const *); @@ -135,24 +128,24 @@ // if not a number. It skips all blanks and tabs, and returns -1 for // end-of-input. - static int yylex (YYSTYPE& token_value) + int parser::lexer (double& token_value) { int c; - if (bufptr >= chunk_size) + if (m_bufptr >= m_chunk_size) return -1; // Skip white space. - while ((c = buf[bufptr++]) == ' ' || c == '\t' || c == '\n') + while ((c = m_buf[m_bufptr++]) == ' ' || c == '\t' || c == '\n') ; // Process numbers. if (c == '.' || isdigit (c)) { int chars_read = 0; - bufptr--; - sscanf (&buf[bufptr], "%lf%n", &token_value, &chars_read); - bufptr += chars_read; + m_bufptr--; + sscanf (&m_buf[m_bufptr], "%lf%n", &token_value, &chars_read); + m_bufptr += chars_read; return NUM; } @@ -162,16 +155,16 @@ int parser::parse_and_execute (const std::string& line) { - bufptr = 0; - chunk_size = line.length (); - buf = line.c_str (); + m_bufptr = 0; + m_chunk_size = line.length (); + m_buf = line.c_str (); int status; do { - YYSTYPE token_value; - int input_char = yylex (token_value); + double token_value; + int input_char = lexer (token_value); if (input_char < 0) return -1;