|
|
This thread is locked; no one can reply to it.
|
1
2
|
| What is a TOKEN (C++) |
|
relay01
Member #6,988
March 2006
|
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 }; Your conversion function may have the form, 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 ) What's a TOKEN? _____________________________________ |
|
X-G
Member #856
December 2000
|
... typedef struct { WhatKind kind; double num; char op; } TOKEN;
-- |
|
Indeterminatus
Member #737
November 2000
|
See here _______________________________ |
|
relay01
Member #6,988
March 2006
|
aah... i still don't get it... what are "lexemes"? _____________________________________ |
|
X-G
Member #856
December 2000
|
Eris on a freaking pogo stick, just use the damn wikipedia. Just try it. -- |
|
relay01
Member #6,988
March 2006
|
_____________________________________ |
|
kentl
Member #2,905
November 2002
|
You suck relay01! |
|
gnolam
Member #2,030
March 2002
|
Given the lack of code tags, I would have to agree with that statement. -- |
|
nonnus29
Member #2,606
August 2002
|
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
|
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. 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) 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:
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
|
Not if he also had to actually compile to machine code -- |
|
kentl
Member #2,905
November 2002
|
Yeah but technically that's not compiler theory. |
|
Thomas Fjellstrom
Member #476
June 2000
|
Um, compiling code isn't compiler theory? Whod'a thunk it! -- |
|
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
|
I think you need to read what I said again 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
That last message I was pulling your chain -- |
|
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
|
Maybe hes just old? -- |
|
X-G
Member #856
December 2000
|
Writing an assembler is obviously a compiler theory thing. A compiler is a compiler no matter what it compiles. -- |
|
Thomas Fjellstrom
Member #476
June 2000
|
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. -- |
|
X-G
Member #856
December 2000
|
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. -- |
|
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 what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|
Thomas Fjellstrom
Member #476
June 2000
|
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 -- |
|
kentl
Member #2,905
November 2002
|
That's some crazy stuff Johan. Sounds cool Thomas. Tell us more about them? Perhaps show some of that assembler code? |
|
|
1
2
|