Facebook
From Harmless Hog, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 53
  1. %{
  2. #include <stdio.h>
  3. #include "y.tab.h"
  4. %}
  5. %option noyywrap
  6. %%
  7. "int"|"float"|"char"|"string" {return TIP;}
  8. "const" {return CONST;}
  9.  
  10. "begin_progr" {return BGIN;}
  11. "end_progr" {return END;}
  12. @[_a-zA-Z][_a-zA-Z0-9]* {return ID;}
  13. ":=" {return ASSIGN;}
  14. [0-9]+ {return NR;}
  15.  
  16. "+" {return PLUS;}
  17. "-" {return MINUS;}
  18. "*" {return ORI;}
  19. "/" {return IMPARTIRE;}
  20.  
  21. [ \t] ;
  22. \n {yylineno++;}
  23. . {return yytext[0];}
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. %{
  48. #include <stdio.h>
  49. extern FILE* yyin;
  50. extern char* yytext;
  51. extern int yylineno;
  52. %}
  53. %token ID TIP BGIN END ASSIGN NR OPERATOR CONST
  54. %token PLUS MINUS ORI IMPARTIRE
  55. %start progr
  56. %left PLUS
  57. %left MINUS
  58. %left ORI
  59. %left IMPARTIRE
  60.  
  61. %%
  62.  
  63. progr: declaratii bloc {printf("program corect sintactic\n");}
  64.      ;
  65.  
  66. declaratii :  declaratie ';'
  67.            | declaratii declaratie ';'
  68.  
  69. declaratie : TIP seq
  70.            | CONST TIP seq
  71.            | TIP ID '(' lista_param ')'
  72.            | TIP ID '(' ')'
  73.            ;
  74.  
  75. seq        : seq ',' se
  76.            | se
  77.            ;
  78.  
  79. se         : ID
  80.            | ID ASSIGN AExp
  81.            ;
  82.  
  83.  
  84.  
  85. AExp       : NR
  86.            | ID
  87.            | AExp PLUS AExp
  88.            | AExp MINUS AExp
  89.            | AExp ORI AExp
  90.            | AExp IMPARTIRE AExp
  91.            | '(' AExp ')'
  92.            ;
  93.  
  94. lista_param : param
  95.             | lista_param ','  param
  96.             ;
  97.            
  98. param : TIP ID
  99.       ;
  100.      
  101. /* bloc */
  102. bloc : BGIN list END  
  103.      ;
  104.      
  105. /* lista instructiuni */
  106. list :  statement ';'
  107.      |  list statement ';'
  108.      ;
  109.  
  110. /* instructiune */
  111. statement: ID ASSIGN AExp
  112.          | ID '(' lista_apel ')'
  113.          ;
  114.        
  115. lista_apel : AExp
  116.            | lista_apel ',' AExp
  117.            ;
  118. %%
  119. void yyerror(char * s){
  120. printf("eroare: %s la linia:%d\n",s,yylineno);
  121. }
  122.  
  123. int main(int argc, char** argv){
  124. yyin=fopen(argv[1],"r");
  125. yyparse();
  126. }
  127.  
  128.  
  129.