How to resolve the algorithm Parsing/RPN to infix conversion step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Parsing/RPN to infix conversion step by step in the J programming language

Table of Contents

Problem Statement

Create a program that takes an RPN representation of an expression formatted as a space separated sequence of tokens and generates the equivalent expression in infix notation.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Parsing/RPN to infix conversion step by step in the J programming language

Source code in the j programming language

tokenize=: ' ' <;._1@, deb

ops=: ;:'+ - * / ^'
doOp=: plus`minus`times`divide`exponent`push@.(ops&i.)
parse=:3 :0
  stack=: i.0 2
  for_token.tokenize y do.doOp token end.
  1{:: ,stack
)

parens=:4 :0
  if. y do. '( ',x,' )' else. x end.
)

NB. m: precedence, n: is right associative, y: token
op=:2 :0
  L=. m -   n
  R=. m - -.n
  smoutput;'operation: ';y
  'Lprec left Rprec right'=. ,_2{.stack
  expr=. ;(left parens L > Lprec);' ';y,' ';right parens R > Rprec
  stack=: (_2}.stack),m;expr
  smoutput stack
)

plus=:     2 op 0
minus=:    2 op 0
times=:    3 op 0
divide=:   3 op 0
exponent=: 4 op 1

push=:3 :0
  smoutput;'pushing: ';y
  stack=: stack,_;y
  smoutput stack
)


   parse '3 4 2 * 1 5 - 2 3 ^ ^ / +'
pushing: 3
+-+-+
|_|3|
+-+-+
pushing: 4
+-+-+
|_|3|
+-+-+
|_|4|
+-+-+
pushing: 2
+-+-+
|_|3|
+-+-+
|_|4|
+-+-+
|_|2|
+-+-+
operation: *
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
pushing: 1
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|_|1    |
+-+-----+
pushing: 5
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|_|1    |
+-+-----+
|_|5    |
+-+-----+
operation: -
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|2|1 - 5|
+-+-----+
pushing: 2
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|2|1 - 5|
+-+-----+
|_|2    |
+-+-----+
pushing: 3
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|2|1 - 5|
+-+-----+
|_|2    |
+-+-----+
|_|3    |
+-+-----+
operation: ^
+-+-----+
|_|3    |
+-+-----+
|3|4 * 2|
+-+-----+
|2|1 - 5|
+-+-----+
|4|2 ^ 3|
+-+-----+
operation: ^
+-+-----------------+
|_|3                |
+-+-----------------+
|3|4 * 2            |
+-+-----------------+
|4|( 1 - 5 ) ^ 2 ^ 3|
+-+-----------------+
operation: /
+-+-------------------------+
|_|3                        |
+-+-------------------------+
|3|4 * 2 / ( 1 - 5 ) ^ 2 ^ 3|
+-+-------------------------+
operation: +
+-+-----------------------------+
|2|3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3|
+-+-----------------------------+
3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3


  

You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Fortran programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Quackery programming language
You may also check:How to resolve the algorithm Sylvester's sequence step by step in the Swift programming language
You may also check:How to resolve the algorithm Digital root step by step in the zkl programming language