Guest User

Untitled

a guest
Jun 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. // /*-------------------------------------------------------------------*\
  2. // | Concrete Template : Program_Parse_1
  3. // \*-------------------------------------------------------------------*/
  4.  
  5. #ifndef CT_PROGRAM_PARSE_1
  6. #define CT_PROGRAM_PARSE_1 1
  7.  
  8. ///------------------------------------------------------------------------
  9. /// Global Context --------------------------------------------------------
  10. ///------------------------------------------------------------------------
  11.  
  12. #include "AT/Program/Parse.h"
  13. #include "CI/BL_Tokenizing_Machine/1.h"
  14. #include "CT/BL_Tokenizing_Machine/Get_1.h"
  15.  
  16. ///------------------------------------------------------------------------
  17. /// Interface -------------------------------------------------------------
  18. ///------------------------------------------------------------------------
  19.  
  20. concrete_template <
  21. concrete_instance class Statement,
  22. /*!
  23. implements
  24. abstract_instance Statement_Kernel <Statement>,
  25. implements
  26. abstract_instance Statement_Parse <
  27. Statement,
  28. Tokenizing_Machine
  29. >
  30. !*/
  31. concrete_instance class Program_Base,
  32. /*!
  33. implements
  34. abstract_instance Program_Kernel <Statement>
  35. !*/
  36. concrete_instance class Tokenizing_Machine =
  37. BL_Tokenizing_Machine_Get_1 <
  38. BL_Tokenizing_Machine_1
  39. >
  40. /*!
  41. implements
  42. abstract_instance BL_Tokenizing_Machine_Kernel,
  43. implements
  44. abstract_instance BL_Tokenizing_Machine_Get
  45. !*/
  46. >
  47. class Program_Parse_1 :
  48. implements
  49. concrete_instance Program_Parse <Statement>,
  50. extends
  51. concrete_instance Program_Base
  52. {
  53. private:
  54.  
  55. local_procedure_body Parse_Instruction_Definition (
  56. alters Character_IStream& str,
  57. alters Tokenizing_Machine& m,
  58. alters Text& token_text,
  59. alters Integer& token_kind,
  60. produces Text& name,
  61. produces Statement& body
  62. )
  63. /*!
  64. requires
  65. str.is_open = true and
  66. token_text = "INSTRUCTION" and
  67. token_kind = KEYWORD and
  68. m.ready_to_dispense = false
  69. ensures
  70. if there exists x, y: string of character,
  71. i: IDENTIFIER, s: STATEMENT
  72. (#token_text * #m.buffer * #str.content = x * y and
  73. INSTRUCTION_TO_STRING_OF_TOKENS (i, s) =
  74. REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (x)))
  75. then
  76. str.is_open = true and
  77. str.ext_name = #str.ext_name and
  78. there exists z: string of character
  79. (#token_text * #m.buffer * #str.content =
  80. z * token_text * m.buffer * str.content and
  81. INSTRUCTION_TO_STRING_OF_TOKENS (name, body) =
  82. REMOVE_SEPARATORS (TOKENIZE_PROGRAM_TEXT (z)) and
  83. token_kind = WHICH_TOKEN (token_text) and
  84. m.ready_to_dispense = false)
  85. !*/
  86. {
  87. // Students to fill this in
  88. object Statement_Parse_1 temp;
  89.  
  90. assert((token_text == "INSTRUCTION"), "Expecting INSTRUCTION");
  91.  
  92. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  93. name = token_text;
  94.  
  95. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  96. assert((token_text == "IS"), "Expecting IS");
  97.  
  98. while(count < self.Length_Of_Block())
  99. {
  100. self.Add_To_Block(count, temp);
  101. count++;
  102. }
  103. temp.Parse(str);
  104. Parse_Block(str, m, token_text, token_kind);
  105.  
  106. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  107.  
  108. }
  109.  
  110. public:
  111.  
  112. procedure_body Parse (
  113. alters Character_IStream& str
  114. )
  115. {
  116. // Students to fill this in
  117.  
  118. object Tokenizing_Machine m;
  119. object Text token_text, input, name, p_name;
  120. object Integer token_kind;
  121. object Character ch;
  122. object Statement body;
  123.  
  124. while (not str.At_EOS())
  125. {
  126. str << input;
  127. while (input.Length() > 0)
  128. {
  129. input.Remove(0, ch);
  130. m.Insert(ch);
  131. }
  132. }
  133.  
  134. //get PROGRAM
  135. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  136. assert ((token_text == "PROGRAM"), "Expecting PROGRAM");
  137.  
  138. //get name
  139. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  140. token_text = p_name;
  141.  
  142. // get IS
  143. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  144. assert((token_text == "IS"), "Expecting IS");
  145.  
  146. //get CONTEXT
  147. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  148. // assert((token_text == "{"), "Expecting {");
  149. while(token_text == "INSTRUCTION")
  150. {
  151. Parse_Instruction_Definition(str, m, token_text, token_kind, name, body);
  152. }
  153. // m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  154. // assert((token_text == "}"), "Expecting }");
  155.  
  156. //get BEGIN
  157. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  158. assert((token_text == "BEGIN"), "Expecting BEGIN");
  159.  
  160. //get BLOCK
  161. Parse_Block(str, m, token_text, token_kind);
  162.  
  163. //get END
  164. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  165. assert((token_text == "END"), "Expecting END");
  166.  
  167. //get name at end of file and check against name at top
  168. m.Get_Next_Non_Seperator_Token(str, token_text, token_kind);
  169. assert((token_text == p_name), "Invalid program identifier");
  170. }
Add Comment
Please, Sign In to add comment