driver.hh 1.75 KB
Newer Older
1
2
3
4
5
6
7
// See https://www.gnu.org/software/bison/manual/html_node/Calc_002b_002b-Parsing-Driver.html
#ifndef DRIVER_HH
# define DRIVER_HH
# include <string>
# include <map>
# include "parser.hh"

8
9
10
// Give Flex the prototype of yylex we want:
// because of %define api.value.type variant and %define api.token.constructor in parser.yy
// the parser defines the type symbol_type, and expects yylex to have the following prototype.
11
12
13
# define YY_DECL \
  yy::parser::symbol_type yylex (driver& drv)
// ... and declare it for the parser's sake.
14
15
16
// yylex now returns a complete symbol, aggregating its type (i.e., the traditional value returned by yylex), 
// its semantic value, and possibly its location.
// see https://www.gnu.org/software/bison/manual/html_node/Complete-Symbols.html
17
18
19
YY_DECL;

// Conducting the whole scanning and parsing of Calc++.
20
21
// This is the parsing context, an interface to the parser and scanner
// Contains all the data to exchange -> opens file to scan, instantiates the parser etc.
22
23
24
25
26
class driver
{
public:
  driver ();

27
  std::map<std::string, int> variables; // this holds the variables and their values we find on the way
28

29
  int result; // the evaluated expession result
30

31
  // Run the parser on file F.  Return 0 on success and 1 on failure.
32
33
34
35
36
37
38
  int parse (const std::string& f);
  // The name of the file being parsed.
  std::string file;
  // Whether to generate parser debug traces.
  bool trace_parsing;

  // Handling the scanner.
39
  // These functions are declared at the end of scanner.ll
40
41
  void scan_begin ();
  void scan_end ();
42
  
43
44
  // Whether to generate scanner debug traces.
  bool trace_scanning;
45
46
  // The token's location used by the scanner. 
  // made available by 'yy::location& loc = drv.location;' in scanner.ll
47
48
49
  yy::location location;
};
#endif // ! DRIVER_HH