parser.yy 3.11 KB
Newer Older
1
// See https://www.gnu.org/software/bison/manual/html_node/Calc_002b_002b-Parser.html
2
// needed for creation of parser.hh (C++ style)
3
4
5
6
%skeleton "lalr1.cc" /* -*- C++ -*- */
%require "3.5"
%defines

7
8
9
// In C we would define a %union to use as our symbol type
// In C++ and newer bison we can tell it to use a std::variant for a symbol type
// We declare our intention to use is with 
10
11
12
%define api.token.constructor
%define api.value.type variant
%define parse.assert
13
// and then just use whatever types we want. 'parse.assert' makes sure we use the right types in symbols
14
15

%code requires {
16
  // place your includes here
17
18
19
20
  # include <string>
  class driver;
}

21
22
23
// The parsing context is passed to flex and bison.
// Equivalent to both %lex-param and %parse-param
// You could also define 'int yylex (driver& drv)' and 'int yyparse (driver& drv);'
24
25
%param { driver& drv }

26
27
//request location tracking
// made available by 'yy::location& loc = drv.location;' in scanner.ll
28
29
30
31
32
33
34
35
36
%locations

%define parse.trace
%define parse.error verbose

%code {
# include "driver.hh"
}

37
// DRY - tokens shall begin with "TOK_"
38
39
40
41
42
43
44
45
46
47
48
49
%define api.token.prefix {TOK_}
%token
  END  0  "end of file"
  ASSIGN  ":="
  MINUS   "-"
  PLUS    "+"
  STAR    "*"
  SLASH   "/"
  LPAREN  "("
  RPAREN  ")"
;

50
51
52
// these are our variant types
// bison generates functions like 
// 'symbol_type make_NUMBER (const std::string&, const location_type&);' for us, which can be overwritten
53
54
%token <std::string> IDENTIFIER "identifier"
%token <int> NUMBER "number"
55
56
57
58
59
// declare nonterminal expression
// compare to %type: https://www.gnu.org/software/bison/manual/html_node/Type-Decl.html
// tl;dr: use nterm explicitly for nonterminals, type can also be a terminal
// int: an exp resolves/ is evaluated to an int
// replace e.g. with AST node types
60
%nterm <int> exp
61
62
// unit etc. do not need to be declared here, because they have no type to be evaluated to,
// as they are evaluated to other nterms
63

64
%printer { yyo << $$; } <*>;  //print values using their operator<<
65
66

%%
67
68
69
70
71
%start unit;  // this is our root node, so to speak
unit: assignments exp  { drv.result = $2; };    
// here we set the final outcome of exp to the result of our driver
// sort of to keep it when the compiling is done
// imagine you could also put a pointer to the root node of you ast here :)
72
73
74
75
76
77

assignments:
  %empty                 {}
| assignments assignment {};

assignment:
78
79
  "identifier" ":=" exp { drv.variables[$1] = $3; };  
  // we save our variable inside the driver because we want to store it for later use
80

81
82
// you will find this everytime mathematical expressions are used
// it declares operator precedence; see here https://www.gnu.org/software/bison/manual/html_node/Precedence-Decl.html
83
84
85
86
87
%left "+" "-";
%left "*" "/";
exp:
  "number"
| "identifier"  { $$ = drv.variables[$1]; }
88
| exp "+" exp   { $$ = $1 + $3; }   // these are real mathematical expressions being evaluated and stored back in exp ($$)
89
90
91
92
93
94
95
96
97
98
99
| exp "-" exp   { $$ = $1 - $3; }
| exp "*" exp   { $$ = $1 * $3; }
| exp "/" exp   { $$ = $1 / $3; }
| "(" exp ")"   { $$ = $2; }
%%

void
yy::parser::error (const location_type& l, const std::string& m)
{
  std::cerr << l << ": " << m << '\n';
}