Created
January 24, 2017 05:38
-
-
Save Lifelovinglight/fa4f05f748f5179a00441b1f22878329 to your computer and use it in GitHub Desktop.
bit ugly but simplest possible example of a meaningful pushdown automata parser
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
(defpackage :hippie-prompt | |
(:use :common-lisp) | |
(:export :prompt | |
:set-prompt)) | |
(in-package :hippie-prompt) | |
(defun symbolize (ln) | |
(intern (string-upcase (coerce ln 'string)))) | |
(defun symbolize-stack (stack &optional (stack2 (list))) | |
(if (symbolp (car stack)) | |
(cons (symbolize stack2) stack) | |
(symbolize-stack (cdr stack) (cons (car stack) stack2)))) | |
(defun symbolize-prompt-input (input &optional (stack (list 'empty-stack))) | |
(cond | |
((null input) | |
(symbolize-stack stack)) | |
((whitespacep (car input)) | |
(if (letterp (car stack)) | |
(symbolize-prompt-input (cdr input) (symbolize-stack stack)) | |
(symbolize-prompt-input (cdr input) stack))) | |
((letterp (car input)) (symbolize-prompt-input (cdr input) (cons (car input) stack))) | |
(t (symbolize-prompt-input (cdr input) stack)))) | |
(defun letterp (char) | |
(member char (coerce "abcdefghijklmnopqrstuvwxyz" 'list))) | |
(defun whitespacep (char) | |
(or (eq char #\Space) | |
(eq char #\Tab))) | |
(defun empty-symbolp (symbol) | |
(equal "" (symbol-name symbol))) | |
(defun remove-empty-symbols (ln) | |
(remove-if #'empty-symbolp ln)) | |
(defvar *prompt* "> ") | |
(defun prompt () | |
(princ *prompt*) | |
(cdr (reverse (remove-empty-symbols (symbolize-prompt-input (coerce (read-line) 'list)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment