Advertisement
anticlown

Lab #3(lexer + parser), PHP lang

Dec 10th, 2023 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #####################################
  2. | lexer start | //made with flex(GNU)
  3. #####################################
  4.  
  5. %{
  6. #include "y.tab.h"
  7. %}
  8.  
  9. %%
  10.  
  11. [ \t\n] {
  12. /* игнорирование нескольких пробелов подряд, новых строк */
  13. }
  14.  
  15. "$"[a-zA-Z_][a-zA-Z0-9_]* {
  16. return IDENTIFIER;
  17. }
  18.  
  19. \"[^"]*\" {
  20. return STRING_LITERAL;
  21. }
  22.  
  23. [0-9]+ {
  24. return INTEGER;
  25. }
  26.  
  27. [0-9]+(\.)[0-9]+ {
  28. return FLOAT;
  29. }
  30.  
  31. [><]|">="|"<="|"=="|"!=" {
  32. return COMPARE_OP;
  33. }
  34.  
  35. "=" {
  36. return ASSIGN_OP;
  37. }
  38.  
  39. [-+*/] {
  40. return ARITH_OPP;
  41. }
  42.  
  43. "++"|"--" {
  44. return INC_OP;
  45. }
  46.  
  47. "(" {
  48. return L_PAREN;
  49. }
  50.  
  51. ")" {
  52. return R_PAREN;
  53. }
  54.  
  55. "{" {
  56. return L_FIG;
  57. }
  58.  
  59. "}" {
  60. return R_FIG;
  61. }
  62.  
  63. ";" {
  64. return 0;
  65. }
  66.  
  67. ":" {
  68. return COLON;
  69. }
  70.  
  71. "if" {
  72. return IF;
  73. }
  74.  
  75. "else" {
  76. return ELSE;
  77. }
  78.  
  79. "while" {
  80. return WHILE;
  81. }
  82.  
  83. "for" {
  84. return FOR;
  85. }
  86.  
  87. . {
  88. yyerror("invalid character");
  89. }
  90.  
  91. %%
  92.  
  93. int yywrap() {
  94. return 1;
  95. }
  96.  
  97.  
  98. #####################################
  99. | parser start | //made with bison(GNU)
  100. #####################################
  101.  
  102. %{
  103. #include <stdio.h>
  104.  
  105. extern int yylineno;
  106. extern char *yytext;
  107. void yyerror(char *);
  108.  
  109. %}
  110.  
  111. %token IDENTIFIER
  112.  
  113. /* statement tokens */
  114. %token IF
  115. %token ELSE
  116. %token WHILE
  117. %token FOR
  118.  
  119. /* bracket tokens */
  120. %token L_PAREN
  121. %token R_PAREN
  122. %token L_FIG
  123. %token R_FIG
  124.  
  125. /* tokens */
  126. %token COLON
  127.  
  128. /* operation tokens */
  129. %token COMPARE_OP
  130. %token ASSIGN_OP
  131. %token ARITH_OPP
  132. %token INC_OP
  133.  
  134. /* type tokens */
  135. %token INTEGER
  136. %token FLOAT
  137. %token STRING_LITERAL
  138.  
  139.  
  140. %%
  141.  
  142. block:
  143. | statement
  144. | L_FIG block R_FIG
  145. ;
  146.  
  147. statement: assign_statement
  148. | expression
  149. | if_statement
  150. | if_else_statement
  151. | for_loop
  152. | while_loop
  153. ;
  154.  
  155. expression: arith_expression
  156. | logical_expression
  157. ;
  158.  
  159. logical_expression: expression COMPARE_OP expression
  160. ;
  161.  
  162. atom: INTEGER
  163. | FLOAT
  164. | STRING_LITERAL
  165. | IDENTIFIER
  166. ;
  167.  
  168. arith_expression: atom
  169. | atom ARITH_OPP arith_expression
  170. ;
  171.  
  172. /* STATEMENTS START */
  173. assign_statement: IDENTIFIER ASSIGN_OP expression { printf("\t\tcorrect ASSIGNMENT statement \n"); }
  174. ;
  175.  
  176. if_statement: IF L_PAREN logical_expression R_PAREN block { printf("\t\tcorrect IF statement \n"); }
  177. ;
  178.  
  179. if_else_statement: IF L_PAREN logical_expression R_PAREN block ELSE block { printf("\t\tcorrect IF-ELSE statement statement \n"); }
  180. ;
  181.  
  182. for_loop: FOR L_PAREN arith_expression logical_expression arith_expression R_PAREN block { printf("\t\tcorrect FOR statement \n"); }
  183. ;
  184.  
  185. while_loop: WHILE L_PAREN logical_expression R_PAREN COLON block { printf("\t\tcorrect WHILE statement \n"); }
  186. ;
  187. /* STATEMENTS END */
  188.  
  189. %%
  190.  
  191. int main() {
  192. fprintf(stdout, "\n\tThis program receives a text that contains PHP code and checks it\'s correctness\n\n");
  193.  
  194. yylineno = 1;
  195.  
  196. yyparse();
  197. fprintf(stdout, "\n\tEnded successfully!\n");
  198. return 0;
  199. }
  200.  
  201. void yyerror(char *errmsg)
  202. {
  203. fprintf(stderr, "\n%s (%d): %s\n", errmsg, yylineno, yytext);
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement