Advertisement
SimoDR

tinyC_BySimo

May 7th, 2020
2,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CFDG 1.52 KB | None | 0 0
  1. grammar tinyc;
  2.  
  3. start       : program EOF ;
  4. program     : statement | statement program ;
  5.          
  6. statement   : decl ';' | assign ';' | branch | loop | out ';' |in ';' ;
  7.  
  8. decl        : 'int' ID ;
  9. assign      : ID '=' expr | ID '=' in;
  10. out         : 'write' '(' expr ')' ;
  11. in          : 'read' '(' ')' ;
  12. branch      : 'if' '(' relation ')' '{' program '}' | 'if' '(' relation ')' '{' program '}' 'else' '{' program '}' ;
  13.  
  14. loop       : 'while' '(' relation ')' '{' program '}' ;
  15.  
  16. relation     : exprBool;
  17.  
  18. exprBool     : exprBool LT exprBool | exprBool LEQ exprBool | exprBool EQ exprBool | exprBool NEQ exprBool | exprBool GEQ exprBool | exprBool GT exprBool | exprBool OR orExprBool | NOT exprBool | orExprBool ;
  19. orExprBool   : orExprBool  AND andExprBool  | andExprBool  ;
  20. andExprBool  : '(' exprBool ')' | expr ;
  21.  
  22. expr        : expr ADD term | expr SUB term | term;
  23. term        : term MULT oper | term DIV oper | term MOD oper | oper ;
  24. oper        : '(' expr ')' | NUMBER | ID | in ;
  25.  
  26.  
  27. ADD       : '+' ;
  28. SUB       : '-' ;
  29. DIV       : '/' ;
  30. MOD       : '%' ;
  31. MULT      : '*' ;
  32. AND       : '&&' ;
  33. OR        : '||' ;
  34. NOT       : '!'  ;
  35. EQ        : '==' ;
  36. LT        : '<' ;
  37. LEQ       : '<=' ;
  38. GT        : '>' ;
  39. GEQ       : '>=' ;
  40. NEQ       : '!=' ;
  41. ID        : [a-z]+ ;
  42. NUMBER    : [0-9]+ ;
  43. COMMENT   : '/*' .*? '*/' -> skip ;     // .*? matches anything until the first */
  44. LINE_COMMENT : '//' ~[\r\n]* -> skip ;  // ~[\r\n]* matches anything but \r and \n
  45. WS        : [ \n\t\r]+ -> skip;
  46. ErrorChar : . ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement