EX2.RIG 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. -- Example of RIGAL program.
  2. -- Takes input from file EX.TXT
  3. -- Performs simple arithmetical expression parsing.
  4. -- Expression tree is returned.
  5. -- Polish Postfix Form is printed out.
  6. #MAIN
  7. OPEN S ' '; -- Opens file S for output to the screen
  8. $E:=#CALL_PAS(3 'EX.TXT'); -- Scanner takes input from file EX.TXT
  9. S<<$E; -- Prints the input text
  10. $T:=#ANALYSE($E); -- Calls expression analysis
  11. PRINT $T; -- Prints expression tree
  12. #CALL_PAS(13 $T); -- Prints expression tree in graphical form
  13. S<<;
  14. #PRINT_POLISH($T); -- Calls Polish Postfix Form printing
  15. ##
  16. #ANALYSE -- Traverses token list
  17. (. $E:=#EXPRESSION .) / RETURN $E /
  18. ##
  19. -- BNF form :
  20. #EXPRESSION -- expression ::=
  21. $A1:=#ADDITIVE_EL -- add_el
  22. (* $OP := ( '+' ! '-' ) $A2:=#ADDITIVE_EL -- ( ('+'!'-') add_el )*
  23. / $A1 := <. OP:$OP, ARG1:$A1 , ARG2:$A2 .> / *)
  24. / RETURN $A1 /
  25. ##
  26. #ADDITIVE_EL -- add_el ::=
  27. $A1:=#TERM -- term
  28. (* $OP := ( '*' ! '/' ) $A2 :=#TERM -- ( ('*'!'/') term )*
  29. / $A1 := <. OP:$OP, ARG1:$A1 , ARG2:$A2 .> / *)
  30. / RETURN $A1 /
  31. ##
  32. #TERM -- term ::=
  33. $A := ( $ID ! $NUM ) / RETURN $A / ;; -- (identifier ! number !
  34. '(' $A:=#EXPRESSION ')' / RETURN $A / -- '(' expression ')' )
  35. ##
  36. #PRINT_POLISH
  37. <. ARG1 : #PRINT_POLISH, -- Trees are traversed recursively
  38. ARG2 : #PRINT_POLISH,
  39. OP : $OP .>
  40. / S<] $OP / ;;
  41. $EL / S<] $EL / -- Leaves are printed immediately
  42. ##