forked from PhamAlexT/Compilateur-MiniC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyseurSyntaxique.mly
More file actions
151 lines (137 loc) · 2.84 KB
/
AnalyseurSyntaxique.mly
File metadata and controls
151 lines (137 loc) · 2.84 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
%{
open SyntaxeAbstr
%}
%token <int> CONST
%token <string> IDENT
%token SEMICOLON ";"
%token COMMA ","
%token AFF "="
%token PLUS "+" MINUS "-"
%token TIMES
%token L_PAR "(" R_PAR ")"
%token L_ACC "{" R_ACC "}"
%token LT GT LEQ GEQ EQ NEQ
%token INT BOOL VOID
%token TRUE FALSE
%token NOT "!"
%token IF ELSE WHILE
%token PUTCHAR
%token RETURN
%token EOF
%token MINUS_U
%left LT LEQ GT GEQ EQ NEQ
%left PLUS MINUS
%left TIMES IDENT
%nonassoc MINUS_U
%nonassoc NOT
%left L_PAR
%start prog
%type <SyntaxeAbstr.prog> prog
%%
(* programme est compose d'une premiere partie de var globales et suivi de declarations de fonctions puis fin fichier*)
(*sinon error*)
prog:
| gl=globals fs=list(fun_def) EOF
{
{globals=List.rev gl;functions=fs}
}
| error
{ let pos = $startpos in
let message = Printf.sprintf
"ERREUR: Grammaire - Echec à la position: %d, %d"
pos.pos_lnum
(pos.pos_cnum - pos.pos_bol)
in
failwith message }
;
(*types variables ds () d'une fonction: type + identifiant*)
param:
| t = typesVar i=IDENT {i,t}
;
;
(*regle pour les fonctions : types + identifiant + ( + parametre + ) + { + sequence + } *)
fun_def:
| t = typesFonctions i = IDENT L_PAR ps = separated_list(COMMA,param) R_PAR
L_ACC l =globals s = list(instr) R_ACC
{
{name=i;params=ps;return=t;locals=l; code = s}
}
;
globals:
| g1 = globals g2 = global {g2::g1}
| {[]}
;
(*valeur globales d'un programme *)
global:
(*cas sans value : initialise un int a 0 et un bool a false*)
| t = typesVar i = IDENT "=" e = expr ";" {(i,t,e)}
| INT i = IDENT ";" {(i,Int, Cst 0)}
| BOOL i = IDENT ";" {(i,Bool,CreaBool false)}
(*
| INT i=IDENT SEMICOLON {Printf.printf "global int sans valeur\n" ;(i,Int)}
| BOOL i=IDENT SEMICOLON {Printf.printf "global false\n"; (i,Bool)}
(*cas avec valeur*)
| INT i=IDENT "=" expr SEMICOLON {(i,Int)}
| BOOL i=IDENT "=" expr SEMICOLON {(i,Bool)} *)
;
expr:
(*constante*)
| n = CONST
{Cst n}
| TRUE {CreaBool true}
| FALSE {CreaBool false}
(*Opération binaire*)
| e1=expr op=binop e2=expr
{Binop(op,e1,e2)}
| "(" e = expr ")"
{e}
(* acces variable *)
| id=IDENT
{Get(id)}
(*appel fonction *)
| id=IDENT "(" e = separated_list(COMMA,expr) ")"
{Call(id,e)}
(*Negation*)
| "!" e = expr
{Not(e)}
| "-" e = expr {Binop(Mul,e,Cst (-1))} %prec MINUS_U
;
instr:
(*affichage*)
| PUTCHAR L_PAR e=expr R_PAR ";"
{Putchar(e)}
(*if*)
| id=IDENT "=" e=expr ";"
{Set(id,e)}
| IF "(" e1=expr ")" "{" seq1=list(instr) "}" ELSE "{" seq2=list(instr) "}"
{If(e1,seq1,seq2)}
(*while*)
| WHILE "(" e=expr ")" "{" s = list(instr) "}"
{While(e,s)}
(*return*)
| RETURN e=expr ";"
{Return e}
(*expression simple*)
| e=expr
{Expr e}
;
%inline binop:
| PLUS {Add}
| MINUS {Sub}
| TIMES {Mul}
| LT {Lt}
| GT {Lt}
| LEQ {Lt}
| GEQ {Lt}
| EQ {Eq}
| NEQ {Neq}
;
%inline typesVar:
| INT {Int}
| BOOL {Bool}
;
%inline typesFonctions:
| INT {Int}
| BOOL {Bool}
| VOID {Void}
;