BUILDING YOUR OWN C INTERPRETER This article is based on his book Born to Code in C (Osborne/McGraw Hill).
He can be reached at RR 1, Box 130, Mahomet, IL 61853. In this article, I develop a C interpreter that can execute a subset of K ANSI C. The interpreter not only is functional as presented, but is designed so that you can easily enhance and extend it. In fact, you can even add features not found in ANSI C if you choose. By the time you finish reading this article, you'll have a C interpreter you can use and enlarge, and you will have gained considerable insight into the structure of the C language itself. Although ANSI C has only 32 keywords, it is a rich and powerful language. It would, of course, take far more than a single article to fully describe and implement an interpreter for the entire C language. Instead, this C interpreter understands a fairly narrow subset of the language. Table 1 lists the features that are implemented in it. Table 1: Features provided by this C interpreter Parameterized functions with localIn order to simplify and shorten the source code to the interpreter, I have imposed one small restriction on the C grammar: The targets of the if, while, do, and for must be blocks of code surrounded by beginning and ending curly braces. You may not use a ladies oakley sunglasses single statement. For example, my interpreter will not correctly interpret code in Example 1; instead, you must write code like that in Example 2. Because the objects of the program control statements are often blocks of code anyway, this restriction does not seem too harsh. (With a little effort, though, you can remove this restriction.) Example 1: This oakley sunglasses factory outlet C interpreter will not interpret this code Example 2: This C interpreter will interpret this code Reducing the Source Code to its Components To create the C interpreter, we first construct the expression evaluator and the piece of code that reads and analyzes expressions, the expression parser. There are several different ways to design an expression parser for C. Many commercial compilers use a table driven parser that is generally created by a parser generator program. While table driven parsers are oakley safety glasses usually faster than other methods, they are hard to create by hand. For this C interpreter I will use a recursive descent parser, which is essentially a collection of mutually recursive functions that process an expression. If the parser is used in a compiler, then its function is to generate the proper object code that corresponds to the source code. In an interpreter, however, the object of the parser is to evaluate a given expression. Fundamental to all interpreters (and compilers, for that matter) is a special function that reads the source code and returns the next logical symbol from it. For historical reasons, these logical symbols are generally referred to as tokens, and computer languages in general (and C in particular) define programs in terms of tokens.
This C interpreter categorizes tokens as shown in Table 2. The function that returns tokens from the source code for the C interpreter is called get_token() and is shown in Listing One, lines 313 through 423. The get_token() function uses the global data and enumeration types oakley canteen in Figure 1.
Prev: oakley sungkasses
Next: oakley batwolf