home *** CD-ROM | disk | FTP | other *** search
/ cs.rhul.ac.uk / www.cs.rhul.ac.uk.zip / www.cs.rhul.ac.uk / pub / rdp / rdp_cs3460.tar / minitree.bnf < prev    next >
Text File  |  1998-05-07  |  3KB  |  81 lines

  1. (*******************************************************************************
  2. *
  3. * RDP release 1.50 by Adrian Johnstone (A.Johnstone@rhbnc.ac.uk) 20 December 1997
  4. *
  5. * minitree.bnf - a mini parser which builds an intermediate form
  6. *
  7. * This file may be freely distributed. Please mail improvements to the author.
  8. *
  9. *******************************************************************************)
  10. TITLE("Minitree compiler V1.50 (c) Adrian Johnstone 1997")
  11. SUFFIX("m")
  12. PARSER(program)
  13. USES("ml_aux.h")
  14. USES("mt_aux.h")
  15. OUTPUT_FILE("minitree.mvm")
  16. TREE
  17. POST_PARSE([* code_generate(rdp_sourcefilename, rdp_outputfilename, rdp_tree); *])
  18.  
  19. SYMBOL_TABLE(mini 101 31
  20.          symbol_compare_string
  21.          symbol_hash_string
  22.          symbol_print_string
  23.          [* char* id; *]
  24.         )
  25.  
  26. check_declared ::= [* if (symbol_lookup_key(mini, &name, NULL) == NULL)
  27.               {
  28.                 text_message(TEXT_ERROR, "Undeclared variable '%s'\n", name);
  29.                 symbol_insert_key(mini, &name, sizeof(char*), sizeof(mini_data));
  30.               }
  31.                *].
  32.  
  33. program   ::=  { [var_dec | statement] ';'^}.
  34.  
  35. var_dec ::= 'int'^^ (dec_body)@','^.
  36.  
  37. dec_body ::= ID:name^^ ['='^ e0 ]:^
  38.              [* symbol_insert_key(mini, &name, sizeof(char*), sizeof(mini_data)); 
  39.                 if (*name == '_' && *(name+1) == '_')
  40.                   text_message(TEXT_ERROR_ECHO, "variable names must not begin with two underscores\n");
  41.              *].
  42.  
  43. statement ::= ID:name check_declared '='^^ e0  |                       (* assignment *)
  44.               'if'^^ e0 'then'^ statement [ 'else'^ statement ] |   (* if statement *)
  45.               'while'^^ e0 'do'^ statement |                       (* while do statement *)
  46.               'print'^^ '('^ ( e0  | String )@','^ ')'^ |            (* print statement *)
  47.               'begin'^^ (statement)@';'^ 'end'^.                    (* compound statement *)
  48.  
  49. e0 ::= e1^^ [ '>'^^^ e1  |  (* Greater than *)
  50.               '<'^^^ e1  |  (* Less than *)
  51.               '>='^^^ e1 |  (* Greater than or equal *)
  52.               '<='^^^ e1 |  (* Less than or equal *)
  53.               '=='^^^ e1 |  (* Equal *)
  54.               '!='^^^ e1    (* Not equal *)
  55.             ] .
  56.  
  57. e1 ::= e2^^ { '+'^^^ e2  |  (* Add *)
  58.               '-'^^^ e2     (* Subtract *)
  59.           } .
  60.  
  61. e2 ::= e3^^ { '*'^^^ e3  |  (* Multiply *)
  62.               '/'^^^ e3     (* Divide *)
  63.             } .
  64.  
  65. e3 ::= e4^^ | 
  66.        '+'^ e3  |  (* Posite: note suppression from intermediate form! *)
  67.        '-'^^ e3 .  (* Negate *)
  68.              
  69.  
  70. e4 ::= e5 [ '**'^^ e4 ]:^^.
  71.  
  72. e5 ::= ID:name^^ check_declared | (* Variable access *)
  73.        INTEGER^^  |    (* Numeric literal *)
  74.        '('^ e1^^ ')'^.    (* Parenthesised expression *)
  75.  
  76. comment ::= COMMENT_NEST('(*' '*)').  (* Comments: stripped by lexer *)
  77. String^  ::= STRING_ESC('"' '\\').     (* Strings for print *)
  78.  
  79. (* End of minitree.bnf *)
  80.  
  81.