|
|
I already have a section on the subject of parsing expressions, under the heading of Quick BASIC (because BASIC is freely available and otherwise accessible to many people who don't yet know how to parse expressions but may want to learn.) But I've also developed similar code in the c language. Here's one incarnation. An expression toolI needed a command-line tool for generating computed series' of data that I could then pipe into other programs for processing (piping is the process of passing the output of one program as input to another program.) I needed the ability to generate not just one series, but the ability to produce many hundreds of them in sequence. And I needed to break up each of them with a header and trailer message that could include pertinent information about each group. This page is about the resulting tool I wrote. Probably the fastest way to get across some of the capabilities and its syntax is to provide some examples.
Example 1 simply prints out the sine of PI/3 on one line and then quits. (I've chosen to put the command I typed in one color and the output in another color.) This illustrates that the program can be used as a general purpose calculator. It includes quite a few different transcendentals, including hyperbolic, two different types of random number generations: uniform and gaussian distributed. And some more. It should accept most familiar forms of algebraic expressions and provide reasonable results. The values are displayed to 10 significant digits, by default. (But the default can be changed -- discussed later on, here.)
Example 2 shows a problem. I added a couple of spaces and an error resulted. The problem created here is due to the way that arguments are passed by DOS to EQ and my intent that EQ could support multiple expressions, looping, and statements. DOS uses spaces to separate arguments passed to EQ, so inserting spaces as in example 2 makes it look like three different expressions to EQ instead of just one. However, here's a way to handle that:
Example 3 points out that you can force DOS to include an expression that includes spaces into a single expression or statement if you use double quote marks to surround the text. Just keep that in mind, when using the program. To continue:
Example 4 prints out three lines. The colon (:) indicates printing a message instead of a calculation result. You can either enclose it in quotes, or not. But quotes are needed if there are special characters like spaces, where DOS needs to know that it all should be included into a single command line argument. The double quotes themselves aren't printed, though.
Example 5 shows that you can use variable names. Assignments statements aren't printed, just executed. So that's why there is only one line of output, the result of computing the sine of 'a'. The program includes only two pre-defined variables: pi and e. Case is not important here, so you can use either upper or lower or mixed cases with the same results.
Example 6 shows that the text output permitted by the colon includes the ability to insert the values of variables, if you prefix them with the ampersand (&) character.
Example 7 adds two new elements, needed for looping. An argument starting with an open-set-sign is taken to be the start of a loop and the expression that follows it determines the number of iterations. The loop will continue until the close-set-sign is seen. This allows repeated lines of output, each with a different calculation. In example 5, the output is a series of sine results for angles varying from 0 degrees to 90 degrees. In the above case, the variable 'n' determines how many equal segments to use in that 90-degree angle interval.
Example 8 just illustrates that these loops can be successive. But they also can be nested, as well. They are fairly general purpose.
Example 9 shows a header, a list of data pairs, and a trailer. It illustrates most of the capability of the program. I've also added the ability to specify the number of significant digits to use, when displaying values:
Example 10 shows how to limit the number of significant places in the output for an expression. This specifier is not allowed in statements, as calculations proceed at double precision significance at all times. However, it is allowed in record outputs.
Example 11 shows how that works. In all cases, the specifier must be provided as simple digits; expressions are not handled. The default is 10 digits and the maximum allowed is 17. Finally, I've just recently added the ability to copy the output from EQ to the Windows clipboard, using the -c arugment option as the first argument to EQ. You may also get help from it, using -h as the first argument. EQ, the toolboxBut while the above examples show you what the final program can do, it doesn't really illustrate anything about the expression parser that is included in the source code. That parser not only has the ability to support general expressions and variable assignment statements, but it can also pre-compile these expressions for faster execution. The expression parser can be used in your own c programs with about as much ease as using the standard library function strtod(). And the pre-compiled expressions will execute faster. (The code is an internally designed 'push-pop' code, not actual machine language.) I'm including a copy of the source code and an executable for this expression parser and compiler, along with the useful harness for it that is illustrated above.
As usual, source code and make files are provided with this program. If the subject of expression parsing and evaluation is interesting to you, I have some pages on the subject under the Quick BASIC heading. The information is general enough to be useful for programming in other languages or just understanding some of the details. The starting point page is here.
Last updated: Monday, February 13, 2006 14:09
|