-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatito.g4
134 lines (124 loc) · 4.14 KB
/
patito.g4
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
grammar patito;
@parser::header {
from varfunctable import *
global funclist, quads
funclist = FuncTable()
quads = Cuadruplos()
global current_content, current_func, current_scope, current_type, current_var, various_vars, temporal_operator
current_var = ''
current_func = ''
current_type = ''
current_scope = 0
current_content = ''
various_vars = []
temporal_operator = ''
}
startRule : programa EOF;
programa : 'program'{quads.quad_main_init()} Id {global current_func, current_scope; current_func = $Id.text; funclist; funclist.add_function($Id.text)}
';' (vars? funcs*
'main'{quads.quad_main_set()} {current_scope+=1;
current_func = "main";
funclist.add_function(current_func)}
body {current_scope-=1}
'end' {funclist.print_all_functions(); quads.printQuads()});
vars : {global current_content, current_func, current_scope, current_type, current_var, various_vars}
'var' (Id {various_vars.append($Id.text)} (',' Id {various_vars.append($Id.text)})* ':' type {current_type = $type.text} ';'{
for myvar in various_vars: funclist.add_variable_to_function(current_func, myvar, current_type, current_scope, current_content);
various_vars = [];
})+;
type : 'int' | 'float';
funcs : {global current_content, current_func, current_scope, current_type, current_var}
'void' {current_scope+=1} Id {current_func = $Id.text; funclist.add_function(current_func)}
'(' (Id ':' type
{current_var = $Id.text;
current_type = $type.text;
funclist.add_variable_to_function(current_func, current_var, current_type, current_scope, current_content)}
(',' Id ':' type
{current_var = $Id.text; current_type = $type.text;
funclist.add_variable_to_function(current_func, current_var, current_type, current_scope, current_content)}
)*)* ')' '[' vars? body ']' ';'
{current_scope-=1;}
{funclist.delete_all_variables(current_func)};
body : '{' (statement)* '}';
statement : assign | condition | cycle | f_call | print;
assign : Id {
quads.push_operate(funclist.get_variable_memory($Id.text, current_scope), funclist.get_variable_type($Id.text, current_scope), False, False, 0, '')
} '=' {
quads.push_operator('=')
}expression {
quads.quads_Assign()
}';';
expression : exp {
quads.quads_Compare()
}(('>' {
quads.push_operator('>')
}| '<' {
quads.push_operator('<')
}| '!='{
quads.push_operator('!=')
}) exp{
quads.quads_Compare()
})*;
exp : termino {
quads.quads_PlusMin()
}(('+' {
quads.push_operator('+')
}| '-'{
quads.push_operator('-')
}) termino{
quads.quads_PlusMin()
})*;
termino : factor {
quads.quads_MultDiv()
}(('*' {
quads.push_operator('*')
}| '/'{
quads.push_operator('/')
}) factor{
quads.quads_MultDiv()
})*;
factor : ('(' {
quads.push_FF()
}expression ')'{
quads.pop_FF()
}) | ({global temporal_operator}('+' {temporal_operator = '+'}| '-' {temporal_operator = '-'}) (Id {
quads.push_operate(funclist.get_variable_memory($Id.text, current_scope), funclist.get_variable_type($Id.text, current_scope), False, False, $Id.text, temporal_operator); temporal_operator = ''
} | cte)) | (Id{
quads.push_operate(funclist.get_variable_memory($Id.text, current_scope), funclist.get_variable_type($Id.text, current_scope), False, False, $Id.text, '')
} | cte);
Id : [a-zA-Z][a-zA-Z0-9]*;
cte : Cte_int{global temporal_operator}{
quads.push_operate($Cte_int.text, "int", True, False, 0, temporal_operator); temporal_operator = ''
}| Cte_float {
quads.push_operate($Cte_float.text, "float", True, False, 0, temporal_operator); temporal_operator = ''
}| Cte_string {
quads.push_operate($Cte_string.text, "string", True, False, 0, temporal_operator); temporal_operator = ''
};
Cte_int : [0-9]+;
Cte_float : [0-9]+ '.' [0-9]+;
Cte_string : '"' .*? '"';
condition : 'if' '(' expression')'{
quads.quad_if_1()
}
body ('else' {
quads.quad_if_3()
} body)? ';'{
quads.quad_if_2()
};
cycle : 'while' {
quads.quad_while_1()
} body 'do' '(' expression ')'{
quads.quad_while_return()
} ';'{
};
f_call : Id '(' (expression (',' expression)*)? ')' ';';
print : 'print' {
quads.push_operator('print')
}'(' (Cte_string {
quads.push_operate($Cte_string.text, "string", True, False, 0, '')
}| expression) (',' (Cte_string {
quads.push_operate($Cte_string.text, "string", True, False, 0, '')
}| expression))* ')'{
quads.quads_Print()
} ';';
WS : [ \t\r\n]+ -> skip;