Subj : Re: How-to on writing an interpreter? To : comp.programming From : CTips Date : Sat Jul 16 2005 10:49 am Phlip wrote: > C. Rebert wrote: > > >>I'm trying to implement an interpreter for a programming language, but >>so far haven't been able to find any good materials on how to write a >>simple stack-based interpreter. I would appreciate it if someone could >>point me to some resources on writing such an interpreter. I'd be open >>to buying such materials, as long as they aren't ludicrously expensive. > > > Industrial-strength parsers come from parser generators like Lex and Yacc. > Learn them and live them; they have a thriving corpus and community. I would disagree; "industrial strength parsers" tend *NOT* to be written using yacc/bison. There are several reasons, including: - error handling - inability to represent many languages (roughly - bison/yacc can handle LALR(1) languages, but most languages are not LALR(1)) flex is a pretty decent scanner generator, but for a variety of reasons (including context-sensitive scanning) it doesn't get that much use industrihally. You're better off asking about parsers/scanners as well as interpreters in comp.compilers. If you just want to implement an interpreter, as opposed to an interpreter for a specific language, I'd recommend implementing an interpreter for Forth. Forth is a stack based language with an ultimately simple syntax and grammar (if one can call it that), so you can focus on the stack-based interpreter aspect. (It uses RPN so if you're an old-time HP calculator user, it will seem pretty obvious). e.g. look at the following statement in forth 1 3 + "1": means push 1 onto the stack *TOS++ = 1; "3": means push 3 onto the stack *TOS++ = 3; "+": means take the top two elements on the stack, add them together and push back onto the stack t0 = *--TOS; t1 = *--TOS; *TOS++ = t0 + t1; Forth also has an existing GPL environment (gforth), so you can cross-check your results against that. .