Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. %{
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. extern int yylex();
  7. extern int yyparse();
  8. extern FILE *yyin;
  9. extern int nrLine;
  10. void yyerror(const char *s);
  11. %}
  12.  
  13. %token IDENTIFICATOR
  14. %token CONSTANT
  15. %token FUNCTION
  16. %token CONST
  17. %token VAR
  18. %token BEGINN
  19. %token END
  20. %token IF
  21. %token ELSE
  22. %token FOR
  23. %token WHILE
  24. %token INTEGER
  25. %token REAL
  26. %token ARRAY
  27. %token OF
  28. %token READ
  29. %token WRITE
  30. %token CALL
  31. %token GT
  32. %token LT
  33. %token GE
  34. %token LE
  35. %token ASSIGN
  36. %token NEQ
  37. %token EQ
  38. %token OR
  39. %token AND
  40. %token NO_PARAMS
  41.  
  42. %%
  43.  
  44. function: FUNCTION IDENTIFICATOR declarParam BEGINN declarParam listaInstructiuni END
  45.  
  46. declarParam: NO_PARAMS | params
  47.  
  48. params: declarare | declarare params
  49.  
  50. declarare: VAR IDENTIFICATOR ':' type | CONST IDENTIFICATOR ':' type
  51.  
  52. type: type1|type2
  53.  
  54. type1: INTEGER | REAL
  55.  
  56. type2: ARRAY '[' CONSTANT ']' OF type1
  57.  
  58. listaInstructiuni: instructiune | instructiune listaInstructiuni
  59.  
  60. instructiune: atribuire | io | if | while | for | call
  61.  
  62. atribuire: IDENTIFICATOR ASSIGN expresie
  63.  
  64. expresie: term | expresie '+' term | expresie '-' term
  65.  
  66. term: factor | factor '*' term | factor '/' term |factor '%' term
  67.  
  68. factor: CONSTANT | IDENTIFICATOR | identifCompus | '(' expresie ')'
  69.  
  70. identifCompus: IDENTIFICATOR '[' chestie ']'
  71.  
  72. chestie: IDENTIFICATOR | CONSTANT
  73.  
  74. io: READ '(' IDENTIFICATOR ')' | READ '(' identifCompus ')' | WRITE '(' IDENTIFICATOR ')' | WRITE '(' identifCompus ')'
  75.  
  76. if: IF conditie '{' listaInstructiuni '}' | IF conditie '{' listaInstructiuni '}' ELSE '{' listaInstructiuni '}'
  77.  
  78. conditie: expresie relatie expresie
  79.  
  80. relatie: LE | LT | GE | GT | EQ | NEQ
  81.  
  82. while: WHILE conditie '{' listaInstructiuni '}'
  83.  
  84. for: FOR ':' IDENTIFICATOR ASSIGN chestie ',' chestie '{' listaInstructiuni '}' | FOR ':' IDENTIFICATOR ASSIGN chestie ',' chestie ',' chestie '{' listaInstructiuni '}'
  85.  
  86. call: CALL ':' IDENTIFICATOR '(' listaParam ')'
  87.  
  88. listaParam: IDENTIFICATOR ':' type | expresie ':' type | IDENTIFICATOR ':' type listaParam | expresie ':' type listaParam
  89. %%
  90.  
  91. int main(int argc, char *argv[]) {
  92. ++argv, --argc; /* skip over program name */
  93.  
  94. // sets the input for flex file
  95. if (argc > 0)
  96. yyin = fopen(argv[0], "r");
  97. else
  98. yyin = stdin;
  99.  
  100. //read each line from the input file and process it
  101. while (!feof(yyin)) {
  102. yyparse();
  103. }
  104. printf("The file is sintactly correct!\n");
  105. return 0;
  106. }
  107.  
  108. void yyerror(const char *s) {
  109. printf("Error: %s at line -> %d ! \n", s, nrLine);
  110. exit(1);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement