|Q: Parsing operands & operators firstname.lastname@example.org (Randy Waldrop) (1998-09-05)|
|From:||Randy Waldrop <email@example.com>|
|Date:||5 Sep 1998 01:27:01 -0400|
|Keywords:||parse, SQL, question|
Here is a question for you compiler gurus.
I am writing, in java, a parser for SQL92-like conditional
statements. However, I am having trouble getting the tokens
parsed and ordered correctly.
1. Parse the statement into operands & operators. Parentheses
are handled as operators.
2. Process the tokens, putting them into Reverse Polish Notation
sequence and eliminating the parentheses. Paren levels,
operator precedence and associativity levels are honored.
The trouble I'm having is with unary operators, like 'NOT'.
They are binding to the wrong operand. For example:
input: (a NOT = b)
my rpn: a NOT b = (this is wrong)
rpn should be: a b = NOT (this is right)
The question: What is the logical rule which should cause the
'NOT' to apply to the result of the following operator (not
to the preceeding operand)?
I have a guess - that unary operators should be designated as
'prefix' or 'postfix', and 'NOT' should be 'prefix'. Does
this make sense?
[In the parser, I'd treat NOT = as an operator, since there aren't that many
relationals you'd have to special case, or else make it a meta-operator
that puts a flag bit on the operator it affects to tell the output
routine to emit a NOT after it emits the operator. Yes, this is a hack.
Welcome to the wonderful world of compilers. -John]
Return to the
Search the comp.compilers archives again.