Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What is a TOKEN (C++)

Credits go to Indeterminatus and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
What is a TOKEN (C++)
relay01
Member #6,988
March 2006
avatar

I'm not beyond asking for help especially when my professor is a real tool.

this is my homework assignment

( 20 points ) We discussed in class converting an infix expression to a postfix expression. In the implementation of in2post.cpp, we have used an operator stack to hold the operators and both the inputs and outputs are strings; it can only handle integers. You may generalize it by passing in the infix expression in a stack and output the postfix expression in a queue. You may define a TOKEN which can be an operator or a number as follows:

enum WhatKind { oper, number };
typedef struct {
WhatKind kind;
double num;
char op;
} TOKEN;

Your conversion function may have the form,
void in2post( stack <TOKEN> &infix, queue <TOKEN> &postqueue );

Before calling this function, you have to parse the infix expression into TOKENs and put them in the infix stack. The output is the postfix tokens saved in postqueue.

Next, write a function to evaluate the postfix expression. You may first convert the postfix queue to the post stack. Your function may look like:

double evaluate ( stack <TOKEN> &post )
{
stack eval;
double first, second, ans;
char bin_op;
TOKEN tp, te;
while ( !post.empty() ){
tp = post.top();
post.pop();
if ( tp.kind == number )
eval.push( tp );
else {
...
}
}
....
}

What's a TOKEN?

_____________________________________

X-G
Member #856
December 2000
avatar

...

typedef struct {
WhatKind kind;
double num;
char op;
} TOKEN;

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Indeterminatus
Member #737
November 2000
avatar

See here ;D

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

relay01
Member #6,988
March 2006
avatar

aah... i still don't get it... what are "lexemes"?

_____________________________________

X-G
Member #856
December 2000
avatar

Eris on a freaking pogo stick, just use the damn wikipedia. Just try it.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

relay01
Member #6,988
March 2006
avatar

:'( ok

_____________________________________

kentl
Member #2,905
November 2002

You suck relay01! ;)

gnolam
Member #2,030
March 2002
avatar

Given the lack of code tags, I would have to agree with that statement.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

nonnus29
Member #2,606
August 2002
avatar

Sheesh, I had to do that in MIPS assembler, and here you get to use a nice high level language. What are you complaining about?

???

Quote:

especially when my professor is a real tool.

Yeah, because he did half the work for you.... ::)

TeamTerradactyl
Member #7,733
September 2006
avatar

relay01,

I don't know if your question was answered or not.

I believe your professor means that TOKEN should be one of the math functions.
A * B - C / (D + E)

Each of the MULTIPLY, DIVIDE, ADD, SUBTRACT are the "TOKENS" to which your professor is referring.

You should probably have two strings: infixString and postfixString.

Basically:

1) You are given an INFIX problem (like above)
2) Parse the INFIX expression, and print out the resulting POSTFIX expression
- i) For each character that is read in from the infixString, perform the following:
- ii) If the input character is a letter, append that letter to the end of postfixString
- iii) If the input character is a TOKEN, put that TOKEN onto a stack which you will retrieve later
- iv) If the next input character is another letter, get (read: POP) the TOP element from your stack and insert it onto the end of your postfixString.
- v) You have to make sure to check the preference of the TOKENS when you remove them: *, / and % have higher precedence than + or -, so be sure to remove the correct one.
3) Output, at the very end, the POSTFIX string

The only exception to the above is if your professor's code includes parentheses (like in my example INFIX expression). If he does, once you encounter a ), you have to loop though all the items in your stack until you find the ( and push everything you find into your postfixString.

This way, the above will do this:
A * B - C / (D + E)

infixString          postfixString                  myStack
-----------          -------------                  -------
A                    A                              (empty)
*                                                   *
B                    AB*                            (empty) -- (because we removed the "*")
-                    AB*                            -
C                    AB*C                           - (not removed because the NEXT character is higher-precedence: "/")
/                    AB*C                           -/
(                    AB*C                           -/(
D                    AB*CD                          -/(
+                    AB*CD                          -/(+
E                    AB*CDE+                        -/( (removed the "+")
)                    AB*CDE+                        -/ (removed the "(")
                     AB*CDE+/                       - (still inside the FOR..LOOP for the ")" TOKEN)
                     AB*CDE+/-                      (empty)

I hope that helps somewhat with your assignment.

kentl
Member #2,905
November 2002

Quote:

Sheesh, I had to do that in MIPS assembler, and here you get to use a nice high level language. What are you complaining about?

Well if you are learning compiler theory then I would argue that it's better to stay high level. You'll get more done faster and learn more about the stuff you are supposed to study (compiler theory, not assembler).

Thomas Fjellstrom
Member #476
June 2000
avatar

Not if he also had to actually compile to machine code ;) Knowing the assembler would go a long way to understanding the workings of the chip/system, and what to compile the code to ;D

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kentl
Member #2,905
November 2002

Yeah but technically that's not compiler theory. :)

Thomas Fjellstrom
Member #476
June 2000
avatar

Um, compiling code isn't compiler theory? Whod'a thunk it!

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kentl
Member #2,905
November 2002

Assembler isn't compiler theory. If it was then you could say that any format is compiler theory, as it's necessary knowledge if you want to compile something into said format (PostScript, PDF, SVG, XML, HTML and so on). Compiler theory in itself are the generic methods used to turn one format into another.

Thomas Fjellstrom
Member #476
June 2000
avatar

I think you need to read what I said again :P

I said:

Knowing the assembler would go a long way to understanding the workings of the chip/system, and what to compile the code to ;D

That last message I was pulling your chain :P

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kentl
Member #2,905
November 2002

Yeah he needs to know assembler in that case. Can't really get why he head to implement his compiler in assembler though, but I guess it could be a good exercise if you study assembler.

Thomas Fjellstrom
Member #476
June 2000
avatar

Maybe hes just old? ;) IIRC his day job involves a lot of COBOL. (at least I think its him..)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

X-G
Member #856
December 2000
avatar

Writing an assembler is obviously a compiler theory thing. A compiler is a compiler no matter what it compiles. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Thomas Fjellstrom
Member #476
June 2000
avatar

I think hes saying writing it IN assembler isn't so much compiler theory, and may get in the way of teaching pure compiler theory.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

X-G
Member #856
December 2000
avatar

Well, compiler theory is just that, theory: It's got nothing to do with whatever language you're using should you want to actually write a compiler.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

kentl
Member #2,905
November 2002

Yes, that's what I'm saying Thomas. I haven't said that writing an assembler isn't a compiler theory thing, at least I don't think so? I've said that learning assembler isn't a compiler theory thing and that you would probably want to use a high level language if you want to create a compiler. As you will get more stuff done and have a greater freedom to experiment.

Johan Halmén
Member #1,550
September 2001

When I started with assembler, I was the assembler. I wrote the assembler code with pencil on paper. Then, using a table, I looked up the hexcodes and wrote them down on the same paper next to the assembler mnemonics. Then I had to count the bytes to get all absolute and relative jumps right. Then I typed in the hexcode.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Thomas Fjellstrom
Member #476
June 2000
avatar

I couldn't stand the thought of having to do that for my two VM projects, so I ended up writing some nice assemblers for both of them ;) first one was two pass, so you could actually reference labels that are declared after you use them :o

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

kentl
Member #2,905
November 2002

That's some crazy stuff Johan. :) When was that?

Sounds cool Thomas. Tell us more about them? Perhaps show some of that assembler code? :)

 1   2 


Go to: