Re: How to tell the difference bewteen comparing and assigning
- Posted by "Carl W." <euphoria at cyreksoft.yorks.com> May 13, 2002
- 442 views
jbrown wrote: > Aside from not covering all situations, what do i do about this: > > if a > b then a = b else b = a end if > > i could try looking only between the "if" and "then" tokens (and ditto > for while..do) but it would be hard to do that for all possible > situations: > > ?(a=b) --check for ( > ?a[b=b] --check for [ > ? a=b --check for ? > a = a = b --check for previous = > etc etc etc > > as there are too many exceptions and to make a parser which takes care > of this cleanly would be quite difficult. i was thinking about making my > preprocessor use == for comparision and := for assignment and ignore the > = sign completely. > (Perhaps even make the = sign an illegal token, although that would be > going to the extreme.) I'd prefer not to do that however, so if you or > anyone else has an idea on how this could be done, it would be greatly > appreciated. I think what you need to do is parse the source by context. i.e. Judge what you need to do by what has gone before. There's only one statement form using '=' that is an assignment; All others are boolean statements. Assignments *always* begin with a variable or a sequence index, so you can safely ignore 'what comes next' even if it does contain a second equals sign. In fact, the *only* valid statements _beginning_ with a variable are assignments. Here's a little euphoria-like pseudocode: while Parsing_Source do get next token end get look for a typename, if there is one, it's a variable declaration store variable declarations in a symbol table for future ref. end look parse other syntax -- Any '=' found in here is part of a boolean expression. end parse look for so-far-unmatched token in the symbol table. if found: -- must be an assignment get next non-whitespace char end get check for a '['; if it is: -- skip sequence subscript skip past the next ']' get next non-whitespace char end get end check check to see if it's an '='; fail with error if not. get an expression -- Any '=' found here is part of a boolean expression. end get end look end while HTH, Carl -- [ Carl R White -=- aka -=- Cyrek the Illogical ] [ () E-mail...: cyrek{}cyreksoft.yorks.com ] [ /\ URL......: http://www.cyreksoft.yorks.com ]