Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED!nerds-end From: Christian Gollwitzer Newsgroups: comp.compilers Subject: Re: I have managed to parse and evaluate using an AST. I got stuck in statements and block Date: Thu, 19 Feb 2015 10:22:00 +0100 Organization: A noiseless patient Spider Lines: 61 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <15-02-028@comp.compilers> References: <15-02-027@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: miucha.iecc.com 1424390214 17105 2001:470:1f07:1126:0:676f:7373:6970 (19 Feb 2015 23:56:54 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Thu, 19 Feb 2015 23:56:54 +0000 (UTC) Keywords: interpreter, parse Posted-Date: 19 Feb 2015 18:56:54 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1530 Am 18.02.15 um 20:20 schrieb mehmet.coskun@gmail.com: > I am writing a simple interpreter. I have managed to implement: > > Lexer > Recursive descent parser > Building AST Any reason why you don't leave these steps to a tool like lex and yacc? Would be much easier to enhance the language. > Evaluating AST > > My interpreter can evaluate arithmetic expressions and boolean expressions. > > But I got stuck in implementing statements, code block, if and while statements. > [...] > > Can you please help me to design if, while, statement and statement > block functions? How can I implement this? What do I miss here in my > implementation that caused me getting stuck without a working > interpreter. Writing an interpreter is generally easier than a compiler. You just need to execute the commands in high-level language. Compound statements generally work by recursive invocation of the interpreter on the blocks. This is not much different from your Evaluate() function, which evals the subexpressions recursively. Let's assume you have a Dispatch function which switches on the NodeType and invokes a specialized function to interpret it. Then an if would be somthing like (pseudocode) proc if(condition, trueblock, falseblock) { bool doit = Evaluate(condition); if (doit) { Dispatch(trueblock); } else { Dispatch(falsbelock); } } It's really not much more than just writing out the if-then-else in the implementation language (which is Pascal in your case, I assume) and decorating execution blocks with Dispatch() and expression blocks with Evaluate(). Of course, you need to enhance your Node structure such that it can hold three elements, instead of only two. Or an arbitrary list of arguments, which would come handy if you choose to implement n-ary functions. Of course, such a simplistic interpreter does not provide the more advanced features in modern interpreters like stackless execution (for coroutines) or fast execution (due to branching and function calls). Exceptions are possible using return codes on the Dispatch function. > Please note that I have to use data structures like linked list, tree > and similars. But not object oriented or a more high level language. Your choice, it should be possible - but you should add in a another very handy data structure, namely associative arrays (hash tables). Linked lists are certainly awkward to implement variable lookup. Christian