1 /* 2 This is a dirty little parser for LISP-like expressions. It is meant 3 to to parse expressions written by machine. 4 5 * Comments are not recognized. 6 * Almost any token is accepted as an atom. 7 8 * Dot expressions are ok. 9 * Extra whitespace is ok. 10 11 * If anything goes wrong, exit with a message goes to stderr. 12 13 */ 14 15 #include <stdio.h> 16 #include <string.h> 17 #include <stdlib.h> /* for malloc, free */ 18 19 #if 1 20 #define BOOLEAN char 21 #define FALSE 0 22 #define TRUE 1 23 #else 24 typedef enum { FALSE=0, TRUE=1 } BOOL; 25 #endif 26 27 typedef struct bnode * Bnode; 28 29 struct bnode { 30 Bnode car; 31 Bnode cdr; 32 BOOLEAN atom; 33 char *label; 34 }; 35 36 /* Prototypes from lisp.c */ 37 38 void zap_btree(Bnode p); 39 BOOLEAN true_listp(Bnode p); 40 void fprint_btree(FILE *fp, Bnode p); 41 void p_btree(Bnode p); 42 43 BOOLEAN nullp(Bnode p); 44 Bnode parse_lisp(FILE *fp); 45 int atom(Bnode p); 46 Bnode car(Bnode p); 47 Bnode cdr(Bnode p); 48 Bnode cadr(Bnode p); 49 Bnode caddr(Bnode p); 50 int length(Bnode p); 51 52