Guest User

Untitled

a guest
May 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. COMPILER Taste
  2.  
  3. const int // types
  4. undef = 0, integer = 1, boolean = 2, str = 3;
  5.  
  6. const int // object kinds
  7. var = 0, proc = 1, cons = 3;
  8.  
  9. public SymbolTable tab;
  10. public CodeGenerator gen;
  11.  
  12. /*--------------------------------------------------------------------------*/
  13. CHARACTERS
  14. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  15. digit = "0123456789".
  16. cr = '\r'.
  17. lf = '\n'.
  18. tab = '\t'.
  19. quote = "\"".
  20. squote = '\''.
  21. nonletters = "!£$%^&*()_+{}@@~?><|[];'#/., ".
  22.  
  23. TOKENS
  24. ident = letter {letter | digit}.
  25. number = digit {digit}.
  26. str = quote {letter | nonletters | digit} quote.
  27.  
  28.  
  29. COMMENTS FROM "/*" TO "*/" NESTED
  30. COMMENTS FROM "//" TO lf
  31.  
  32. IGNORE cr + lf + tab
  33.  
  34.  
  35.  
  36. PRODUCTIONS
  37. /*------------------------------------------------------------------------*/
  38. AddOp<out Op op>
  39. = (. op = Op.ADD; .)
  40. ( '+'
  41. | '-' (. op = Op.SUB; .)
  42. ).
  43. /*------------------------------------------------------------------------*/
  44. Expr<out int type> (. int type1; Op op; .)
  45. = SimExpr<out type>
  46. [ RelOp<out op>
  47. SimExpr<out type1> (. if (type != type1) SemErr("incompatible types");
  48. gen.Emit(op); type = boolean; .)
  49. ].
  50. /*------------------------------------------------------------------------*/
  51. Factor<out int type> (. int n; Obj obj; string name; .)
  52. = (. type = undef; .)
  53. ( Ident<out name> (. obj = tab.Find(name); type = obj.type;
  54. if (obj.kind == var) {
  55. if (obj.level == 0) gen.Emit(Op.LOADG, obj.adr);
  56. else gen.Emit(Op.LOAD, obj.adr);
  57. } else SemErr("variable expected"); .)
  58. | str (. gen.sEmit(Op.SWRITE, t.val); type = str; .)
  59. | number (. n = Convert.ToInt32(t.val);
  60. gen.Emit(Op.CONST, n); type = integer; .)
  61. | '-'
  62. Factor<out type> (. if (type != integer) {
  63. SemErr("integer type expected"); type = integer;
  64. }
  65. gen.Emit(Op.NEG); .)
  66. | "true" (. gen.Emit(Op.CONST, 1); type = boolean; .)
  67. | "false" (. gen.Emit(Op.CONST, 0); type = boolean; .)
  68. ).
  69. /*------------------------------------------------------------------------*/
  70. Ident<out string name>
  71. = ident (. name = t.val; .).
  72. /*------------------------------------------------------------------------*/
  73. MulOp<out Op op>
  74. = (. op = Op.MUL; .)
  75. ( '*'
  76. | '/' (. op = Op.DIV; .)
  77. ).
  78. /*------------------------------------------------------------------------*/
  79. ProcDecl (. string name; Obj obj; int adr; .)
  80. = "void"
  81. Ident<out name> (. obj = tab.NewObj(name, proc, undef); obj.adr = gen.pc;
  82. if (name == "Main") gen.progStart = gen.pc;
  83. tab.OpenScope(); .)
  84. '(' ')'
  85. '{' (. gen.Emit(Op.ENTER, 0); adr = gen.pc - 2; .)
  86. { VarDecl | ConstDecl| Stat }
  87. '}' (. gen.Emit(Op.LEAVE); gen.Emit(Op.RET);
  88. gen.Patch(adr, tab.topScope.nextAdr);
  89. tab.CloseScope(); .).
  90. /*------------------------------------------------------------------------*/
  91. RelOp<out Op op>
  92. = (. op = Op.EQU; .)
  93. ( "=="
  94. | "!=" (. op = Op.NEQ; .)
  95. | '<' (. op = Op.LSS; .)
  96. | "<=" (. op = Op.LSE; .)
  97. | '>' (. op = Op.GTR; .)
  98. | ">=" (. op = Op.GRE; .)
  99. ).
  100. /*------------------------------------------------------------------------*/
  101. SimExpr<out int type> (. int type1; Op op; .)
  102. = Term<out type>
  103. { AddOp<out op>
  104. Term<out type1> (. if (type != integer || type1 != integer)
  105. SemErr("integer type expected");
  106. gen.Emit(op); .)
  107. }.
  108. /*------------------------------------------------------------------------*/
  109. Stat (. int type; string name; Obj obj; int adr, adr2, loopstart; .)
  110. = Ident<out name> (. obj = tab.Find(name); .)
  111. ( ":=" (. if ((obj.kind != var) && (obj.kind != cons)) SemErr("cannot assign to procedure"); if (obj.kind == cons) SemErr("cannot assign to constant"); .)
  112. (Expr<out type>
  113. ( '?' (. if (type != boolean) SemErr("boolean type expexted");
  114. gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
  115. Expr<out type> (. if (type != obj.type) SemErr("incompatible types");
  116. gen.Emit(Op.FJMP, 0); adr2 = gen.pc - 2; .)
  117. ':' (. gen.Patch(adr,gen.pc); .)
  118. Expr<out type> (. if (type != obj.type) SemErr("incompatible types");
  119. gen.Patch(adr2,gen.pc); .)
  120. | (. if (type != obj.type) SemErr("incompatible types"); .)
  121. ) (. if (obj.level == 0){ gen.Emit(Op.STOG, obj.adr);}else {if(obj.type != str){gen.Emit(Op.STO, obj.adr);} else {gen.Emit(Op.STO, obj.adr);} } .)
  122. )
  123.  
  124. | '(' ')' (. if (obj.kind != proc) SemErr("object is not a procedure");
  125. gen.Emit(Op.CALL, obj.adr); .)
  126. ) ';'
  127. | "if"
  128. '(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
  129. gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
  130. Stat
  131. [ "else" (. gen.Emit(Op.JMP, 0); adr2 = gen.pc - 2;
  132. gen.Patch(adr, gen.pc);
  133. adr = adr2; .)
  134. Stat
  135. ] (. gen.Patch(adr, gen.pc); .)
  136.  
  137. | "while" (. loopstart = gen.pc; .)
  138. '(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
  139. gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
  140. Stat (. gen.Emit(Op.JMP, loopstart); gen.Patch(adr, gen.pc); .)
  141.  
  142. | "read"
  143. Ident<out name> ';' (. obj = tab.Find(name);
  144. if (obj.type != integer) SemErr("integer type expected");
  145. gen.Emit(Op.READ);
  146. if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
  147. else gen.Emit(Op.STO, obj.adr); .)
  148.  
  149. | "write"
  150. (Expr<out type> ';' (. if (type == integer) gen.Emit(Op.WRITE); .)
  151. |
  152. str ';' (. gen.sEmit(Op.SWRITE, t.val); .)
  153. )
  154. | '{' { Stat | VarDecl} '}' .
  155. /*------------------------------------------------------------------------*/
  156. Taste (. string name; .)
  157. = "program"
  158. Ident<out name> (. tab.OpenScope(); .)
  159. '{'
  160. { VarDecl | ProcDecl | ConstDecl}
  161. '}' (. tab.CloseScope();
  162. if (gen.progStart == -1) SemErr("main function never defined");
  163. .).
  164.  
  165. /*------------------------------------------------------------------------*/
  166. Term<out int type> (. int type1; Op op; .)
  167. = Factor<out type>
  168. { MulOp<out op>
  169. Factor<out type1> (. if (type != integer || type1 != integer)
  170. SemErr("integer type expected");
  171. gen.Emit(op); .)
  172. }.
  173. /*------------------------------------------------------------------------*/
  174. Type<out int type>
  175. = (. type = undef; .)
  176. ( "int" (. type = integer; .)
  177. | "bool" (. type = boolean; .)
  178. | "string" (. type = str; .)
  179. | "const" (. type = integer; .)
  180. ).
  181. /*------------------------------------------------------------------------*/
  182. VarDecl (. string name; int type; .)
  183. = Type<out type>
  184. Ident<out name> (. tab.NewObj(name, var, type); .)
  185. { ',' Ident<out name> (. tab.NewObj(name, var, type); .)
  186. } ';'.
  187.  
  188. /*-------------------------*/
  189. ConstDecl (. string name; int type; int n; .)
  190. = "const" (. type = 1; .)
  191. Ident<out name> (. tab.NewObj(name, cons, type); .)
  192. "="
  193. number (. n = Convert.ToInt32(t.val); gen.Emit(Op.CONST, n); type = integer; .)
  194. ';'.
  195.  
  196. END Taste.
Add Comment
Please, Sign In to add comment