-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
83 lines (65 loc) · 1.66 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%skeleton "lalr1.cc" /* -*- C++ -*- */
%require "3.0"
%defines
%define parser_class_name { Parser }
%define api.token.constructor
%define api.value.type variant
%define parse.assert
%define api.namespace { al }
%code requires
{
#include <iostream>
#include <string>
#include <vector>
#include <stdint.h>
#include "ast.h"
#include "compile_time.h"
using namespace std;
namespace al {
class Lexer;
class Token;
}
}
%code top
{
#include <iostream>
#include "lex.h"
#include "parser.tab.hpp"
#include "location.hh"
static al::Parser::symbol_type yylex(al::Lexer &lexer) {
return lexer.lex();
}
using namespace al;
}
%code {
}
%lex-param { al::Lexer &lexer }
%parse-param { al::Lexer &lexer } { al::CompileTime &rt }
%locations
%define parse.trace
%define parse.error verbose
%token <std::shared_ptr<al::ast::StringLiteral>> STRING
%token <std::shared_ptr<al::ast::Symbol>> SYMBOL
%token <std::string> INT
%token QUOTE "'";
%token LEFTPAR "(";
%token RIGHTPAR ")";
%token SEMICOLON ";";
%token END 0 "end of file"
%type< std::shared_ptr<al::ast::Exp> > exp;
%type< std::shared_ptr<al::ast::ExpList> > exp_list;
%type< std::shared_ptr<al::ast::List> > list;
%start program
%%
program : exp { rt.setASTRoot($1); }
exp: list { $$ = $1; }
| SYMBOL { $$ = $1; }
| STRING { $$ = $1; }
;
list: LEFTPAR exp_list RIGHTPAR { $$ = std::make_shared<al::ast::List>($2); }
exp_list: { $$ = std::make_shared<al::ast::ExpList>(); }
| exp exp_list { $$ = $2->prepend($1); }
%%
void al::Parser::error(const location &loc , const std::string &message) {
cout << "Error: " << message << endl;
}