Advertisement
kaenan

Polish postfix pseudo

Nov 12th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. Constanta Caracter SUCCESS <- 0
  2.  
  3. Constanta Caracter NATURAL_NUMBER <- 'N'
  4. Constanta Caracter LETTER <- 'L'
  5.  
  6.  
  7. Polish_expr
  8.         Sir expr
  9.         Intreg value
  10.  
  11. Data
  12.         Caracter type
  13.         Intreg value
  14.  
  15.  
  16. subprogram calculator(Sir string)
  17.  
  18.     /* The polish expr. */
  19.     Polish_expr  result
  20.  
  21.     Stack  s
  22.  
  23.     Sir  token, expr
  24.  
  25.         /* Used for storing parsed token. */
  26.     Data  input
  27.  
  28.     /* Statistic used for validation of *expr*
  29.      * and error reporting.
  30.      */
  31.     Caracter  last_operation
  32.         Intreg  err
  33.  
  34.         last_operation <- '\0'
  35.         err <-0
  36.  
  37.  
  38.         Intreg  index, j
  39.         index <- 0
  40.  
  41.     /* While there's more of the string to process. */
  42.     cat timp  string[index] AND !err
  43.  
  44.  
  45.             /* Skip white space. */
  46.             cat timp  string[index] AND strchr(" \t\n", string[index])
  47.                     index++
  48.             sf_cattimp
  49.  
  50.  
  51.             /* Get token. */
  52.             j <- 0
  53.  
  54.             cat timp  string[index] AND !strchr(" \t\n", string[index])
  55.                 token[j++] <- string[index++]
  56.             sf_cattimp
  57.             /* GOT token! */
  58.  
  59.  
  60.             /* Get data from token. */
  61.             input <- parse_token(token)
  62.  
  63.  
  64.             /* If it's a natural number, push it on the stack. */
  65.             daca  input.type = NATURAL_NUMBER
  66.                 push(st, input.value)
  67.  
  68.  
  69.                 /* Forming of expr. */
  70.                 strncat(expr, token, strlen(token))
  71.                 strncat(expr, " ", 1)
  72.             sf_daca
  73.  
  74.             altfel daca  input.type = LETTER
  75.                 /* Operate */
  76.  
  77.                 daca input.value = '+'
  78.                     daca  SUCCESS != stackOperate(st, add, input.value) // add(Intreg a, b) returneaza a + b;
  79.                         err++
  80.                     sf_daca
  81.  
  82.                     last_operation <- input.value
  83.  
  84.  
  85.                     /* Forming of expr. */
  86.                     strncat(expr, token, strlen(token))
  87.                     strncat(expr, " ", 1)
  88.  
  89.  
  90.                 altfel daca  input.value = '-'
  91.                     daca  SUCCESS != stackOperate(st, substract, input.value) // substract(Intreg a, b) returneaza a - b;
  92.                         err++
  93.                     sf_daca
  94.  
  95.                     last_operation <- input.value
  96.  
  97.  
  98.                     /* Forming of expr. */
  99.                     strncat(expr, token, strlen(token))
  100.                     strncat(expr, " ", 1)
  101.                 sf_altfeldaca
  102.  
  103.  
  104.                 altfel daca  input.value = '*'
  105.                     daca  SUCCESS != stackOperate(st, multiply, input.value) // multiply(Intreg a, b) returneaza a * b;
  106.                         err++
  107.                     sf_daca
  108.  
  109.                     last_operation <- input.value
  110.  
  111.  
  112.                     /* Forming of expr. */
  113.                     strncat(expr, token, strlen(token))
  114.                     strncat(expr, " ", 1)
  115.                 sf_altfeldaca
  116.  
  117.  
  118.  
  119.                 altfel daca  input.value = '/'
  120.                     daca  SUCCESS != stackOperate(st, divide, input.value) // divide(Intreg a, b)
  121.                         err++                                              //     daca  a = 0
  122.                     sf_daca                                                //        *afisare mesaj de eroare
  123.                                                                            //        *iesire din program
  124.                     last_operation <- input.value                          //     returneaza a / b
  125.  
  126.  
  127.                     /* Forming of expr. */
  128.                     strncat(expr, token, strlen(token))
  129.                     strncat(expr, " ", 1)
  130.                 sf_altfeldaca
  131.  
  132.  
  133.                 altfel daca  input.value = '%'                           // mod(Intreg a, b)
  134.                     daca  SUCCESS != stackOperate(st, mod, input.value)  //     daca  a = 0
  135.                         err++                                            //        *afisare mesaj de eroadre
  136.                     sf_daca                                              //        *iesire din program
  137.                                                                          //     returneaza a % b
  138.                     last_operation <- input.value
  139.  
  140.  
  141.                     /* Forming of expr. */
  142.                     strncat(expr, token, strlen(token))
  143.                     strncat(expr, " ", 1)
  144.  
  145.                 sf_altfeldaca
  146.  
  147.                 altfel
  148.                     Afiseaza [Error] Undefinded operator: token.
  149.  
  150.                     err++
  151.                 sf_altfel
  152.                 sf_daca
  153.             sf_altfeldaca
  154.  
  155.             altfel daca  token[0]
  156.                 /* If token is !natural_number, !letter and !empty : bad input! */
  157.  
  158.                 Afiseaza [Error] Bad input: token.
  159.                 err++
  160.             sf_altfeldaca
  161.             sf_daca
  162.         sf_cattimp
  163.  
  164.  
  165.     /* If there were operands, but no operation(s). */
  166.     daca  !err AND !last_operation AND !isEmpty(st)
  167.             Afiseaza [Error] Please provide an operation.
  168.             err++
  169.         sf_daca
  170.  
  171.  
  172.     /* If too many operands were given. */
  173.     daca  !err AND last_operation AND heightOf(st) > 1
  174.             err_op_arity(last_operation)
  175.             err++
  176.         sf_daca
  177.  
  178.  
  179.     /* If err OR if input was null, show USAGE message. */
  180.         daca  err OR !last_operation
  181.             Afiseaza Usage: a b [+-*/] | long a, b.
  182.  
  183.             return { NULL, 0 }
  184.         sf_daca
  185.  
  186.  
  187.     /* Remove trailing [space]. */
  188.     expr[strlen(expr)] <- '\0'
  189.  
  190.     result.expr = expr
  191.         result.value =  pop(st)
  192.  
  193.     return result
  194. }
  195.  
  196.  
  197. subprogram parse_token(Sir token)
  198.  
  199.     data d
  200.  
  201.     long long value <- 0
  202.     char type <- '0'
  203.  
  204.  
  205.     /* Convert token to long. */
  206.         value <- strtol_(token) // strtol_ conversteste Sir in Intreg, verificand errori de input
  207.                                 // si ignorand spatii(si 0-uri) nesemnificative. Returneaza -1 in caz de eroare.
  208.  
  209.     daca value >= 0
  210.         type <- NATURAL_NUMBER
  211.     sf_daca
  212.  
  213.     /* We're dealing with non-numbers/negative numbers then. */
  214.     altfel daca strlen(token) = 1 {
  215.         type <- LETTER
  216.         value <- token[0
  217.     }
  218.  
  219.     /* Else not a number, nor a single letter. */
  220.  
  221.     d.value <- value
  222.     d.type <- type
  223.  
  224.     return d
  225.  
  226. #sf_subprogram
  227.  
  228.  
  229. subprogram stackOperate(Stack *s, Intreg operator, Caracter symbol) // Intreg operator este functie.
  230.  
  231.     daca heightOf(s) < 2
  232.         err_op_arity(symbol)
  233.  
  234.         retureaza -1
  235.     sf_daca
  236.  
  237.     Intreg termL, termR, result
  238.  
  239.     termR <- pop(s)
  240.     termL <- pop(s)
  241.  
  242.     daca  (symbol = '/' OR symbol = '%') AND termR = 0
  243.         afiseaza [Error] Ilegal operation; trying to symbol by 0: "termL termR symbol"
  244.  
  245.         returneaza DOMAIN_ERR
  246.     sf_daca
  247.  
  248.     result <- operator(termL, termR)
  249.     push(s, result)
  250.  
  251.     returneaza SUCCESS
  252.  
  253. #sf_subprogram
  254.  
  255.  
  256. procedura err_op_arity(char op)
  257.  
  258.     afiseaza [Error] The "op" operator takes two operands: "a b op" | long a, b.
  259.  
  260. #sf_procedura
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement