A custom Lisp interpreter built in C using Flex and Bison.
CI-LISP is a minimalist Lisp interpreter developed in C that utilizes Flex for lexical analysis and Bison for parsing. This project demonstrates how to build an interpreter that can evaluate arithmetic expressions and handle function calls using an Abstract Syntax Tree (AST).
This was a school solo project, a shell of the .c,.y,.l, and .h files was provided and I completed the rest, it was not written from scratch.
The project features a modular design where:
createNumberNode
and createFunctionNode
build nodes for numbers and function expressions respectively.eval
and its helper functions traverse the AST to compute results, handling arithmetic operations like addition, subtraction, multiplication, and division.yyerror
and warning
, which output descriptive messages for issues like division by zero or memory allocation failures.
// Evaluates an AST node and returns its computed value
RET_VAL eval(AST_NODE *node) {
if (!node) {
yyerror("NULL ast node passed into eval!");
return NAN_RET_VAL;
}
switch (node->type) {
case NUM_NODE_TYPE:
return evalNumNode(node);
case FUNC_NODE_TYPE:
return evalFuncNode(node);
default:
warning("Unknown node type in eval");
return NAN_RET_VAL;
}
}
{double_pattern} {
llog(DOUBLE);
yylval.dval = strtod(yytext, NULL);
return DOUBLE;
}
{int} {
llog(INT);
yylval.dval = strtod(yytext, NULL);
return INT;
}
s_expr:
f_expr {
$$ = $1;
}
| number {
$$ = $1;
}
| QUIT {
exit(EXIT_SUCCESS);
}
;
add
, sub
, mult
, and div
.To compile CI-LISP, ensure you have Flex and Bison installed. Then run:
yacc -d cilisp.y
lex cilisp.l
cat cilisp.c lex.yy.c y.tab.c > t.c
gcc t.c -o cilisp -lm -lfl
Run the interpreter with:
./cilisp
CI-LISP will read from standard input.