Guest User

Untitled

a guest
May 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. with Ada.Text_IO; use Ada.Text_IO;
  2. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  3. with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
  4. with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
  5.  
  6. procedure parser is
  7. type TokenType is (TokenTypeNone,TokenTypeInteger,TokenTypeOperator,TokenTypeError,TokenTypeEnd);
  8. type TokenOperator is (TokenOperatorAdd,TokenOperatorSub,TokenOperatorMul,TokenOperatorDiv);
  9. Input : Unbounded_String:=To_Unbounded_String("10+4");
  10. InputPos : Integer:=1;
  11. TokenInteger : Integer;
  12. TokenOp : TokenOperator;
  13. CalcResult : Integer;
  14.  
  15. function GetToken return TokenType is
  16.  
  17. Char : Character;
  18. CurrType : TokenType:=TokenTypeNone;
  19.  
  20. begin
  21.  
  22. while InputPos<=Length(Input) loop
  23. Char:=Element(Input,InputPos);
  24. Put("Input Pos:");
  25. Put(InputPos);
  26. Put("::");
  27. Put(Char);
  28. New_Line;
  29.  
  30. case Char is
  31. when ' ' =>
  32. if CurrType/=TokenTypeNone then
  33. exit;
  34. end if;
  35. when '0'..'9' =>
  36. case CurrType is
  37. when TokenTypeNone => CurrType := TokenTypeInteger;
  38. TokenInteger := Character'Pos(Char)-Character'Pos('0');
  39. when TokenTypeInteger => TokenInteger := TokenInteger*10+ (Character'Pos(Char)-Character'Pos('0'));
  40. when others => null;
  41. end case;
  42. when '+' =>
  43. if CurrType=TokenTypeNone then
  44. TokenOp := TokenOperatorAdd;
  45. CurrType := TokenTypeOperator;
  46. InputPos := InputPos+1;
  47. end if;
  48. exit;
  49. when others =>
  50. CurrType:=TokenTypeError;
  51. end case;
  52.  
  53. InputPos := InputPos+1;
  54.  
  55. end loop;
  56. if CurrType=TokenTypeNone then
  57. CurrType:=TokenTypeEnd;
  58. end if;
  59. return CurrType;
  60. end;
  61.  
  62. function Process2 return Integer is
  63. LeftValue : Integer;
  64. begin
  65. return 0;
  66. end;
  67.  
  68. function Process return Integer is
  69. LeftValue : Integer;
  70. RightValue : Integer;
  71. Op : TokenOperator;
  72. begin
  73. LeftValue:=Process2;
  74. while GetToken=TokenTypeOperator loop
  75. Op := TokenOp;
  76. if Op not in (TokenOperatorAdd,TokenOperatorSub) then
  77. return LeftValue;
  78. end if;
  79. RightValue := Process2;
  80. end loop;
  81. return LeftValue;
  82. end;
  83.  
  84. begin
  85. Put("Input String:");
  86. Put(Input);
  87. New_Line;
  88. CalcResult:=Process;
  89. Put("Result");
  90. Put(CalcResult);
  91. end;
Add Comment
Please, Sign In to add comment