Skip to content

Instantly share code, notes, and snippets.

@TuxCoding
Created January 29, 2017 10:25
Show Gist options
  • Save TuxCoding/a1973df5c49bf7965f5976624ad73485 to your computer and use it in GitHub Desktop.
Save TuxCoding/a1973df5c49bf7965f5976624ad73485 to your computer and use it in GitHub Desktop.
A push-down automaton written in Prolog
uprg(P):- uprg(P, [#]). % Creates a new push-down automaton
%Input | head of the stack :- Stackoutput
uprg([p|Tail], Stack):- uprg(Tail, [p|Stack]).
uprg([p|Tail], [#|Stack]):- uprg(Tail, [p, #|Stack]).
uprg([p|Tail], [a|Stack]):- uprg(Tail, [p, a|Stack]).
uprg([a|Tail], [p|Stack]):- uprg(Tail, [a|Stack]).
uprg([a|Tail], [a|Stack]):- uprg(Tail, [a|Stack]).
uprg([b|Tail], [a|Stack]):- uprg(Tail, [b|Stack]).
uprg([e|Tail], [b|Stack]):- uprg(Tail, Stack).
uprg([], [#]). %No more input and our last stack element is the default stack character -> valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment