Question: Write in c programming. Increment output string index Put space in output string Increment output string index If stack is not empty, pop left parenthesis

Write in c programming.
Increment output string index
Put space in output string
Increment output string index
If stack is not empty, pop left parenthesis off stop of stack but do not put in outDijkstra's Shunting-Yard Algorithm:
NOTE: Modified to include only numbers, operators, and parentheses.
While there are tokens to be read:
Read a token.
If the token is a number, then add it to the output queue.
If the token is an operator, 01, then:
while there is an operator token, 02, at the top of the stack, and 01 is left-associative and its precedence is less than or equal to that of 02.
NOTE: Do we need to be concerned about associativity? See note below.
pop 02 off the stack, onto the output queue;
push ol onto the stack.
If the token is a left parenthesis, then push it onto the stack.
o. If the token is a right parenthesis:
While the token at the top of the stack is not a left parenthesis
pop operator off the stack onto the output queue.
Pop the left parenthesis from the stack, but not onto the output queue.
Note: If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
When there are no more tokens to read: (ie end of input string)
While there are still operator tokens in the stack:
Pop the operator onto the output queue.
Note: If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.
Note:
Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence.
Consider the expression 7-4+2. The result could be either:
(7-4)+2=5 when + and - are left-associative
or
7-(4+2)=1 when + and - are right-associative.
Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.
Detailed example
Input: 3+4**21-5
\table[[Token,Action,Output (in RPN),Operator Stack,Notes],[3,\table[[Add token to],[output]],3,,],[+,Push token to stack,3,+,],[4,\table[[Add token to],[output]],34,+,],[*,Push token to stack,34,+,\table[[* has higher],[precedence than +]]],[2,\table[[Add token to],[output]],342,**+,],[1,Pop stack to output,342**,+,\table[[land * have same],[precedence]]],[Push token to stack,342**,1+,\table[[/ has higher],[precedence than +]]],[1,Push token to stack,342**,,],[1,\table[[Add token to],[output]],342**1,1+,],[-,Push token to stack,342**1,,],[5,\table[[Add token to],[output]],342**45,,],[),Pop stack to output,342**15-,,\table[[Repeated until "("],[found]]],[Pop stack,342**15-,1+,\table[[Discard matching],[parenthesis]]],[end,\table[[Pop entire stack to],[output]],342**15-1+,,]]
State 0:
Increment index string index
If current input is number, next state is 1
If current input is operator, next state is 2
If current input is left parenthesis, next state is 3
If current input is right parenthesis, next state is 4
If current input is end of string, next state is 5
If current input is garbage, next state is 6
State 1: (found number)
Copy current character to output string
Increment output string index
Increment input string index
While character in input string is digit or decimal point
Copy character to output string
Increment output string index
Increment input string index
Put space in output string
Increment output string index
Decrement input string index
Next state is 0
State 2: (found operator)
While the stack is not empty and the charqcter at top of stack is an operator and the precedence of the operator that is current character in input string is less than or equal to that of the operator on top of the stack
Pop operator off top of 'stack and put in output string
Increment output string index
0 Put space in output string
Increment output string index
If stack is not full, Push the operator that is current character in input string on stack Otherwise, print error and exit
Next state is 0
State 3: (found left parenthesis)
If stack is not full, push left parenthesis found in current input character on stack Otherwise, display error message and exit
Next state is 0
State 4: (found right parenthesis)
While stack is not empty and item at top of stack is not left parenthesis
Pop operator off top of stack and put in output string
Write in c programming. Increment output string

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!