Fog Creek Software
Discussion Board




Easy Parsing of IF statement?

Does anyone know of an easy way to parse IF THEN ELSE ENDIF statements?  Especially embedded ones.  I am currently doing so using a stack/nesting level method, which does work, but I'm thinking there must be an easier way, a way that would allow for better error checking.

This is a sample of what I'm doing, see modMatchITE.bas:

http://www.sswltd.com/downloads/expressionanalyzer.zip

(Yes, I know the code is not perfect.)

Dave B.
Thursday, October 16, 2003

why don't you try one of the lexer/parser packages out there?

lexico
Thursday, October 16, 2003

It looks like you're using a custom-built parser for different language elements. 

Are you familiar with the more traditional parsing techniques using a so-called context-free (CF) grammar?  These allow you to define the parsing elements using a structured language that describes how the input should be structured, and then parse the input using one of several different standard algorithms.  This approach generally gives you greater flexibility, plus you can take advantage of the collective wisdom from others who have used these algorithms.

You can either build your own code that incorporates one of the common algorithms, or you can incorporate a parsing engine that others have written.  Here are two that work with VB6 (and other languages):

http://www.devincook.com/goldparser/

http://www.programmar.com/main.shtml

Also, there's a great introductory textbook, "Parsing Techniques: A Practical Guide," that the authors have republished as a free PDF:

http://www.cs.vu.nl/~dick/PTAPG.html

Hope this helps.

Robert Jacobson
Thursday, October 16, 2003

I might try a parser generator if I deem it necessary, but I really don't like yacc or lex.

Thanks for the links Robert.

Dave B.
Thursday, October 16, 2003

Take the grammar of your language, tokenize and then meta-tokenize using state machines targeting the construct.

Pablo
Thursday, October 16, 2003

lemon is a fairly nice parser generator

SG
Friday, October 17, 2003

One approach (the one Excel uses) is to express this as a function. E.g if(<test>,<then>,<else>).

njkayaker
Monday, October 20, 2003

*  Recent Topics

*  Fog Creek Home