Advertisement
Five_NT

limbaj.y

Dec 28th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include "functii.h"
  4.  
  5. extern FILE* yyin;
  6. extern char* yytext;
  7. extern int yylineno;
  8. extern int yylval;
  9. %}
  10. %start PRG
  11. %left PLUS MINUS
  12. %left INM IMP
  13.  
  14. %token ID TIP
  15. %token BGINF ENDF
  16. %token EGAL
  17. %token NR
  18. %token PLUS MINUS
  19. %token INM IMP
  20. %token MOD
  21. %token PRINT
  22.  
  23. %%
  24. PRG: declaratii bloc {printf("program corect sintactic\n");}
  25.      ;
  26.  
  27. declaratii :  declaratie ';'
  28.        | declaratii declaratie ';'
  29.        ;
  30. declaratie : TIP ids
  31.            | TIP ID '(' lista_param ')'
  32.            | TIP ID '(' ')'
  33.            ;
  34.  
  35. ids : ID ',' ids
  36.     | ID
  37.     ;  
  38.  
  39. lista_param : param
  40.             | lista_param ','  param
  41.             ;
  42.            
  43. param : TIP ID
  44.       ;
  45.      
  46.  
  47. /* bloc */
  48. bloc : BGINF list ENDF  
  49.      ;
  50.  
  51. lista_apel : NR
  52.            | lista_apel ',' NR
  53.            ;
  54.  
  55. /* lista instructiuni */
  56. list : stmt ';'
  57.      | list stmt ';'
  58.      ;
  59.  
  60. /* instructiuni */
  61. expr: ID | NR
  62.     | expr PLUS expr {$$ = $1 + $3;}
  63.     | expr MINUS expr {$$ = $1 - $3;}
  64.     | expr INM expr {$$ = $1 * $3;}
  65.     | expr IMP expr {if($3 != 0) $$ = $1 / $3;}
  66.     | MOD '(' expr ',' expr ')' {$$ = f_mod($1, $3);}
  67.     ;
  68.  
  69. stmt: ID EGAL expr
  70.     | ID '(' lista_apel ')'
  71.     | PRINT '(' ids ')'
  72.     ;
  73.  
  74. %%
  75. int yyerror(char * s){
  76. printf("eroare: %s la linia:%d\n",s,yylineno);
  77. }
  78.  
  79. int main(int argc, char** argv){
  80. yyin=fopen(argv[1],"r");
  81. yyparse();
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement