Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@703 ab42f6e0-554d-0410-b580-99e487e6eeb2
- Loading branch information
1 parent
8b8d753
commit 46b21dc
Showing
10 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
test: | ||
swipl -f RunParser.pro -t "main('input.txt')" | ||
|
||
debug: | ||
cat input.txt | ||
swipl -s Parser.pro | ||
|
||
clean: | ||
rm -rf *~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
%% 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
:- ['Parser.pro']. | ||
|
||
main(File) :- | ||
parseFile(File,program,S), | ||
write(S),nl. | ||
|
||
parseFile(File,P,R) :- | ||
open(File,read,Stream,[]), | ||
read_stream_to_codes(Stream, Contents), | ||
close(Stream), | ||
apply(P,[R,Contents,Rest]), | ||
eof(Rest,_). | ||
|
||
eof([],[]). | ||
eof([0' |T],R) :- eof(T,R). %' | ||
eof([10|T],R) :- eof(T,R). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
if ¬x ≤ 0 | ||
then | ||
y:=((2+x)-y) | ||
else | ||
while z=0 do | ||
z:=2; | ||
skip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
test: | ||
swipl -f RunParser.pro -t "main('input.txt')" | ||
|
||
fail: | ||
swipl -f RunParser.pro -t "main('bad.txt')" | ||
|
||
debug: | ||
cat input.txt | ||
swipl -s Parser.pro | ||
|
||
clean: | ||
rm -rf *~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
% A root tree is a regular tree with an opening and closing tags and a forest in between | ||
tree(tree(N,AL,TL)) --> | ||
layout, | ||
string("<"), | ||
name(N), | ||
attrs(AL,_), | ||
string(">"), | ||
trees(TL), | ||
string("</"), | ||
name(N), | ||
string(">"), | ||
layout. | ||
|
||
% A non-empty forest | ||
trees([H|T]) --> | ||
tree(H), | ||
trees(T). | ||
|
||
% An empty forest | ||
trees([]) --> []. | ||
|
||
% Attribute list | ||
attrs([H|T],[N|U]) --> | ||
layout, | ||
attr(H), | ||
attrs(T,U), | ||
{ | ||
H = [N,_], | ||
not(member(N,U)) | ||
}. | ||
attrs([],[]) --> []. | ||
|
||
% Attribute | ||
attr([N,V]) --> | ||
name(N), | ||
string("="), | ||
value(V). | ||
|
||
% Bare strings | ||
string([],X,X). | ||
string([H|T1],[H|T2],X) :- string(T1,T2,X). | ||
|
||
% Consuming spaces | ||
layout --> [0' ], layout. % space ' | ||
layout --> [0' ], layout. % tab ' | ||
layout --> [10], layout. % newline | ||
layout --> []. | ||
|
||
% Only lowcased tag names are allowed | ||
name(N) --> | ||
letter(H), letters(T), | ||
{ atom_codes(N,[H|T]) }. | ||
|
||
% Bare letters | ||
letters([H|T]) --> letter(H), letters(T). | ||
letters([]) --> []. | ||
letter(H,[H|T],T) :- H >= 0'a, H =< 0'z. | ||
|
||
value(V) --> | ||
char(H), chars(T), | ||
{ atom_codes(V,[H|T]) }. | ||
|
||
chars([H|T]) --> char(H), chars(T). | ||
chars([]) --> []. | ||
char(H,[H|T],T) :- H >= 0'a, H =< 0'z. | ||
char(H,[H|T],T) :- H >= 0'0, H =< 0'9. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
:- ['Parser.pro']. | ||
|
||
main(File) | ||
:- | ||
parseFile(File,tree,S), | ||
write(S), nl. | ||
|
||
parseFile(File,P,R) | ||
:- | ||
open(File,read,Stream,[]), | ||
read_stream_to_codes(Stream, Contents), | ||
close(Stream), | ||
apply(P,[R,Contents,Rest]), | ||
eof(Rest,_). | ||
|
||
eof([],[]). | ||
eof([0' |T],R) :- eof(T,R). %' | ||
eof([10|T],R) :- eof(T,R). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<a x=4 x=5> | ||
<b></b> | ||
<c y=true></c> | ||
</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<a x=4> | ||
<b></b> | ||
<c x=2 y=true></c> | ||
</a> |