Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BNF 2.38 KB | None | 0 0
  1. grammar lang;
  2.  
  3. // PARSER RULES
  4. program : func_body?;
  5.  
  6. return_stmt : 'return' test ';';
  7. if_stmt
  8.     : 'if' test '{' stmt* '}' ('else if' test '{' stmt* '}')* ('else' '{' stmt* '}')?
  9.     | 'if' paren_test stmt ('else if' paren_test stmt)* ('else' stmt)?
  10.     ;
  11.  
  12. param : ID (':' ID) ('=' test)?;
  13. let_params : param (',' param)*;
  14. func_body : stmt+;
  15.  
  16. func_def_stmt
  17.     : 'func' ID '(' let_params? ')' (':' ID) '{' func_body? '}'
  18.     ;
  19.  
  20. let_def_stmt
  21.     : 'pub'? ('final' | 'static')? 'let' let_params ';'
  22.     ;
  23.  
  24. func_call_stmt
  25.     : ID ('(' (arglist)? ')' | '[' test ']')
  26.     ;
  27.  
  28. stmt
  29.     : ';'
  30.     | test '=' test ';'
  31.     | test op_augassign test ';'
  32.     | test ('++' | '--') ';'
  33.     | ('++' | '--') test ';'
  34.     | if_stmt
  35.     | paren_test ';'
  36.     | test ';'
  37.     | let_def_stmt
  38.     | func_def_stmt
  39.     | func_call_stmt ';'
  40.     ;
  41.  
  42. paren_test: '(' test ')';
  43. test: or_test ('?' test ':' test)?
  44.     ;
  45. or_test: and_test ('||' and_test)*
  46.     ;
  47. and_test: not_test ('&&' not_test)*
  48.     ;
  49. not_test: '!' not_test | comparison
  50.     ;
  51. comparison: expr (op_comp expr)*
  52.     ;
  53.  
  54. op_augassign : '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=';
  55. op_comp : '<' | '>' | '<=' | '>=' | '!=' | '==' | 'is' | 'has';
  56.  
  57. expr: xor_expr ('|' xor_expr)*
  58.     ;
  59. xor_expr: and_expr ('^' and_expr)*
  60.     ;
  61. and_expr: shift_expr ('&' shift_expr)*
  62.     ;
  63. shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  64.     ;
  65. arith_expr: term (('+'|'-') term)*
  66.     ;
  67. term: factor (('*'|'/'|'%') factor)*
  68.     ;
  69. factor: ('+'|'-'|'~') factor | power
  70.     ;
  71. power: atom trailer* ('**' factor)?
  72.     ;
  73.  
  74. atom : ID | NUMBER | STRING;
  75.  
  76. trailer
  77.     : '(' (arglist)? ')' | '[' test ']' | '.' ID
  78.     ;
  79.  
  80. arglist
  81.     : test (',' test)*
  82.     ;
  83.  
  84. // LEXER RULES
  85. ID : [a-zA-Z_] [a-zA-Z0-9_]+;
  86.  
  87. STRING : '"' (ESC_SEQ | ~('\\'|'"'))* '"';
  88. CHAR : '\'' (ESC_SEQ | ~('\\'|'\'')) '\'';
  89.  
  90. NUMBER
  91.     : SIGN? [0-9]+ EXPONENT?
  92.     | SIGN? [0-9]+ '.' [0-9]+ EXPONENT? 'f'?
  93.     | SIGN? '.' [0-9]+ EXPONENT? 'f'?
  94.     | SIGN? HEX_NUMBER
  95.     ;
  96.  
  97. HEX_NUMBER
  98.     : '0' [xX] HEX+
  99.     ;
  100.  
  101. fragment
  102. HEX
  103.     : [a-fA-F0-9]
  104.     ;
  105.  
  106. EXPONENT
  107.     : [eE] SIGN? [0-9]+
  108.     ;
  109.  
  110. fragment
  111. ESC_SEQ
  112.     : '\\' ('b'|'t'|'n'|'f'|'r'|'"'|'\''|'\\')
  113.     | UNICODE_ESC
  114.     | OCTAL_ESC
  115.     ;
  116.  
  117. fragment
  118. OCTAL_ESC
  119.     : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
  120.     | '\\' ('0'..'7') ('0'..'7')
  121.     | '\\' ('0'..'7')
  122.     ;
  123.  
  124. fragment
  125. UNICODE_ESC
  126.     : '\\' 'u' HEX HEX HEX HEX
  127.     ;
  128.  
  129. WS
  130.   : [ \r\n\t] + -> skip
  131.   ;
  132.  
  133. UNKNOWN : . -> skip;
  134.  
  135. fragment
  136. SIGN
  137.     : [+-]
  138.     ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement