TwisterMike

scanner.c

Feb 16th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. /*Group 2 Program 2
  2. Paul Maclean- mac7537@calu.edu
  3. Mike Gorse- gor9632@calu.edu
  4. Robert Breckenridge- bre6896@calu.edu
  5. Chase Smith- smi8808@calu.edu
  6.  
  7. CSC 460
  8. Language Translations
  9. */
  10. #include "scanner.h"
  11. #include "file_util.h" //i need them logicals...................................... this seems ill-advised
  12.  
  13.  
  14. char lineBuff[LINE_BUFF_SIZE] = { '\0' };
  15. char tokenBuff[TOKEN_BUFF_SIZE] = { '\0' };
  16. char tokenType[TOKEN_TYPE_SIZE] = { '\0' };
  17.  
  18. int buffpos = 0;
  19.  
  20. int lineCount = 1;
  21. int linePos = 0;
  22.  
  23. FILE* SrcFile;
  24.  
  25.  
  26. //*** Scanner Support Functions ***//
  27.  
  28. logical isDelimiter(char currchar) {
  29. return (currchar == ' ' || currchar == '\n' || currchar == '\t' || currchar == EOF);
  30. }
  31.  
  32. char getUpperChar() {
  33. char filechar = fgetc(InpFile);
  34. if (filechar == '\n' || (filechar == EOF && linePos > 0)) {
  35. char* errorBuffer[OUTFILE_BUFF_SIZE] = { '\0' };
  36. char* lisfileBuffer[OUTFILE_BUFF_SIZE] = { '\0' };
  37. char currErr;
  38. sprintf(lisfileBuffer, "%2d %s\n", lineCount, lineBuff);
  39. fputs(lisfileBuffer, LisFile);
  40.  
  41. int i = 0;
  42. for(i;i<Lex_Err_Index;i++)
  43. {
  44. currErr = Lex_Err_Buff[i];
  45. sprintf(errorBuffer, "Lexical Error on line %2d- %c not recognized\n", lineCount, currErr);
  46. fputs(errorBuffer, LisFile);
  47. }
  48. Lex_Err_Index = 0;
  49. memset(Lex_Err_Buff, '\0', LINE_BUFF_SIZE);
  50. //clear line buffer. if we end up putting a character back after this we're fucked
  51. memset(lineBuff, '\0', LINE_BUFF_SIZE);
  52. linePos = 0;
  53. lineCount++;
  54. }
  55. else {
  56. lineBuff[linePos] = filechar;
  57. linePos++;
  58. }
  59.  
  60. return toupper(filechar);
  61. }
  62.  
  63. void putBackChar(char currchar) {
  64. lineBuff[linePos] = '\0';
  65.  
  66. //only decrement if able to
  67. if (linePos > 0)
  68. linePos--;
  69.  
  70.  
  71. if (currchar != '\n') {
  72. ungetc(currchar, SrcFile);
  73. }
  74. }
  75.  
  76. logical gatherDigit(char currchar) {
  77. do {
  78. tokenBuff[buffpos] = currchar;
  79. currchar = getUpperChar();
  80. buffpos++;
  81. } while (isdigit(currchar) && !isDelimiter(currchar) && buffpos < TOKEN_BUFF_SIZE);
  82. putBackChar(currchar);
  83. }
  84.  
  85.  
  86.  
  87. void clearBuffer(char* buffer, int bufferSize)
  88. {
  89. int i = 0;
  90. for (i; i < bufferSize; i++)
  91. {
  92. buffer[i] = '\0';
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. char* decode(TokenId typeid)
  102. {
  103. return TOKEN_TYPES[typeid];
  104. }
  105.  
  106. TokenId getReservedId() {
  107. TokenId foundtoken = ID; //assume it's an ID, because
  108. int i = 0;
  109. //only go up to 13 because that's the last enterable reserved word
  110. for (i = 0; i < 13; i++) {
  111. if (strcmp(tokenBuff, TOKEN_TYPES[i]) == 0) {
  112. foundtoken = i;
  113. }
  114. }
  115.  
  116. return foundtoken;
  117. }
  118.  
  119.  
  120.  
  121.  
  122. TokenId getToken()
  123. {
  124. TokenId thistoken = -1;
  125.  
  126. buffpos = 0;
  127. char currchar;
  128.  
  129. do {
  130. currchar = getUpperChar();
  131.  
  132.  
  133. if (isspace(currchar)) {
  134. //skip whitespace
  135. }
  136. else if (isalpha(currchar)) { //compose ID
  137.  
  138. do {
  139. tokenBuff[buffpos] = currchar;
  140. currchar = getUpperChar();
  141. buffpos++;
  142. } while (isalnum(currchar) && !isDelimiter(currchar) && buffpos < TOKEN_BUFF_SIZE);
  143.  
  144. putBackChar(currchar);
  145.  
  146. thistoken = getReservedId();
  147. }
  148.  
  149. else if (isdigit(currchar)) { //compose digit
  150. gatherDigit(currchar);
  151. thistoken = INTLITERAL;
  152. }
  153. else if (currchar == '-') { //check possible '-' paths
  154. char lookahead = getUpperChar();
  155. putBackChar(lookahead); //all paths require an ungetc.
  156.  
  157. if (lookahead == '-') { //compose comment
  158. do {
  159. currchar = getUpperChar();
  160. buffpos++;
  161. } while (currchar != '\n' && currchar != EOF && buffpos < TOKEN_BUFF_SIZE);
  162. }
  163. else if (isdigit(lookahead)) {
  164. gatherDigit(currchar);
  165. thistoken = INTLITERAL;
  166. }
  167. else {
  168. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "-");
  169. thistoken = MINUSOP;
  170. }
  171.  
  172. }
  173. else if (currchar == '(')
  174. {
  175. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "(");
  176. thistoken = LPAREN;
  177. }
  178. else if (currchar == ')')
  179. {
  180. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ")");
  181. thistoken = RPAREN;
  182. }
  183. else if (currchar == ';')
  184. {
  185. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ";");
  186. thistoken = SEMICOLON;
  187. }
  188. else if (currchar == ',')
  189. {
  190. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ",");
  191. thistoken = COMMA;
  192. }
  193. else if (currchar == ':')
  194. {
  195. char lookahead = getUpperChar();
  196. if (lookahead == '=')
  197. {
  198. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ":=");
  199. thistoken = ASSIGNOP;
  200. }
  201. else
  202. {
  203. putBackChar(lookahead);
  204. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ":");
  205. Lex_Err_Buff[Lex_Err_Index] = currchar;
  206. Lex_Err_Index++;
  207. Lex_Err_Total++;
  208. thistoken = LEXERR;
  209. }
  210. }
  211. else if (currchar == '+')
  212. {
  213. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "+");
  214. thistoken = PLUSOP;
  215. }
  216. else if (currchar == '*')
  217. {
  218. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "*");
  219. thistoken = MULTOP;
  220. }
  221. else if (currchar == '/')
  222. {
  223. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "/");
  224. thistoken = DIVOP;
  225. }
  226. else if (currchar == '!')
  227. {
  228. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "!");
  229. thistoken = NOTOP;
  230. }
  231. else if (currchar == '<')
  232. {
  233. char lookahead = getUpperChar();
  234. if (lookahead == '=')
  235. {
  236. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "<=");
  237. thistoken = LESSEQUALOP;
  238. }
  239. else if (lookahead == '>')
  240. {
  241. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "<>");
  242. thistoken = NOTEQUALOP;
  243. }
  244. else
  245. {
  246. putBackChar(lookahead);
  247. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "<");
  248. thistoken = LESSOP;
  249. }
  250. }
  251. else if (currchar == '>')
  252. {
  253. char lookahead = getUpperChar();
  254. if (lookahead == '=')
  255. {
  256. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ">=");
  257. thistoken = GREATEREQUALOP;
  258. }
  259. else
  260. {
  261. putBackChar(lookahead);
  262. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, ">");
  263. thistoken = GREATEROP;
  264. }
  265. }
  266. else if (currchar == '=')
  267. {
  268. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "=");
  269. thistoken = EQUALOP;
  270. }
  271. else if (currchar == EOF)
  272. {
  273. strcpy_s(tokenBuff, TOKEN_BUFF_SIZE, "EOF");
  274. thistoken = SCANEOF;
  275. }
  276. else
  277. {
  278. tokenBuff[buffpos] = currchar;
  279. thistoken = LEXERR;
  280. Lex_Err_Buff[Lex_Err_Index] = currchar;
  281. Lex_Err_Index++;
  282. Lex_Err_Total++;
  283. }
  284. } while (thistoken == -1 && currchar != EOF);
  285. //may need a flag for comments
  286.  
  287.  
  288. return thistoken;
  289. }
  290.  
  291.  
  292.  
  293. //*** Primary Scanner Function ***//
  294.  
  295. void scanner(FILE* InpFile)
  296. {
  297.  
  298. char* printBuffer[OUTFILE_BUFF_SIZE];
  299.  
  300. SrcFile = InpFile;
  301.  
  302. TokenId thistoken;
  303.  
  304. do {
  305. thistoken = getToken();
  306.  
  307. //do things here
  308.  
  309. char* tokentypeptr = decode(thistoken);
  310.  
  311. // printf("Token Number: %d\t\t\tToken Type: %s\t\t\tActual Token: %s\n", thistoken, tokentypeptr, tokenBuff);
  312.  
  313. clearBuffer(printBuffer, OUTFILE_BUFF_SIZE);
  314. sprintf(printBuffer, "Token Number: %d\t\t\tToken Type: %s\t\t\tActual Token: %s\n", thistoken, tokentypeptr, tokenBuff);
  315. fputs(printBuffer, OutFile);
  316.  
  317. clearBuffer(tokenBuff, TOKEN_BUFF_SIZE);
  318. } while (thistoken != SCANEOF);
  319. clearBuffer(printBuffer, OUTFILE_BUFF_SIZE);
  320. sprintf(printBuffer, "Number of total errors: %d\n", Lex_Err_Total);
  321. fputs(printBuffer, LisFile);
  322.  
  323. }
Add Comment
Please, Sign In to add comment