Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - How to convert Infix notation to postfix notation

 
Thread Tools Search this Thread
Old 10-26-2009, 04:33 AM   #1
Default How to convert Infix notation to postfix notation


i have a string as (a+b)+8-(c/d) in Infix form.

how can i convert it to postfix form using C language,,,????




Tameem
  Reply With Quote
Old 10-26-2009, 06:30 AM   #2
spinoza1111
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 26, 12:33*pm, Tameem <etam...@gmail.com> wrote:
> i have a string as (a+b)+8-(c/d) in Infix form.
>
> how can i convert it to postfix form using C language,,,????


I will assume that that string is not the ONLY string you have to
convert. If it is, then the answer is

int main()
{
printf("ab+8+cd/-\n");
}

Each one of the following productions should be written as a separate
C function.

expression -> additionFactor [ "+"|"-" expression ]
additionFactor -> multiplicationFactor [ "*"|"/" expression ]
multiplicationFactor -> term
term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance

Write code using the above "productions" that emits postfix code. For
each production write one C function. I won't write C for you because
I don't like C, I suck at it through disuse consequent on my dislike,
and it's your homework assignment, not mine.

"->" means "consists of the following material left to right"

"|" means "or"

Lower case and camelCase words are grammar categories that will
correspond to procedures in YOUR code

Upper case words are lexemes recognised by C code

Quoted material occurs as is.

Material in square brackets is optional. An expression could be just
an additionFactor. In turn an additionFactor can be just a
multiplicationFactor and a multiplication factor a number. Therefore,
"3" is an expression.

When you parse a "term" as "(" expression ")" you must look ahead in
the overall algorithm I shall give you to find, not the first right
parenthesis, but the one that actually balances the left parenthesis.
To do this, maintain a counter. Increment it by one for each left
parenthesis the lookahead sees: decrement it by one for each right
parenthesis. When you see right parenthesis and the counter goes to
zero, you have found the right parenthesis.

OK, here's the untested and uncompiled C Sharp like pseudo code for
"expression": convert it to C:

string expressionMain(string strInstring)
{
int intIndex1 = 0;
string strPolish = "";
return expression(strInstring, ref intIndex1, ref strPolish);
}
string expression(string strInstring, ref int intIndex1, ref string
strPolish)
{
if (!additionFactor(string strInstring,
ref intIndex1,
ref strPolish)) return "Not valid";
if (strInstring[intIndex]=='+' || strInstring[intIndex]=='-')
{
int intIndexSave = intIndex1; intIndex1++;
if (expression(strInstring, ref intIndex1, ref strPolish))
strPolish += strInstring[intIndexSave];
else return("Not valid");
}
return strPolish;
}

C Sharp idioms: ref followed by type in a formal parameter list in a
function decl is like using asterisk in front of a parameter in C. It
means that the parameter is passed by reference. In C Sharp, a very
cool language, you must ALWAYS use the keyword ref in the "actual
parameters" passed when you call the function just so you know what
the hell you are doing, as opposed to C which likes to fool you.

Chapter 3 of my book Build Your Own .Net and Compiler (Edward G.
Nilges, Apress May 2004) provides a complete solution for this
homework but in Visual Basic .Net. Therefore this is not a commercial
promotion. If you find my book useful, buy it, check it out of the
library, or steal it.


spinoza1111
  Reply With Quote
Old 10-26-2009, 11:29 AM   #3
Michael Schumacher
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
Tameem wrote:

> i have a string as (a+b)+8-(c/d) in Infix form.
>
> how can i convert it to postfix form using C language,,,????


