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 ShuntingYard 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, then:
while there is an operator token, at the top of the stack, and is leftassociative and its precedence is less than or equal to that of
NOTE: Do we need to be concerned about associativity? See note below.
pop 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 The result could be either:
when and are leftassociative
or
when and are rightassociative.
Usually the addition, subtraction, multiplication, and division operators are leftassociative, while the exponentiation, assignment and conditional operators are rightassociative. 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:
tableTokenAction,Output in RPNOperator Stack,NotestableAdd token tooutputPush token to stack,tableAdd token tooutputPush token to stack,table has higherprecedence than tableAdd token tooutputPop stack to output,tableland have sameprecedencePush token to stack,table has higherprecedence than Push token to stack,tableAdd token tooutputPush token to stack,tableAdd token tooutputPop stack to output,tableRepeated until foundPop stack,tableDiscard matchingparenthesisendtablePop entire stack tooutput
State :
Increment index string index
If current input is number, next state is
If current input is operator, next state is
If current input is left parenthesis, next state is
If current input is right parenthesis, next state is
If current input is end of string, next state is
If current input is garbage, next state is
State : 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
State : 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
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
State : 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
State : 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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
