Skip to content

Instantly share code, notes, and snippets.

@mondalaci
Last active December 16, 2015 02:39
Show Gist options
  • Save mondalaci/5364380 to your computer and use it in GitHub Desktop.
Save mondalaci/5364380 to your computer and use it in GitHub Desktop.
Parse and print procedure and function names of the supplied Pascal program.
% Simple Pascal parser written in Prolog that prints the names and
% line numbers of the procedures and functions found in input.pas
% I put together this mess for the programming languages course of the university in 2005.
printIden(L) :-
get_char(C),
print(C),
C == ';' ->
print(' '),
print(L),
nl,
parse(C, L, 0)
;
printIden(L).
parse(C, L, P) :-
(C == end_of_file ->
!
;
(C == 'n' ->
L2 is L + 1,
get_char(C2),
parse(C2, L2, 0)
;
(C == 'p' ->
get_char(C3),
(C3 == 'r' ->
get_char(C4),
(C4 == 'o' ->
get_char(C5),
(C5 == 'c' ->
get_char(C6),
(C6 == 'e' ->
get_char(C7),
(C7 == 'd' ->
get_char(C8),
(C8 == 'u' ->
get_char(C9),
(C9 == 'r' ->
get_char(C10),
(C10 == 'e' ->
get_char(_),
printIden(L)
;
parse(C10, L, 1))
;
parse(C9, L, 1))
;
parse(C8, L, 1))
;
parse(C7, L, 1))
;
parse(C6, L, 1))
;
parse(C5, L, 1))
;
parse(C4, L, 1))
;
parse(C3, L, 1))
;
(C == 'f' ->
get_char(C3),
(C3 == 'u' ->
get_char(C4),
(C4 == 'n' ->
get_char(C5),
(C5 == 'c' ->
get_char(C6),
(C6 == 't' ->
get_char(C7),
(C7 == 'i' ->
get_char(C8),
(C8 == 'o' ->
get_char(C9),
(C9 == 'n' ->
get_char(_),
printIden(L)
;
parse(C9, L, 1))
;
parse(C8, L, 1))
;
parse(C7, L, 1))
;
parse(C6, L, 1))
;
parse(C5, L, 1))
;
parse(C4, L, 1))
;
parse(C3, L, 1))
;
(P == 1 ->
get_char(C0),
parse(C0, L, 0)
;
parse(C, L, 1)
))))).
:-
see('input.pas'),
get_char( C ),
parse(C, 1, 0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment