-
Notifications
You must be signed in to change notification settings - Fork 15
/
Parser.pro
142 lines (115 loc) · 2.74 KB
/
Parser.pro
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
%% Parser for While language
% Program is a list of statements
program(S) --> statements(S).
% Non-empty list
statements(slist(H,T)) -->
statement(H),
keyword(";"),
statements(T).
% Empty list
statements(S) --> statement(S).
%% Statements
% Skip statement
statement(skip) -->
keyword("skip").
% Assign statement
statement(assign(identifier(V),E)) -->
identifier(V),
keyword(":="),
aexpression(E).
% Conditional statement
statement(ifthenelse(E,S1,S2)) -->
keyword("if"),
bexpression(E),
keyword("then"),
statement(S1),
keyword("else"),
statement(S2).
% Loop statement
statement(while(E,S)) -->
keyword("while"),
bexpression(E),
keyword("do"),
statement(S).
%% Arithmetic expressions
% Number is a an arithmetic expression
aexpression(number(N)) --> number(N).
% Variable reference is a an arithmetic expression
aexpression(identifier(V)) --> identifier(V).
% A sum of arithmetic expressions is also an arithmetic expression
aexpression(add(E1,E2)) -->
keyword("("),
aexpression(E1),
keyword("+"),
aexpression(E2),
keyword(")").
% Same with subtraction
aexpression(sub(E1,E2)) -->
keyword("("),
aexpression(E1),
keyword("-"),
aexpression(E2),
keyword(")").
% ...and multiplication
aexpression(mul(E1,E2)) -->
keyword("("),
aexpression(E1),
keyword("*"),
aexpression(E2),
keyword(")").
%% Boolean expressions
% Boolean primitives are boolean expressions
bexpression(true) --> keyword("true").
bexpression(false) --> keyword("false").
% Comparison is a boolean expression: equality
bexpression(equals(E1,E2)) -->
aexpression(E1),
keyword("="),
aexpression(E2).
% Comparison is a boolean expression: less than or equal to
bexpression(lte(E1,E2)) -->
aexpression(E1),
keyword("≤"),
aexpression(E2).
% Negation is a boolean expression
bexpression(not(E)) -->
keyword("¬"),
bexpression(E).
% Disjunction of boolean expressions is also a boolean expression
bexpression(and(E1,E2)) -->
keyword("("),
bexpression(E1),
keyword("^"),
bexpression(E2),
keyword(")").
%% Low-level details
% Dealing with layout
layout --> [0' ], layout. %' space
layout --> [0' ], layout. %' tab
layout --> [10], layout. % newline
layout --> [].
% Keywords are space-consuming strings
keyword(X) -->
layout,
string(X).
% Bare strings
string([],X,X).
string([H|T1],[H|T2],X) :- string(T1,T2,X).
% Numbers are space-consuming digits
number(N) -->
layout,
digit(H), digits(T),
{ number_chars(N,[H|T]) }.
% Bare digits
digits([H|T]) --> digit(H), digits(T).
digits([]) --> [].
digit(H,[H|T],T) :- H >= 0'0, H =< 0'9.
% Identifiers are space-consuming letters
identifier(V) -->
layout,
letter(H), letters(T),
{ atom_chars(V,[H|T]) }.
% Bare letters
letters([H|T]) --> letter(H), letters(T).
letters([]) --> [].
letter(H,[H|T],T) :- H >= 0'a, H =< 0'z.