Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 KB | None | 0 0
  1. %{
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #define YY_DECL int alpha_yylex(void* yylval)
  6.  
  7. typedef struct alpha_token_t {
  8.  
  9. unsigned int numline;
  10. unsigned int numToken;
  11. char *content;
  12. char *type;
  13. char *macro;
  14. char *wtf;
  15.  
  16. struct alpha_token_t * next;
  17.  
  18. }alpha_token_t;
  19.  
  20. char * buffered_string = NULL;
  21. char * old_buffered_string = NULL;
  22. int nested_comment = 0;
  23. int * start_comments;
  24. int array_index = 0;
  25. int yytokenno = 0;
  26. int lineno = 1;
  27. int illegal_char_flag = 0;
  28.  
  29. alpha_token_t *head = NULL;
  30. alpha_token_t *tail = NULL;
  31.  
  32. void insert_token(unsigned int numline, unsigned int numToken, char* content, char* type, char* macro, char* wtf){
  33.  
  34. alpha_token_t *new = malloc(sizeof(alpha_token_t));
  35.  
  36. new->numline = numline;
  37. new->numToken = numToken;
  38. new->content = strdup(content);
  39. new->type = strdup(type);
  40. new->macro = strdup(macro);
  41. new->wtf = strdup(wtf);
  42. new->next = NULL;
  43.  
  44. if(head == NULL){
  45. head = new;
  46. tail = new;
  47. head->next = NULL;
  48. return;
  49. }
  50.  
  51. tail->next = new;
  52. tail = new;
  53. }
  54.  
  55. void print_tokens(){
  56. alpha_token_t *tmp = head;
  57.  
  58. while(tmp != NULL){
  59. if(strcmp(tmp->type,"STRING") == 0 || strcmp(tmp->type,"ID") == 0){
  60. printf("%d: #%d \"%s\" %s \"%s\" <-%s\n", tmp->numline,tmp->numToken,tmp->content,tmp->type,tmp->macro,tmp->wtf);
  61. }else{
  62. printf("%d: #%d \"%s\" %s %s <-%s\n", tmp->numline,tmp->numToken,tmp->content,tmp->type,tmp->macro,tmp->wtf);
  63. }
  64.  
  65. tmp = tmp->next;
  66. }
  67. }
  68.  
  69. %}
  70.  
  71. %option header-file="./scanner.h"
  72. %option noyywrap
  73. %option yylineno
  74. %x string
  75. %x blockcom
  76.  
  77. if "if"
  78. else "else"
  79. while "while"
  80. for "for"
  81. function "function"
  82. return "return"
  83. break "break"
  84. continue "continue"
  85. and "and"
  86. not "not"
  87. or "or"
  88. local "local"
  89. true "true"
  90. false "false"
  91. nil "nil"
  92.  
  93. ass "="
  94. add "+"
  95. sub "-"
  96. mul "*"
  97. div "/"
  98. mod "%"
  99. eq "=="
  100. neq "!="
  101. inc "++"
  102. dec "--"
  103. gr ">"
  104. less "<"
  105. greq ">="
  106. leq "<="
  107.  
  108. integer [0-9]+
  109. real [0-9]+\.[0-9]+
  110.  
  111. lcurbr "{"
  112. rcurbr "}"
  113. lbr "["
  114. rbr "]"
  115. lpar "("
  116. rpar ")"
  117. semic ";"
  118. comma ","
  119. colon ":"
  120. dcolon "::"
  121. dot "."
  122. ddot ".."
  123.  
  124. id [a-zA-Z][a-zA-Z_0-9]*
  125. linecom "//".*
  126.  
  127.  
  128. whitespace [\t' '\r]*
  129. newline ['\n']
  130. OTHER .[id]*
  131.  
  132. %%
  133.  
  134.  
  135.  
  136. {if} {insert_token(yylineno,++yytokenno,yytext,"KEYWORD","IF","enumerated"); /*[^_|^$|^~|^?|^#|^&&|^\|\||^!]*/}
  137.  
  138. {else} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","ELSE","enumerated");}
  139.  
  140. {while} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","WHILE","enumerated");}
  141.  
  142. {for} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","FOR","enumerated");}
  143.  
  144. {function} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","FUNCTION","enumerated");}
  145.  
  146. {return} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","RETURN","enumerated");}
  147.  
  148. {break} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","BREAK","enumerated");}
  149.  
  150. {continue} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","CONTINUE","enumerated");}
  151.  
  152. {and} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","AND","enumerated");}
  153.  
  154. {not} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","NOT","enumerated");}
  155.  
  156. {or} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","OR","enumerated");}
  157.  
  158. {local} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","LOCAL","enumerated");}
  159.  
  160. {true} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","TRUE","enumerated");}
  161.  
  162. {false} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","FALSE","enumerated");}
  163.  
  164. {nil} {insert_token(lineno,++yytokenno,yytext,"KEYWORD","NIL","enumerated");}
  165.  
  166. {ass} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","ASSIGN","enumerated");}
  167.  
  168. {add} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","PLUS","enumerated");}
  169.  
  170. {sub} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","MINUS","enumerated");}
  171.  
  172. {mul} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","MULTIPLICATION","enumerated");}
  173.  
  174. {div} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","DIVISION","enumerated");}
  175.  
  176. {mod} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","MODULO","enumerated");}
  177.  
  178. {eq} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","EQUAL","enumerated");}
  179.  
  180. {neq} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","NOT_EQUAL","enumerated");}
  181.  
  182. {inc} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","PLUS_PLUS","enumerated");}
  183.  
  184. {dec} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","MINUS_MINUS","enumerated");}
  185.  
  186. {gr} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","GREATER_THAN","enumerated");}
  187.  
  188. {less} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","LESS_THAN","enumerated");}
  189.  
  190. {greq} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","GREATER_EQUAL","enumerated");}
  191.  
  192. {leq} {insert_token(lineno,++yytokenno,yytext,"OPERATOR","LESS_EQUAL","enumerated");}
  193.  
  194. {integer} {
  195. char * str = malloc(strlen(yytext));
  196. strcpy(str, yytext);
  197.  
  198. insert_token(lineno,++yytokenno,str,"CONST_INT",str,"integer");
  199. }
  200.  
  201. {real} {
  202. char * str = malloc(strlen(yytext));
  203. strcpy(str, yytext);
  204.  
  205. insert_token(lineno,++yytokenno,str,"REAL_INT",str,"real");
  206. }
  207.  
  208. {lcurbr} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","LEFT_CURLY_BRUCKET","enumerated");}
  209.  
  210. {rcurbr} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","RIGHT_CURLY_BRUCKET","enumerated");}
  211.  
  212. {lbr} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","LEFT_BRUCKET","enumerated");}
  213.  
  214. {rbr} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","RIGHT_BRUCKET","enumerated");}
  215.  
  216. {lpar} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","LEFT_PARENTHESIS","enumerated");}
  217.  
  218. {rpar} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","RIGHT_PARENTHESIS","enumerated");}
  219.  
  220. {semic} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","SEMICOLON","enumerated");}
  221.  
  222. {comma} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","COMMA","enumerated");}
  223.  
  224. {colon} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","COLON","enumerated");}
  225.  
  226. {dcolon} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","DOUBLE_COLON","enumerated");}
  227.  
  228. {dot} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","DOT","enumerated");}
  229.  
  230. {ddot} {insert_token(lineno,++yytokenno,yytext,"PUNCTUATION","DOUBLE_DOT","enumerated");}
  231.  
  232. {id} {char * str = malloc(strlen(yytext));
  233. strcpy(str, yytext);
  234. insert_token(lineno,++yytokenno,str,"ID",str,"char*");}
  235.  
  236. {linecom} {insert_token(lineno,++yytokenno,"","LINE_COMMENT","","enumerated");}
  237.  
  238. {whitespace} {}
  239.  
  240. {newline} {lineno++;}
  241.  
  242.  
  243. \" {
  244. BEGIN(string);
  245. buffered_string = NULL;
  246. old_buffered_string = NULL;
  247. }
  248.  
  249. <string>[^"\\]* {
  250.  
  251. if(old_buffered_string == NULL){
  252. buffered_string = malloc(strlen(yytext) + 1);
  253. strcpy(buffered_string, yytext);
  254. old_buffered_string = malloc(strlen(buffered_string));
  255. strcpy(old_buffered_string, buffered_string);
  256.  
  257. }
  258. else{
  259. buffered_string = realloc(buffered_string, strlen(old_buffered_string) + strlen(yytext) + 1);
  260. strcat(buffered_string, yytext);
  261. old_buffered_string = realloc(old_buffered_string, strlen(buffered_string));
  262. strcpy(old_buffered_string, buffered_string);
  263. }
  264. }
  265.  
  266.  
  267. <string>\\t {
  268.  
  269. if(old_buffered_string == NULL){
  270. buffered_string = malloc(2);
  271. char c = '\t';
  272. strncat(buffered_string, &c, 1);
  273. old_buffered_string = malloc(strlen(buffered_string));
  274. strcpy(old_buffered_string, buffered_string);
  275.  
  276. }
  277. else{
  278. buffered_string = realloc(buffered_string, strlen(old_buffered_string) + 2);
  279. char c = '\t';
  280. strncat(buffered_string, &c, 1);
  281. old_buffered_string = realloc(old_buffered_string, strlen(buffered_string));
  282. strcpy(old_buffered_string, buffered_string);
  283. }
  284. }
  285. <string>\\n {
  286.  
  287. if(old_buffered_string == NULL){
  288. buffered_string = malloc(2);
  289. char c = '\n';
  290. strncat(buffered_string, &c, 1);
  291. old_buffered_string = malloc(strlen(buffered_string));
  292. strcpy(old_buffered_string, buffered_string);
  293.  
  294. }
  295. else{
  296. buffered_string = realloc(buffered_string, strlen(old_buffered_string) + 2);
  297. char c = '\n';
  298. strncat(buffered_string, &c, 1);
  299. old_buffered_string = realloc(old_buffered_string, strlen(buffered_string));
  300. strcpy(old_buffered_string, buffered_string);
  301. }
  302.  
  303.  
  304. }
  305.  
  306. <string>\\\" {
  307.  
  308. if(old_buffered_string == NULL){
  309. buffered_string = malloc(2);
  310. char c = '\"';
  311. strncat(buffered_string, &c, 1);
  312. old_buffered_string = malloc(strlen(buffered_string));
  313. strcpy(old_buffered_string, buffered_string);
  314.  
  315. }
  316. else{
  317. buffered_string = realloc(buffered_string, strlen(old_buffered_string) + 2);
  318. char c = '\"';
  319. strncat(buffered_string, &c, 1);
  320. old_buffered_string = realloc(old_buffered_string, strlen(buffered_string));
  321. strcpy(old_buffered_string, buffered_string);
  322. }
  323. }
  324. <string>\\\\ {
  325.  
  326. if(old_buffered_string == NULL){
  327. buffered_string = malloc(2);
  328. char c = '\\';
  329. strncat(buffered_string, &c, 1);
  330. old_buffered_string = malloc(strlen(buffered_string));
  331. strcpy(old_buffered_string, buffered_string);
  332.  
  333. }
  334. else{
  335. buffered_string = realloc(buffered_string, strlen(old_buffered_string) + 2);
  336. char c = '\\';
  337. strncat(buffered_string, &c, 1);
  338. old_buffered_string = realloc(old_buffered_string, strlen(buffered_string));
  339. strcpy(old_buffered_string, buffered_string);
  340. }
  341. }
  342.  
  343. <string>\" {
  344. if(buffered_string == NULL){
  345. insert_token(lineno,++yytokenno,"","STRING","","char*");
  346. }
  347. else if(!illegal_char_flag){
  348. insert_token(lineno,++yytokenno,buffered_string,"STRING",buffered_string,"char*");
  349.  
  350. }
  351. illegal_char_flag = 0;
  352. BEGIN(INITIAL);
  353. }
  354. <string>\\.{1} {
  355. printf(" Illegal escape character in string.\n");
  356. illegal_char_flag = 1;
  357.  
  358. }
  359.  
  360. <string><<EOF>> { printf("unterminated string\n"); BEGIN(INITIAL);}
  361.  
  362.  
  363.  
  364. "/*" {
  365. BEGIN(blockcom);
  366. start_comments = realloc(start_comments, (array_index + 1) * sizeof(int));
  367. start_comments[array_index] = lineno;
  368. array_index++;
  369. }
  370. <blockcom>"*/" {
  371. char str[100];
  372. array_index--;
  373. sprintf(str, "%d - %d", start_comments[array_index], lineno);
  374.  
  375. if(nested_comment == 0){
  376. insert_token(lineno, ++yytokenno, str,"COMMENT", "BLOCK_COMMENT", "enumerated");
  377. BEGIN(INITIAL);
  378. }else{
  379. nested_comment--;
  380. insert_token(lineno, ++yytokenno, str,"COMMENT", "NESTED_COMMENT", "enumerated");
  381. }
  382. }
  383.  
  384. <blockcom>"/*" {
  385. nested_comment++;
  386. start_comments = realloc(start_comments, (array_index + 1) * sizeof(int));
  387. start_comments[array_index] = lineno;
  388. array_index++;
  389. }
  390.  
  391.  
  392. <blockcom>"\n" {
  393.  
  394. lineno++;
  395. }
  396. <blockcom>^"//" {
  397. insert_token(lineno, ++yytokenno, "","LINE_COMMENT", "", "enumerated");
  398.  
  399. }
  400.  
  401.  
  402. <blockcom><<EOF>> {
  403. printf("unterminated comments\n");
  404. BEGIN(INITIAL);
  405. }
  406. <blockcom>. {}
  407.  
  408. {OTHER} {
  409. printf("undefined character %s in line %d\n", yytext, lineno);
  410. }
  411.  
  412. %%
  413.  
  414. int main(int argc, char* argv[]){
  415.  
  416. if (argc > 1) {
  417. if (!(yyin = fopen(argv[1], "r"))) {
  418. fprintf(stderr, "Cannot open file %s\n", argv[1]);
  419. return -1;
  420. }
  421.  
  422. if (argc > 2) {
  423. if (!(yyout = fopen(argv[2], "w"))) {
  424. fprintf(stderr, "Cannot open file %s\n", argv[1]);
  425. return -1;
  426. }
  427. }
  428. }
  429.  
  430. alpha_yylex(head);
  431. print_tokens();
  432. return 0;
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement