Advertisement
Guest User

Parser

a guest
Mar 30th, 2018
2,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. %{
  2. #include "symtab.c"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. extern FILE *yyin;
  7. extern FILE *yyout;
  8. extern int lineno;
  9. extern int yylex();
  10. void yyerror();
  11. %}
  12.  
  13. /* token definition */
  14. %token CHAR INT FLOAT DOUBLE IF ELSE WHILE FOR CONTINUE BREAK VOID RETURN
  15. %token ADDOP MULOP DIVOP INCR OROP ANDOP NOTOP EQUOP RELOP
  16. %token LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE SEMI DOT COMMA ASSIGN REFER
  17. %token ID ICONST FCONST CCONST STRING
  18.  
  19. %start program
  20.  
  21. /* expression priorities and rules */
  22.  
  23. %%
  24.  
  25. program: declarations statements ;
  26.  
  27. declarations: declarations declaration | declaration;
  28.  
  29. declaration: type names SEMI ;
  30.  
  31. type: INT | CHAR | FLOAT | DOUBLE | VOID;
  32.  
  33. names: variable | names COMMA variable;
  34.  
  35. variable: ID |
  36. pointer ID |
  37. ID array
  38. ;
  39.  
  40. pointer: pointer MULOP | MULOP ;
  41.  
  42. array: array LBRACK ICONST RBRACK | LBRACK ICONST RBRACK ;
  43.  
  44. statements: statements statement | statement;
  45.  
  46. statement:
  47. if_statement | for_statement | while_statement | assigment |
  48. CONTINUE SEMI | BREAK SEMI | RETURN SEMI
  49. ;
  50.  
  51. if_statement: IF LPAREN expression RPAREN tail else_if_part else_part ;
  52.  
  53. else_if_part:
  54. else_if_part ELSE IF LPAREN expression RPAREN tail |
  55. ELSE IF LPAREN expression RPAREN tail |
  56. /* empty */
  57. ;
  58. else_part: ELSE tail | /* empty */ ;
  59.  
  60. for_statement: FOR LPAREN expression SEMI expression SEMI expression RPAREN tail ;
  61.  
  62. while_statement: WHILE LPAREN expression RPAREN tail ;
  63.  
  64. tail: statement SEMI | LBRACE statements RBRACE ;
  65.  
  66. expression:
  67. expression ADDOP expression |
  68. expression MULOP expression |
  69. expression DIVOP expression |
  70. expression INCR |
  71. INCR expression |
  72. expression OROP expression |
  73. expression ANDOP expression |
  74. NOTOP expression |
  75. expression EQUOP expression |
  76. expression RELOP expression |
  77. LPAREN expression RPAREN |
  78. variable |
  79. sign constant
  80. ;
  81.  
  82. sign: ADDOP | /* empty */ ;
  83.  
  84. constant: ICONST | FCONST | CCONST ;
  85.  
  86. assigment: reference variable ASSIGN expression SEMI ;
  87.  
  88. reference: REFER | /* empty */ ;
  89.  
  90. %%
  91.  
  92. void yyerror ()
  93. {
  94. fprintf(stderr, "Syntax error at line %d\n", lineno);
  95. exit(1);
  96. }
  97.  
  98. int main (int argc, char *argv[]){
  99.  
  100. // initialize symbol table
  101. init_hash_table();
  102.  
  103. // parsing
  104. int flag;
  105. yyin = fopen(argv[1], "r");
  106. flag = yyparse();
  107. fclose(yyin);
  108.  
  109. // symbol table dump
  110. yyout = fopen("symtab_dump.out", "w");
  111. symtab_dump(yyout);
  112. fclose(yyout);
  113.  
  114. return flag;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement