Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
- with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
- procedure parser is
- type TokenType is (TokenTypeNone,TokenTypeInteger,TokenTypeOperator,TokenTypeError,TokenTypeEnd);
- type TokenOperator is (TokenOperatorAdd,TokenOperatorSub,TokenOperatorMul,TokenOperatorDiv);
- Input : Unbounded_String:=To_Unbounded_String("10+4");
- InputPos : Integer:=1;
- TokenInteger : Integer;
- TokenOp : TokenOperator;
- CalcResult : Integer;
- function GetToken return TokenType is
- Char : Character;
- CurrType : TokenType:=TokenTypeNone;
- begin
- while InputPos<=Length(Input) loop
- Char:=Element(Input,InputPos);
- Put("Input Pos:");
- Put(InputPos);
- Put("::");
- Put(Char);
- New_Line;
- case Char is
- when ' ' =>
- if CurrType/=TokenTypeNone then
- exit;
- end if;
- when '0'..'9' =>
- case CurrType is
- when TokenTypeNone => CurrType := TokenTypeInteger;
- TokenInteger := Character'Pos(Char)-Character'Pos('0');
- when TokenTypeInteger => TokenInteger := TokenInteger*10+ (Character'Pos(Char)-Character'Pos('0'));
- when others => null;
- end case;
- when '+' =>
- if CurrType=TokenTypeNone then
- TokenOp := TokenOperatorAdd;
- CurrType := TokenTypeOperator;
- InputPos := InputPos+1;
- end if;
- exit;
- when others =>
- CurrType:=TokenTypeError;
- end case;
- InputPos := InputPos+1;
- end loop;
- if CurrType=TokenTypeNone then
- CurrType:=TokenTypeEnd;
- end if;
- return CurrType;
- end;
- function Process2 return Integer is
- LeftValue : Integer;
- begin
- return 0;
- end;
- function Process return Integer is
- LeftValue : Integer;
- RightValue : Integer;
- Op : TokenOperator;
- begin
- LeftValue:=Process2;
- while GetToken=TokenTypeOperator loop
- Op := TokenOp;
- if Op not in (TokenOperatorAdd,TokenOperatorSub) then
- return LeftValue;
- end if;
- RightValue := Process2;
- end loop;
- return LeftValue;
- end;
- begin
- Put("Input String:");
- Put(Input);
- New_Line;
- CalcResult:=Process;
- Put("Result");
- Put(CalcResult);
- end;
Add Comment
Please, Sign In to add comment