T
Tameem
i have a string as (a+b)+8-(c/d) in Infix form.
how can i convert it to postfix form using C language,,,????
how can i convert it to postfix form using C language,,,????
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 said: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 have a string as (a+b)+8-(c/d) in Infix form.
how can i convert it to postfix form using C language,,,????
spinoza1111 said: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.
I will assume that that string is not the ONLY string you have to
convert. If it is, then the answer isint 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 will assume that that string is not the ONLY string you have to
convert. If it is, then the answer isint 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
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#>
spinoza1111 said:spinoza1111<[email protected]> writes: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.
spinoza1111 said:spinoza1111<[email protected]> writes: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.
[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.
i have a string as (a+b)+8-(c/d) in Infix form.
how can i convert it to postfix form using C language,,,????
spinoza1111 said: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
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.
spinoza1111 said:<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.
No, and I never said it was. You know where to post if you really
don't know what is wrong with it. I don't want to be drawn further
into an off topic discussion of grammars here. Only in a suitable
group will you get the very best advice; and only there would any
criticism be properly checked. Send me an email (drop the ".usenet"
to get through faster) if you really can't bear to post in a suitable
group, but then neither of us will benefit from third party scrutiny.
Alternatively, feel free to have the last word by calling me
"unqualified" or a "******" again. I grew up gay being called a lot
worse than "unqualified".
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.
I will assume that that string is not the ONLY string you have to
convert. If it is, then the answer isint 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 balanceWrite 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 codeUpper case words are lexemes recognised by C codeQuoted 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.
Ben Bacarisse noticed that I used the wrong grammar and informed me by
email in a very professional way. The grammar should be the following
for direct conversion to code!!
expression → additionFactor [ "+"|"-" additionFactor ] *
additionFactor → multiplicationFactor [ "*"|"/" multiplicationFactor ]
*
multiplicationFactor → LETTER | NUMBER | "(" expression ")"
That is, an expression is a series of one or more additionFactors.
When there are at least two, they are separated by plus or minus. The
replacement of the right recursive call to expression by the iteration
over additionFactor (the asterisk expressing iteration) causes the
addition/subtraction to left associate naturally. Likewise for the
call to expression in the second production.
I also made this same error when developing the code for Build Your
Own .Net Language and Compiler but did not have Ben Bacarisse's expert
assistance. Instead, I discovered it in testing the code for the first
toy compiler in ch 3 and mention the problem, and its solution, in my
book. I was too lazy when posting to check my own goddamn book and
shall try to be more diligent in the future. However, such diligence
shall be pearls before swine in some cases and in others the exception
that proves the rule.
When I make an error I make each error twice, it seems to give the old
brain extra time to relearn things it learned in the past; in the past
week I have made the comma-as-operator-versus-separator as well as
this error twice. I do regret if the original post was useless to the
original poster, because as Ben pointed out, it right-associative.
But (big but): my post was of far greater quality, errors and all,
that the uncollegial crap which constitutes 99% of the postings here.
As was Ben's correction. This dialogue is what this facility should be
all the time, not denials that something is true unaccompanied by
information as to what is, and the politics of personal destruction.
Because of this, no response to this thread by Richard Heathfield will
be either read by me nor responded to by me.
Ben: thanks for your assistance and the very professional manner in
which you made it. If there are other errors please let me know.
Likewise for all other readers, except Richard Heathfield. I do not
think he's qualified to discuss this issue.- Hide quoted text -
- Show quoted text -
In
spinoza1111wrote:
The microsoft.public.dotnet.languages.csharp newsgroup exists for C#
discussions; posting your code there will give you the best chance of
getting informed reviews of that code.
spinoza1111 said:I published it here to show the OP that the grammar based approach
works, and give him "pseudo code" for his C assignment, without doing
his homework for him.
Since by now he's probably handed in his assignment, I challenge you
to translate the C Sharp code to C.
That would not be useful. Your code does not parse the language
defined by the grammar, and what it does do, it does in a rather
roundabout way.
In
spinoza1111wrote:
If the OP is wise, he will treat any of your code, in whatever
language, with a great deal of suspicion.
I see no value in reinventing that particular wheel. I already have
working recursive descent code. What would be the point in
translating yours? If it works (which I doubt), I gain nothing. If it
Did you write it or did you steal it?
I need to see you actually write new C code.
You have a big mouth when
it comes to putting down people but the only time when you put your
big mouth on the line (the Spark Notes test) you failed. I am giving
you another chance.
I wrote and debugged a recursive descent parser for my book. I wrote
it again here after correcting the grammar
It also demonstrates that the material is sufficiently complex for
smart people to make stupid mistakes (Knuth has himself said he
consistently gets binary search wrong the first time), and that the
stupid people aren't the ones making the mistakes.
It's the people too ready to trash and if possible render unemployable
their fellow human beings (by creating a record here) while explaining
away their own mistakes as you explained away your failure to get a
decent grade on the Spark Notes test.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.