Hey everyone!
I'm looking for an algoritem which converts an infix squence to postfix. My problem is that I don't know what to do with the brackets in the squence. Here's what I have so far...

counter=0
init(stack)
While not end of line
read x
if x is a number
print x
counter++
else //x is an oprenad
push(stack,x)
if counter==2
y=pop(stack)
print y
counter=0
while not_empty(stack)
y=pop(stack)
print y

I believe this would work if converted into code. How do I make it work if the expression contains brackets? i dont want the output to have any brackets in it

I would appreciate any help you could offer, and no code is needed (just an algoritem).

Thanks

11-01-2002

Liberty4all

Re:correction

adding spaces....

Code:

counter=0
init(stack)
While not end of line
read x
if x is a number
print x
counter++
else //x is an oprenad
push(stack,x)
if counter==2
y=pop(stack)
print y
counter=0
while not_empty(stack)
y=pop(stack)
print y

You can suggest your own idea of course.....

Code tagged by Hammer

11-01-2002

Liberty4all

sorry...

Take 3.. it won't let me post spaces for some reason

BEGIN
counter=0
init(stack)
While not end of line
{read x
if x is a number
{print x
counter++}
else //x is an oprenad
{push(stack,x)}
if counter==2
{y=pop(stack)
print y
counter=0}
}
while not_empty(stack)
{y=pop(stack)
print y}
END

If that's what you mean, I can help you. My parser uses RPN internally to speed things up.

AND:
Remove your two extra posts and edit your first message using [code] at the beginning and [/code] at the end.

11-01-2002

Imperito

Put you text in CODE tags to get the spaces right.

You know when you came here there was something at the top that said somehting along the lines of "Posting code? Please dear god read this first so the moderators don't pitch a fit".

Had you read it first....

You would have learned the glory of CODE tags.

Put this [CÖDE] before your code and this [/CÖDE] after it. (Use regular 'O's)

11-01-2002

endo

RPN == postfix

11-03-2002

Liberty4all

Quote:

Originally posted by Sang-drax Do you mean like RPN (reverse polish notation)?

i.e. 2 + (3*4) equals2 3 4 * +

If that's what you mean, I can help you. My parser uses RPN internally to speed things up.

AND:
Remove your two extra posts and edit your first message using [code] at the beginning and [/code] at the end.

Yeah, that is what I mean, I would really appreciate if u can help.

I tried to edit/delete my posts but I get an "invalid username or pw" message. It's strange cause it doesn't give me any problems with signing in to post. I'm sorry for messing up, it won't happen again.

11-03-2002

Sang-drax

Hmm, I'll try to explain the algorithm I use. It works perfectly.

First, define every operator that is going to be used and assign each operator a predecence. Remember that ( and ) also are operators and they should have the highest predecence.

You'll also need a Token class, a token can contain a number or an operator (and functions and identifiers for more advanced parsers). A token class may look like this:

//Replace the operator and operands
//with a single expression token using
//container functions
}
}

//If everything went OK, the vector
//should now contain only one token
//with the expression in it.
return tvector[0].expression;
}

Very esoteric, I know, but I'll show how it works using my example vector.
The vector now looks like this:
{2} {*} {(} {5} {+} {6} {)} {^} {2}

First, the parentheis is found, the tokens inside is converted resursivly to this: { {5} {6} {+} }, which is returned.

After replacement, the vector looks like this:
{2} {*} { {5} {6} {+} } {^} {2}
Note that the nested {} counts as a single token.

The next operator found is ^, the operands and operator are rearranged and after replacement and merging of the queues the vetor looks like this:
{2} {*}

The last operator found is *, the same procedure is followed and the final vector (only one token now) is:
{ {2} {5} {6} {+} {2} {^} {*} }
And we're done.

2*(5+6)^2 is2 5 6 + 2 ^ * in RPN

I'll attach some real code from my parser, however I don't think it will help much. It is more generic, using a DataType class instead of number, and features binary and tertiary ooperators.
This typedef is used:

Code:

typedef std::queue<Petter::Token> Expression;

I think this is my longesy post in this forum, but also the hardest to understand, but I'm not a teacher, sorry. :(