This has very little to do with the /language/ C, i.e., there's
no "built-in" way to accomplish this conversion. However, it's
of course possible to implement such a converter in C, e.g., by
writing a recursive-descent parser (for a good deal of details,
see <http://en.wikipedia.org/wiki/Operator-precedence_parser>).

You might also want to take a look at the GNU assembler (gas,
which is part of the GNU binutils package), which implements
such a recursive-descent parser to evaluate constant expressions.


mike



Michael Schumacher
  Reply With Quote
Old 10-26-2009, 12:09 PM   #4
Gene
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 26, 12:33*am, Tameem <etam...@gmail.com> wrote:
> i have a string as (a+b)+8-(c/d) in Infix form.
>
> how can i convert it to postfix form using C language,,,????


It this homework?


Gene
  Reply With Quote
Old 10-26-2009, 12:20 PM   #5
Ben Bacarisse
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
spinoza1111 <> writes:

> On Oct 26, 12:33Â*pm, Tameem <etam...@gmail.com> wrote:
>> i have a string as (a+b)+8-(c/d) in Infix form.
>>
>> how can i convert it to postfix form using C language,,,????

>
> I will assume that that string is not the ONLY string you have to
> convert. If it is, then the answer is
>
> int main()
> {
> printf("ab+8+cd/-\n");
> }
>
> Each one of the following productions should be written as a separate
> C function.
>
> expression -> additionFactor [ "+"|"-" expression ]
> additionFactor -> multiplicationFactor [ "*"|"/" expression ]
> multiplicationFactor -> term
> term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance


This is not the usual grammar for expressions and is poor advice for
someone learning this stuff.

> Write code using the above "productions" that emits postfix code.


To the OP: please don't; Spinoza1111 is leading you astray. Post in a
group about programming (comp.programming is a good one) for better
advice on how to go about this. Also, if this is homework/coursework,
be open about that and explain how far you have been able to get on
your own.

<snip off-topic C#>
--
Ben.


Ben Bacarisse
  Reply With Quote
Old 10-27-2009, 02:49 AM   #6
spinoza1111
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 26, 8:20*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> spinoza1111<spinoza1...@yahoo.com> writes:
> > On Oct 26, 12:33*pm, Tameem <etam...@gmail.com> wrote:
> >> i have a string as (a+b)+8-(c/d) in Infix form.

>
> >> how can i convert it to postfix form using C language,,,????

>
> > I will assume that that string is not the ONLY string you have to
> > convert. If it is, then the answer is

>
> > int main()
> > {
> > printf("ab+8+cd/-\n");
> > }

>
> > Each one of the following productions should be written as a separate
> > C function.

>
> > expression -> additionFactor [ "+"|"-" expression ]
> > additionFactor -> multiplicationFactor [ "*"|"/" expression ]
> > multiplicationFactor -> term
> > term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance

>
> This is not the usual grammar for expressions and is poor advice for
> someone learning this stuff.
>
> > Write code using the above "productions" that emits postfix code.

>
> To the OP: please don't;Spinoza1111is leading you astray. *Post in a


Ben, please indicate the errors and don't imitate Seebach by
discussing personalities instead of ideas. As it is, you sound like a
fundamentalist *imam*.

> group about programming (comp.programming is a good one) for better
> advice on how to go about this. *Also, if this is homework/coursework,
> be open about that and explain how far you have been able to get on
> your own.
>
> <snip off-topic C#>
> --
> Ben.- Hide quoted text -
>
> - Show quoted text -




spinoza1111
  Reply With Quote
Old 10-27-2009, 02:54 AM   #7
spinoza1111
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 26, 8:20*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> spinoza1111<spinoza1...@yahoo.com> writes:
> > On Oct 26, 12:33*pm, Tameem <etam...@gmail.com> wrote:
> >> i have a string as (a+b)+8-(c/d) in Infix form.

>
> >> how can i convert it to postfix form using C language,,,????

>
> > I will assume that that string is not the ONLY string you have to
> > convert. If it is, then the answer is

>
> > int main()
> > {
> > printf("ab+8+cd/-\n");
> > }

>
> > Each one of the following productions should be written as a separate
> > C function.

>
> > expression -> additionFactor [ "+"|"-" expression ]
> > additionFactor -> multiplicationFactor [ "*"|"/" expression ]
> > multiplicationFactor -> term
> > term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance

>
> This is not the usual grammar for expressions and is poor advice for
> someone learning this stuff.
>
> > Write code using the above "productions" that emits postfix code.

>
> To the OP: please don't;Spinoza1111is leading you astray. *Post in a


I would have thought given your technical capabilities that you would
be able, instead of engaging in Fox news style politics of personal
destruction, to identify all flaws in my approach, whether in the
formal grammar of C# pseudo code.

If you cannot, I need an apology from you. NOW, punk.

> group about programming (comp.programming is a good one) for better
> advice on how to go about this. *Also, if this is homework/coursework,
> be open about that and explain how far you have been able to get on
> your own.
>
> <snip off-topic C#>


C# is what C should be. As I indicated, C Sharp was used as pseudocode
because it is probably a homework assignment and the OP needs to do
this homework. The OP probably received poor algorithm guidance from
the teacher, especially if the teacher was like most people here.
> --
> Ben.- Hide quoted text -
>
> - Show quoted text -




spinoza1111
  Reply With Quote
Old 10-27-2009, 03:15 AM   #8
Ben Bacarisse
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
spinoza1111 <> writes:

> On Oct 26, 8:20Â*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> spinoza1111<spinoza1...@yahoo.com> writes:

<snip>
>> > expression -> additionFactor [ "+"|"-" expression ]
>> > additionFactor -> multiplicationFactor [ "*"|"/" expression ]
>> > multiplicationFactor -> term
>> > term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance

>>
>> This is not the usual grammar for expressions and is poor advice for
>> someone learning this stuff.
>>
>> > Write code using the above "productions" that emits postfix code.

>>
>> To the OP: please don't;Spinoza1111is leading you astray. Â*Post in a


[The formatting errors in the above were added by you. My typing is
bad, but not /that/ bad.]

> I would have thought given your technical capabilities that you would
> be able, instead of engaging in Fox news style politics of personal
> destruction, to identify all flaws in my approach, whether in the
> formal grammar of C# pseudo code.


It's not topical here. There must be lots of groups where you can get
advice on writing a grammar for simple expressions, but I don't know,
off hand, where would be the best place. I suggested comp.programming
to the OP, but that is probably not the best place for your question.

> If you cannot, I need an apology from you. NOW, punk.


That's OK then.

<snip>
--
Ben.


Ben Bacarisse
  Reply With Quote
Old 10-27-2009, 06:39 AM   #9
spinoza1111
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 27, 11:15*am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> spinoza1111<spinoza1...@yahoo.com> writes:
> > On Oct 26, 8:20*pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> >>spinoza1111<spinoza1...@yahoo.com> writes:

> <snip>
> >> > expression -> additionFactor [ "+"|"-" expression ]
> >> > additionFactor -> multiplicationFactor [ "*"|"/" expression ]
> >> > multiplicationFactor -> term
> >> > term -> LETTER | NUMBER | "(" expression ")" // Rightmost must balance

>
> >> This is not the usual grammar for expressions and is poor advice for
> >> someone learning this stuff.


You have not shown that it is INCORRECT. It's possible that the
"usual" grammar is incorrect (as in the case of left recursion) given
Gresham's Law that 99% of everything is crap. You need to show that
the grammar is either actually incorrect or not suitable for manual
interpretation, and I do not, personally think you qualified to do so.
This takes experience in writing an actual parser by hand.

In the first production it is obvious that on return from
additionFactor(), the parser can advance the index, and check it for
greater than end. If the index is past the end, we're done. If it is
not, the parser can check either for a plus or a minus.

If either character occurs the parser then calls itself with a source
string that is necessarily smaller guaranteeing the safety of the
recursion. But, there must be an expression when there is an operator.

Disprove this informal logic and desist the Fox news crap, please.
>
> >> > Write code using the above "productions" that emits postfix code.

>
> >> To the OP: please don't;Spinoza1111is leading you astray. *Post in a

>
> [The formatting errors in the above were added by you. *My typing is
> bad, but not /that/ bad.]
>
> > I would have thought given your technical capabilities that you would
> > be able, instead of engaging in Fox news style politics of personal
> > destruction, to identify all flaws in my approach, whether in the
> > formal grammar of C# pseudo code.

>
> It's not topical here. *There must be lots of groups where you can get
> advice on writing a grammar for simple expressions, but I don't know,
> off hand, where would be the best place. *I suggested comp.programming
> to the OP, but that is probably not the best place for your question.


His goal is to write a C program, but we don't want to give him the
homework solution. It appears that his instructor in fact knows about
as much as you about writing a recursive descent parser by hand.
Therefore he's in the right place.
>
> > If you cannot, I need an apology from you. NOW, punk.

>
> That's OK then.
>
> <snip>
> --
> Ben.




spinoza1111
  Reply With Quote
Old 10-27-2009, 05:20 PM   #10
John Bode
 
Posts: n/a
Default Re: How to convert Infix notation to postfix notation
On Oct 25, 11:33*pm, Tameem <etam...@gmail.com> wrote:
> i have a string as (a+b)+8-(c/d) in Infix form.
>
> how can i convert it to postfix form using C language,,,????


As others have said, there's no C-specific way of doing this.
However, here are the general steps you'll need to take:

First, you will need to separate the string into distinct *tokens*.
In your example, you have four kinds of tokens -- operators
('+','-','*','/'), separators ('(',')'), constants ('8'), and
identifiers ('a','b','c','d'). Generally, you'll have a set of rules
that determines what characters make up a particular kind of token
(for example, an identifier must start with a letter and consist of
only letters and digits: "a1", "foo", "x", etc.). As you scan each
character, you'll check against the currently active rule and if it
matches, you'll add it to the token.

There are two ways to go on the next step. You can either build a
full-blown recursive descent parser (a decent explanation appears on
Wikipedia), or you can use a simple stack-based converter. For the
stack-based converter, as you read the expression, you'll pass
constants and identifiers straight to output, and you'll push and pop
operators on the stack based on their precedence. If the operator on
the top of the stack has a lower precedence than the current operator,
then push the current operator. If the operator on the stack has an
equal or higher precedence than the current operator, then pop the
operator off the stack and write it to output, then push the current
operator on the stack. lparens are always pushed onto the stack;
rparens cause everything on the stack to be popped to the next
lparen. Neither lparens nor rparens are written to output.

Given your example, the operations would look something like this:

1. Read '(', push it onto the stack. Stack contents = {'('}
2. Read 'a', write it to output. Output string = "a"
3. Read '+', push it on the stack: {'(', '+'}
4. Read 'b', write it to output: "a b"
5. Read ')', pop everything off the stack to the next '(', but don't
write '(' to output. Output string is "a b +", stack contents = {}
6. Read '+', push it onto the stack: {'+'}
7. Read '8', write it to output: "a b + 8"
8. Read '-', has same precedence as '+', so pop '+' from stack, write
it to output, and push '-': "a b + 8 +", {'-'}
9. Read '(', push it onto the stack: {'-', '('}
10. Read 'c', write it to output: "a b + 8 + c"
11. Read '/', push onto stack: {'-', '(', '/'}
12. Read 'd', write to output: "a b + 8 + c d"
13. Read ')', pop everything to '(': "a b + 8 + c d /", {'-'}
14. End of string, pop remainder of stack: "a b + 8 + c d / -"


John Bode
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Rip DVD and Convert Video on Mac OS dave345 Media 12 07-07-2008 09:32 AM
Need help on Modelsim VHDL syntax? ASAP:) kaji General Help Related Topics 0 03-14-2007 10:43 PM
Need help on a Modelsim VHDL Syntax? ASAP:) kaji Software 0 03-14-2007 10:43 PM
Need Help on a Modelsim VHDL Syntax....ASAP:) kaji Hardware 0 03-14-2007 10:41 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46