blob: 0fb4ee056edd397f4b073cfa371575aa74fbe66a (
plain) (
blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
(*
* Menhir wygeneruje funkcję o nazwie file
*)
%start <Xi_lib.Ast.module_definition> file
%{
open Xi_lib
open Ast
open Parser_utils
(* Generator znaczników *)
let mkTag =
let i = ref 0 in
fun () ->
let tag = !i in
incr i;
NodeTag tag
(* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
* Miejsce na twój kod w Ocamlu
*)
(* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------------------------------------------------- *)
%}
(* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
* Miejsce na dyrektywy
*)
%token EOF
%token LPAREN
%token RPAREN
%token LBRACKET
%token RBRACKET
%token LSBRACKET
%token RSBRACKET
%token COLON
%token OP_PLUS
%token OP_MINUS
%token OP_MULT
%token OP_DIV
%token OP_REM
%token OP_AND
%token OP_OR
%token OP_EQ
%token OP_NEQ
%token OP_LE
%token OP_GE
%token OP_LT
%token OP_GT
%token ASSIGN
%token IF
%token ELSE
%token WHILE
%token RETURN
%token LENGTH
%token <int>INT
%token <string>IDENTIFIER
%token <char>CHAR
%token <bool>BOOL
%token T_BOOL
%token T_INT
(* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------------------------------------------------- *)
%%
(* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
* Miejsce na gramatykę
*)
(* Obecnie potrafimy sparsować tylko pusty plik (wymagamy od razu tokena EOF) *)
file:
| declarations= list(func) EOF
{ ModuleDefinition {global_declarations=declarations } }
| EOF
{ ModuleDefinition {global_declarations=[] } }
func:
| id=identifier LPAREN parameters=list(argument) RPAREN
{ GDECL_Function { loc=Location( {line=0;column=0;file=""});
id=id;formal_parameters=parameters; return_types=[];body=None} }
argument:
| id = identifier COLON t=typ
{ VarDecl {loc=mkLocation $startpos;id=id;tp=t} }
typ:
| t=base_type { t }
| t=typ LSBRACKET option(INT) RSBRACKET
{ TEXPR_Array { loc=mkLocation $startpos;
sub=t;dim=None } }
base_type:
| T_INT {TEXPR_Int {loc=mkLocation $startpos}}
| T_BOOL {TEXPR_Bool {loc=mkLocation $startpos}}
identifier:
| IDENTIFIER
{ Identifier $1 }
(*
** przykład użycia mkLocation
use_declaration:
| USE suffix(identifier, opt(SEMICOLON))
{ GDECL_Use {loc=mkLocation $startpos; id=$2} }
** przykład użycia mkTag
atomic_expression:
| identifier
{ EXPR_Id {loc=mkLocation $startpos; id=$1; tag=mkTag ()} }
*)
(* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------------------------------------------------- *)
|