Advertisement
Guest User

work

a guest
Nov 13th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. /*
  2. * ========================================================================
  3. * uA Interpreter
  4. * ========================================================================
  5. *
  6. * This file is part of ua Interpreter.
  7. *
  8. * ua Interpreter is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation, either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * ua Interpreter is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with ua Interpreter. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * (C) Copyright 2017, Gabor Kecskemeti (g.kecskemeti@ljmu.ac.uk)
  22. */
  23. package uk.ac.ljmu.fet.cs.comp.interpreter.generated;
  24. import uk.ac.ljmu.fet.cs.comp.interpreter.tokens.*;
  25.  
  26. %%
  27.  
  28. %public
  29. %class LexuA
  30. %line
  31. %column
  32. %type Expression
  33.  
  34. %{
  35. StringBuffer string=new StringBuffer();
  36. String opType=null;
  37. Operation.AttKind inspec=null;
  38. Expression left, right;
  39. boolean doLeft=true;
  40.  
  41. private void saveExpr(Expression e) {
  42. if((doLeft&&left!=null)||(!doLeft&&right!=null)) {
  43. throwError("Too complex parameter spec");
  44. }
  45. if(doLeft) {
  46. left=e;
  47. } else {
  48. right=e;
  49. }
  50. }
  51.  
  52. private void switchToInitial() {
  53. opType=null;
  54. left=right=null;
  55. inspec=null;
  56. doLeft=true;
  57. string.setLength(0);
  58. yybegin(YYINITIAL);
  59. }
  60.  
  61. private Operation genOperation() {
  62. try {
  63. Operation o=Operation.opFactory(yyline, opType, left, (Register)right, inspec);
  64. switchToInitial();
  65. return o;
  66. } catch(Exception e) {
  67. throwError("Illegal instruction specification",e);
  68. return null;
  69. }
  70. }
  71.  
  72. private void throwError(String text, Throwable e) {
  73. throw new Error(text+" at line "+ (yyline+1) + " on character "+ (yycolumn+1), e);
  74. }
  75.  
  76. private void throwError(String text) {
  77. throwError(text,null);
  78. }
  79. %}
  80.  
  81. LineTerminator = \r|\n|\r\n
  82. InputCharacter = [^\r\n]
  83. InLineWhiteSpace = [ \t\f]
  84. WhiteSpace = {LineTerminator} | {InLineWhiteSpace}
  85.  
  86. InstructionID = "LD" | "ST" | "JZ" | "JM" | "AD" | "ML" | "DV" | "MV" | "SB"
  87. InputType = [CR]
  88. Register = [ABCD]
  89. Integer = "-"?[:digit:]+
  90. Identifier = ([0-9]|[a-z])([0-9]|[A-z])*
  91. Comment = {InLineWhiteSpace}* ";" {InputCharacter}* {LineTerminator}?
  92.  
  93. %state INSTSPEC PARSPEC STRINGSPEC PRESTRINGWS STRING NRSPEC LABSPEC NRPART VARPART VARSPEC
  94.  
  95. %%
  96. <INSTSPEC> {
  97. {InputType} { yybegin(PARSPEC);
  98. inspec=Operation.AttKind.valueOf(yytext());
  99. left=right=null;
  100. doLeft=true;
  101. }
  102. [^] { throwError("Illegal input type spec"); }
  103. }
  104. <PARSPEC> {
  105. {Register} { saveExpr(new Register(yyline,yytext())); }
  106. {Integer} { saveExpr(new IntNumber(yyline,yytext())); }
  107. {Identifier} { saveExpr(new Identifier(yyline,yytext())); }
  108. "," { doLeft=false; }
  109. {InLineWhiteSpace} { }
  110. {LineTerminator} { return genOperation(); }
  111. <<EOF>> { return genOperation(); }
  112. [^] { throwError("Illegal parameter spec"); }
  113. }
  114. <STRINGSPEC> {
  115. {Identifier} { yybegin(PRESTRINGWS);
  116. string.setLength(0);
  117. saveExpr(new Identifier(yyline,yytext()));
  118. doLeft=false;
  119. }
  120. {InLineWhiteSpace} { }
  121. [^] { throwError("Illegal string identifier"); }
  122. }
  123. <PRESTRINGWS> {
  124. {InLineWhiteSpace} { }
  125. [^] { yybegin(STRING); string.append(yytext());}
  126. }
  127. <STRING> {
  128. [\n\r] { StringConstant sc = new StringConstant(yyline, left, new StringValue(yyline, string.toString()));
  129. switchToInitial();
  130. return sc;
  131. }
  132. \\t { string.append('\t'); }
  133. [^\n\r]+ { string.append( yytext() ); }
  134. }
  135. <NRSPEC> {
  136. {Identifier} { saveExpr(new Identifier(yyline,yytext())); doLeft=false; yybegin(NRPART); }
  137. {InLineWhiteSpace} { }
  138. [^] { throwError("Illegal number constant id"); }
  139. }
  140. <NRPART> {
  141. {Integer} { saveExpr(new IntNumber(yyline,yytext())); }
  142. {InLineWhiteSpace} { }
  143. {LineTerminator} { NumberConstant nc=new NumberConstant(yyline, left, right);
  144. switchToInitial();
  145. return nc;
  146. }
  147. [^] { throwError("Illegal number constant value"); }
  148. }
  149. <VARSPEC> {
  150. {Identifier} { saveExpr(new Identifier(yyline,yytext())); doLeft=false; yybegin(VARPART); }
  151. {InLineWhiteSpace} { }
  152. [^] { throwError("Illegal Variable declaration"); }
  153. }
  154. <VARPART> {
  155. {InLineWhiteSpace} { }
  156. {LineTerminator} { VariableDefinition vd=new VariableDefinition(yyline, left);
  157. switchToInitial();
  158. return vd;
  159. }
  160. [^] { throwError("Illegal Variable declaration"); }
  161. }
  162. <LABSPEC> {
  163. ":" { CodeLabel cl = new CodeLabel(yyline, (Identifier)left, right);
  164. switchToInitial();
  165. return cl;
  166. }
  167. {InLineWhiteSpace} { }
  168. [^] { throwError("Illegal label specification"); }
  169. }
  170. <YYINITIAL> {
  171. {InstructionID} { yybegin(INSTSPEC);
  172. opType=yytext();
  173. inspec=null;
  174. }
  175. "CONST" { yybegin(STRINGSPEC); }
  176. "CONNR" { yybegin(NRSPEC); }
  177. "VAR" { yybegin(VARSPEC); }
  178. {Identifier} { saveExpr(new Identifier(yyline,yytext()));
  179. }
  180. {Comment}|{WhiteSpace} { }
  181. }
  182.  
  183.  
  184. /* error fallback */
  185. [^] { throwError("Illegal character"); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement