Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. grammar MBDVL;
  2.  
  3. /* LEXER RULES */
  4. SINGLE_COMMENT : '//' ~('\r' | '\n')* -> skip ;
  5. MULTILINE_COMMENT : '/*' .*? '*/' -> skip ;
  6. WS : [ \t\n\r]+ -> skip ;
  7. fragment LETTER : ('a' .. 'z') | ('A' .. 'Z') ;
  8. BOOL : 'true' | 'false' ;
  9. IDENTIFIER : LETTER+ ;
  10. fragment NUMBER : ('0' .. '9') | '-'('0' .. '9') ;
  11. ANYCHAR : '.' | '!' | '?' | '/' | ',' | '(' | ')' ;
  12. COORDS : NUM ';' NUM ';' NUM ;
  13. NUM : NUMBER+ | NUMBER+ '.' NUMBER+ ;
  14. STR : '"' (LETTER | NUMBER | WS | ANYCHAR)* '"' | '\'' (LETTER | NUMBER | WS | ANYCHAR)* '\'' ;
  15. ITEM_ID : NUMBER+ ':' NUMBER+ ;
  16. MULDIVMODOP : '*' | '/' | '%' ;
  17. ADDSUBOP : '+' | '-' ;
  18. NEGOP : '!' ;
  19. EQOP : '==' | '!=' | '<' | '<=' | '>' | '>=' ;
  20. LOGOP : '&&' | '||' ;
  21.  
  22. /* PROGRAM GRAMMAR */
  23.  
  24. prog : 'begin' 'bot' body 'end' 'bot' ;
  25. body : glob_var* initiate main function* ;
  26. initiate : 'initiate' stmt* 'end' 'initiate' ;
  27. main : 'loop' stmt* 'end' 'loop' ;
  28. type : 'num' | 'bool' | 'string' | 'block' | 'item' | 'coords' ;
  29. eventtype : 'block' | 'entity' | 'hunger' |'inventory' | 'health' | 'durability' ;
  30.  
  31. function
  32. : 'function' IDENTIFIER '(' args ')' stmt* 'end' 'function' #function_func
  33. | 'activity' IDENTIFIER '(' args ')' stmt* 'end' 'activity' #activity_func
  34. | 'event' eventtype '(' event_args ')' stmt* 'end' 'event' #event_func
  35. ;
  36.  
  37. event_arg
  38. : ITEM_ID #block_event_arg
  39. | NUM #num_event_arg
  40. ;
  41.  
  42. arg
  43. : type IDENTIFIER #type_arg
  44. | type '[' ']' IDENTIFIER #type_arr_arg
  45. | dcl #dcl_arg
  46. | /* epsilon */ #empty_arg
  47. ;
  48.  
  49. i_arg
  50. : cond #iarg
  51. | /* epsilon */ #iarg_empty
  52. ;
  53.  
  54. event_args : event_arg ',' event_args | event_arg ;
  55. args : arg ',' args | arg ;
  56. i_args : i_arg ',' i_args | i_arg ;
  57.  
  58. cond
  59. : '(' cond ')' #paren_cond
  60. | left=cond MULDIVMODOP right=cond #multdiv_cond
  61. | left=cond ADDSUBOP right=cond #addsub_cond
  62. | NEGOP cond #neg_cond
  63. | left=cond EQOP right=cond #eq_cond
  64. | left=cond LOGOP right=cond #log_cond
  65. | '{' cond ';' cond ';' cond '}' #coords_cond
  66. | tpe=(NUM | STR | BOOL | ITEM_ID | IDENTIFIER) #var_cond
  67. | accessing #acc_cond
  68. | func_call #func_call_cond
  69. ;
  70.  
  71. stnd_stmt
  72. : dcl #dcl_stmt
  73. | 'for' IDENTIFIER '=' init=cond way=('to' | 'downto') target=cond 'do' stmt* 'end' 'for' #for_stmt
  74. | rpt=('while' | 'until') cond 'repeat' stmt* 'end' 'repeat' #repeat_stmt
  75. | func_call #func_call_stmt
  76. | IDENTIFIER '=' cond #assign_stmt
  77. | 'return' cond #return_stmt
  78. | 'break' #break_stmt
  79. ;
  80.  
  81. func_call : IDENTIFIER '(' i_args ')' ;
  82. stmt : stnd_stmt | if_stmt ;
  83. else_stmt : stnd_stmt | ifelse_stmt ;
  84. eelse_stmt : else_stmt ;
  85.  
  86. if_stmt
  87. : 'if' cond 'then' stmt* 'end' 'if'
  88. | 'if' cond 'then' stmt* 'else' else_stmt* 'end' 'if'
  89. ;
  90.  
  91. ifelse_stmt
  92. : 'if' cond 'then' else_stmt*
  93. | 'if' cond 'then' else_stmt* 'else' eelse_stmt*
  94. ;
  95.  
  96. glob_var : 'global' dcl ;
  97. arr_items : cond ',' arr_items | cond ;
  98.  
  99. dcl
  100. : type IDENTIFIER '=' cond #var_dcl
  101. | type '[' ']' IDENTIFIER '=' '{' arr_items '}' #arr_dcl
  102. | type '[' ']' id=IDENTIFIER '=' val=IDENTIFIER #arr_id_dcl
  103. ;
  104.  
  105. accessing
  106. : IDENTIFIER '[' val=('X' | 'Y' | 'Z') ']' #coords_accessing
  107. | IDENTIFIER '[' val=NUM+ ']' #arr_accessing
  108. | id=IDENTIFIER '[' val=IDENTIFIER ']' #arr_id_accessing
  109. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement