Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Lab_Final
  9. {
  10. struct wt
  11. {
  12. public string Word;
  13. public string Type;
  14.  
  15. public wt(string w, string t)
  16. {
  17. Word = w;
  18. Type = t;
  19. }
  20. }
  21. class Lexical
  22. {
  23. public static ArrayList ALLex = new ArrayList();
  24.  
  25. public static void Lex(string st)
  26. {
  27. int i = 0;
  28. char c;
  29. char[] ch = st.ToCharArray(0, st.Length);
  30. string w = "";
  31.  
  32. c = getchar(ref i, ch);
  33. while (c != '$')
  34. {
  35. w = "";
  36. if ((c == ' ') || (c == '\r') || (c == '\n') || (c == '\t'))
  37. {
  38. c = getchar(ref i, ch);
  39. continue;
  40. }
  41. else if (char.IsLetter(c))
  42. {
  43. w += c;
  44. c = getchar(ref i, ch);
  45. while (char.IsLetter(c))
  46. {
  47. w += c;
  48. c = getchar(ref i, ch);
  49. }
  50. if (RW(w))
  51. {
  52. ALLex.Add(new wt(w, "RW"));
  53. w = "";
  54. }
  55. else
  56. {
  57. ALLex.Add(new wt(w, "ID"));
  58. }
  59. unget(ref i, ch);
  60.  
  61. }
  62. else if (char.IsDigit(c))
  63. {
  64. w += c;
  65. c = getchar(ref i, ch);
  66. while (char.IsDigit(c))
  67. {
  68. w += c;
  69. c = getchar(ref i, ch);
  70. }
  71. if (c == '.')
  72. {
  73. w += c;
  74. c = getchar(ref i, ch);
  75. while (char.IsDigit(c))
  76. {
  77. w += c;
  78. c = getchar(ref i, ch);
  79. }
  80. unget(ref i, ch);
  81. ALLex.Add(new wt(w, "float_const"));
  82. }
  83. else
  84. {
  85. ALLex.Add(new wt(w, "int_const"));
  86. unget(ref i, ch);
  87. }
  88. }
  89. else if (OP(c))
  90. {
  91. if (c == '=')
  92. {
  93. w += c;
  94. c = getchar(ref i, ch);
  95. if (c == '=')
  96. {
  97. w += c;
  98. ALLex.Add(new wt(w, "OP"));
  99. }
  100. else
  101. {
  102. ALLex.Add(new wt(w, "OP"));
  103. unget(ref i, ch);
  104. }
  105. }
  106. else if (c == '!')
  107. {
  108. w += c;
  109. c = getchar(ref i, ch);
  110. if (c == '=')
  111. {
  112. w += c;
  113. ALLex.Add(new wt(w, "OP"));
  114. }
  115. else
  116. {
  117. ALLex.Add(new wt(w, "OP"));
  118. unget(ref i, ch);
  119. }
  120. }
  121. else if (c == '<')
  122. {
  123. w += c;
  124. c = getchar(ref i, ch);
  125. if (c == '=')
  126. {
  127. w += c;
  128. ALLex.Add(new wt(w, "OP"));
  129. }
  130. else
  131. {
  132. ALLex.Add(new wt(w, "OP"));
  133. unget(ref i, ch);
  134. }
  135. }
  136. else if (c == '>')
  137. {
  138. w += c;
  139. c = getchar(ref i, ch);
  140. if (c == '=')
  141. {
  142. w += c;
  143. ALLex.Add(new wt(w, "OP"));
  144. }
  145. else
  146. {
  147. ALLex.Add(new wt(w, "OP"));
  148. unget(ref i, ch);
  149. }
  150. }
  151. else if (c == '|')
  152. {
  153. w += c;
  154. c = getchar(ref i, ch);
  155. if (c == '|')
  156. {
  157. w += c;
  158. ALLex.Add(new wt(w, "OP"));
  159. }
  160. else
  161. {
  162.  
  163. }
  164. unget(ref i, ch);
  165. }
  166. else if (c == '&')
  167. {
  168. w += c;
  169. c = getchar(ref i, ch);
  170. if (c == '&')
  171. {
  172. w += c;
  173. ALLex.Add(new wt(w, "OP"));
  174. }
  175. else
  176. {
  177.  
  178. }
  179. unget(ref i, ch);
  180. }
  181. else
  182. {
  183. w += c;
  184. ALLex.Add(new wt(w, "OP"));
  185. }
  186. }
  187. else if (c == '"')
  188. {
  189. w += c;
  190. c = getchar(ref i, ch);
  191. while (c != '"')
  192. {
  193. w += c;
  194. c = getchar(ref i, ch);
  195. }
  196. w += c;
  197. ALLex.Add(new wt(w, "string_const"));
  198. }
  199. else if (c == '@')
  200. {
  201. w += c;
  202. c = getchar(ref i, ch);
  203. while (c != '@')
  204. {
  205. w += c;
  206. c = getchar(ref i, ch);
  207. }
  208. w += c;
  209. }
  210. c = getchar(ref i, ch);
  211. }
  212. ALLex.Add(new wt("$", "$"));
  213. }
  214. public static char getchar(ref int i, char[] ch)
  215. {
  216. char c;
  217. c = ch[i];
  218. i++;
  219. return c;
  220. }
  221. public static char unget(ref int i, char[] ch)
  222. {
  223. i--;
  224. char c;
  225. c = ch[i];
  226. return c;
  227. }
  228. public static void print()
  229. {
  230. foreach (wt tk in ALLex)
  231. {
  232. Console.WriteLine("\t" + tk.Word + "\t" + tk.Type);
  233. }
  234. }
  235. public static bool RW(string s)
  236. {
  237. switch (s)
  238. {
  239. case "int":
  240. case "float":
  241. case "string":
  242. case "Program":
  243. case "begin":
  244. case "end":
  245. case "fun":
  246. case "call":
  247. case "while":
  248. return true;
  249. default:
  250. return false;
  251. }
  252. }
  253. public static bool OP(char c)
  254. {
  255. switch (c)
  256. {
  257. case '=':
  258. case '+':
  259. case '-':
  260. case '*':
  261. case '/':
  262. case '(':
  263. case ')':
  264. case ';':
  265. case ',':
  266. case '<':
  267. case '>':
  268. case '!':
  269. case '|':
  270. case '&':
  271. return true;
  272. default:
  273. return false;
  274. }
  275. }
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement