Contents | Prev | Next | Index


Language Grammar

The syntax of Modula-2 can be described using a simplified grammar notation, see section Syntax for further information on how to read it. Some language constructs are an extension to standard Modula-2 and are shown here in a red color. These extensions are usually not needed unless foreign Java classes are being imported and used.

compilation_unit ::= def_module
                 ::= imp_module
                 ::= program_module
imp_module       ::= IMPLEMENTATION program_module

program_module ::= MODULE <identifier> priority ; package
                   import_list block <identifier> . 
priority       ::= <empty>
               ::= [ const_expr ]                         
package        ::= java_package                           
               ::= <empty>
import_list    ::= import_list import
               ::= <empty>
import         ::= IMPORT item_list ;
               ::= FROM mod_id IMPORT mod_item_list ;     
item_list      ::= item_list , item
               ::= item
item           ::= <identifier>                           
               ::= FROM <identifier>                      
mod_id         ::= <identifier>                           
mod_item_list  ::= mod_item_list , <identifier>           
               ::= <identifier>                           

def_module ::= definition MODULE <identifier> ; 
               package import_list def_list 
               END <identifier> .    
definition ::= foreign DEFINITION                         
foreign    ::= java_foreign                               
           ::= <empty>
def_list   ::= def_list def
           ::= <empty>                                    
def        ::= CONST const_def_list
           ::= TYPE type_def_list
           ::= VAR var_decl_list
           ::= procedure_heading semicolon                
semicolon  ::= ;
           ::= ; java_throws                              

const_decl_list ::= const_decl_list const_decl ;
                ::= <empty>
const_def_list  ::= const_def_list const_def ;
                ::= <empty>
const_def       ::= <identifier> = const_expr             
type_def_list   ::= type_def_list type_def ;
                ::= <empty>
type_def        ::= type_decl
                ::= <identifier>                          
var_decl_list   ::= var_decl_list var_decl ;
                ::= <empty>

const_decl        ::= const_def
                  ::= const_var_decl = typed_const       
const_var_decl    ::= const_var_id : formal_type         
const_var_id      ::= <identifier>                       
typed_const       ::= const_expr                         
                  ::= pointer_const                      
                  ::= array_const                        
                  ::= record_const                       
pointer_const     ::= POINTER TO qualident               
                  ::= PROCEDURE qualident                
                  ::= ^ qualident                        
record_const      ::= ( field_const_list )
field_const_list  ::= field_const_list ; field_const
                  ::= field_const
field_const       ::= field_id : typed_const             
                  ::= <empty>
field_id          ::= <identifier>                       
array_const       ::= [ const_list ]
const_list        ::= const_list , element_const
                  ::= element_const
element_const     ::= typed_const                        

var_decl          ::= var_ident_list : type             
var_ident_list    ::= var_ident_list , var_ident        
                  ::= var_ident
var_ident         ::= <identifier>                      
                  ::= <identifier> [ <string> ]         
                  ::= <identifier> java_name            

procedure_heading ::= procedure proc_id                   
                  ::= procedure proc_id formal_parameters 
procedure         ::= modifier PROCEDURE                  
modifier          ::= java_modifiers                      
                  ::= <empty>                             
proc_id           ::= <identifier>                        
                  ::= receiver <identifier>               
                  ::= <identifier> [ <string> ]           
                  ::= receiver <identifier> [ <string> ]  
                  ::= <identifier> java_name              
                  ::= receiver <identifier> java_name     
receiver          ::= ( VAR <identifier> : <identifier> ) 
                  ::= ( <identifier> : <identifier> )     
formal_parameters ::= ( )                                 
                  ::= ( ) : qualident                     
                  ::= ( fp_section_list )                 
                  ::= ( fp_section_list ) : qualident     
fp_section_list   ::= fp_section_list ; fp_section        
                  ::= fp_section
fp_section        ::= VAR ident_list : formal_type        
                  ::= ident_list : formal_type            
formal_type       ::= qualident                           
                  ::= open_array qualident                
open_array        ::= open_array ARRAY OF                 
                  ::= ARRAY OF                            

qualident       ::= qualifier_list . <identifier>         
                ::= <identifier>                          
qualifier_list  ::= qualifier_list . qualifier            
                ::= qualifier                             
ident_list      ::= ident_list , <identifier>             
                ::= <identifier>

block          ::= decl_list END               
               ::= decl_list BEGIN stmt_seq END
decl_list      ::= decl_list decl
               ::= <empty>                                
decl           ::= CONST const_decl_list
               ::= TYPE type_decl_list
               ::= VAR var_decl_list
               ::= procedure_decl ;
               ::= module_decl ;
type_decl_list ::= type_decl_list type_decl semicolon
               ::= <empty>
type_decl      ::= type_id = type                         
type_id        ::= <identifier>                           
               ::= <identifier> java_name                 
procedure_decl ::= procedure_heading semicolon
                   block <identifier>                     
               ::= procedure_heading semicolon FORWARD    
module_decl    ::= MODULE loc_mod_id priority ;
                   import_list export block <identifier>  
loc_mod_id     ::= <identifier>                           
export         ::= EXPORT QUALIFIED ident_list ;          
               ::= EXPORT ident_list ;                    
               ::= <empty>

stmt_seq          ::= stmt_seq ; stmt         
                  ::= stmt                     
stmt              ::= assignment
                  ::= procedure_call
                  ::= if_stmt
                  ::= case_stmt
                  ::= while_stmt
                  ::= repeat_stmt
                  ::= loop_stmt
                  ::= for_stmt
                  ::= with_stmt
                  ::= EXIT                                
                  ::= return_stmt
                  ::= <empty>                             

assignment        ::= left_designator := expr         
left_designator   ::= designator                          
procedure_call    ::= proc_designator                     
                  ::= proc_designator actual_parameters   
proc_designator   ::= designator                          
actual_parameters ::= ( param_list )                      
                  ::= ( )                                 
param_list        ::= param_list , param_expr             
                  ::= param_expr                          
param_expr        ::= expr                                
if_stmt           ::= IF boolean_expr then_stmt  
                      elsif_stmt_list else_stmt END    
boolean_expr      ::= expr                                
then_stmt         ::= THEN stmt_seq                       
elsif_stmt_list   ::= elsif_stmt_list elsif_stmt
                  ::= <empty>
elsif_stmt        ::= ELSIF boolean_expr THEN stmt_seq    
else_stmt         ::= ELSE stmt_seq                       
                  ::= <empty>                             
case_stmt         ::= switch_expr case_list else_case END 
switch_expr       ::= CASE expr OF                      
case_list         ::= case_list | case                    
                  ::= case
case              ::= case_label_list : stmt_seq          
                  ::= <empty>
case_label_list   ::= case_label_list , case_labels      
                  ::= case_labels
case_labels       ::= const_expr .. const_expr            
                  ::= const_expr                          
else_case         ::= ELSE stmt_seq                    
                  ::= <empty>                             
while_stmt        ::= WHILE boolean_expr DO stmt_seq END  
repeat_stmt       ::= REPEAT stmt_seq UNTIL boolean_expr  
loop_stmt         ::= LOOP stmt_seq END                   
for_stmt          ::= FOR control_list DO stmt_seq END    
control_list      ::= control_assignment to_expr by_expr  
control_assignment ::= control_var ::= expr            
control_var       ::= <identifier>                        
to_expr           ::= TO expr                             
by_expr           ::= BY const_expr                       
                  ::= <empty>                             
with_stmt         ::= WITH rec_designator DO stmt_seq END 
rec_designator    ::= designator                          
                  ::= qualident : qualident               
return_stmt       ::= RETURN                              
                  ::= RETURN expr                         

const_expr      ::= expr                               
expr            ::= simple_expr relation simple_expr   
                ::= simple_expr
simple_expr     ::= simple_expr add_operator term      
                ::= term
                ::= unary term                         
term            ::= term mul_operator factor           
                ::= factor
factor          ::= <charcater>                        
                ::= <integer>                          
                ::= <real>                             
                ::= <string>                           
                ::= set                                
                ::= designator                         
                ::= func_designator actual_parameters  
                ::= ( expr )                
                ::= not factor                         
set             ::= set_specifier { elem_list }
                ::= set_specifier { }          
set_specifier   ::= qualident                          
                ::= <empty>                            
elem_list       ::= elem_list , elem                   
                ::= elem
elem            ::= elem_expr .. elem_expr             
                ::= elem_expr                          
elem_expr       ::= expr                               
func_designator ::= designator                         

relation           ::= =                               
                   ::= #                               
                   ::= <>                              
                   ::= <                               
                   ::= <=                              
                   ::= >                               
                   ::= >=                              
                   ::= IN                              
                   ::= IS                              
add_operator       ::= +                               
                   ::= -                               
                   ::= OR                              
                   ::= XOR                             
mul_operator       ::= *                               
                   ::= /                               
                   ::= DIV                             
                   ::= MOD                             
                   ::= &                               
                   ::= AND                             
                   ::= SHL                             
                   ::= SHR                             
not                ::= NOT
                   ::= ~
unary              ::= +                               
                   ::= -                               

designator        ::= designator_id selector_list    
                  ::= designator_id                  
designator_id     ::= qualident                      
selector_list     ::= selector_list selector         
                  ::= selector                       
selector          ::= . <identifier>                 
                  ::= [ index_list ]       
                  ::= ^                              
                  ::= ( qualident )  
index_list        ::= index_list , index             
                  ::= index
index             ::= expr                           

type             ::= simple_type
                 ::= array_type
                 ::= record_type
                 ::= set_type
                 ::= pointer_type
                 ::= procedure_type
simple_type      ::= qualident                       
                 ::= enumeration
                 ::= subrange_type
enumeration      ::= ( enum_list )                           
enum_list        ::= enum_list , <identifier>                
                 ::= <identifier>                            
subrange_type    ::= [ const_expr .. const_expr ]            
                 ::= base_type [ const_expr .. const_expr ]  
base_type        ::= qualident                               
array_type       ::= ARRAY index_type_list OF type           
index_type_list  ::= index_type_list , index_type            
                 ::= index_type
index_type       ::= simple_type                             
record_type      ::= record field_list_seq END               
                 ::= record record_base field_list_seq END   
record           ::= modifier RECORD                         
record_base      ::= ( parent interface_list )
parent           ::= qualident                               
                 ::= <empty>
interface_list   ::= interface_list , qualident              
                 ::= <empty>
field_list_seq   ::= field_list_seq ; field_list             
                 ::= field_list
field_list       ::= modifier field_id_list : type           
                 ::= CASE tag OF variant_list else_variant END
                 ::= <empty>
field_id_list    ::= field_id_list , field_name              
                 ::= field_name                              
field_name       ::= <identifier>                            
                 ::= <identifier> java_name                  
tag              ::= : qualident                             
                 ::= modifier field_name : qualident         
variant_list     ::= variant_list | variant                  
                 ::= variant
variant          ::= variant_label_list : field_list_seq     
                 ::= <empty>
variant_label_list ::= variant_label_list , variant_labels
                 ::= variant_labels
variant_labels   ::= const_expr .. const_expr                
                 ::= const_expr                              
else_variant     ::= ELSE field_list_seq       
                 ::= <empty>
set_type         ::= SET OF simple_type                      
pointer_type     ::= POINTER TO type                         
                 ::= POINTER TO open_array qualident         
procedure_type   ::= procedure formal_type_list 
                 ::= procedure                   
formal_type_list ::= ( )                                     
                 ::= ( ) : qualident                         
                 ::= ( ft_section_list )                     
                 ::= ( ft_section_list ) : qualident         
ft_section_list  ::= ft_section_list , ft_section            
                 ::= ft_section
ft_section       ::= VAR formal_type                         
                 ::= formal_type                             


The following comment-embedded compiler directives are only accepted at certain places of a Modula-2 module and are treated by the above described Modula-2 language grammar like terminal tokens. They are shown here in red.

java_foreign   ::= (*$JAVA FOREIGN *)
java_package   ::= (*$JAVA PACKAGE <string> *)
java_throws    ::= (*$JAVA THROWS exception_list *)
exception_list ::= exception_list , <identifier>
               ::= <identifier>
java_name      ::= (*$JAVA NAME <string> *)
java_modifiers ::= (*$JAVA modifier_list *)
modifier_list  ::= modifier_list modifier
               ::= modifier
modifier       ::= PUBLIC
               ::= PROTECTED
               ::= PRIVATE
               ::= ABSTRACT
               ::= STATIC
               ::= FINAL
               ::= TRANSIENT
               ::= VOLATILE
               ::= SYNCHRONIZED
               ::= NATIVE
               ::= INTERFACE
               ::= CONSTRUCTOR


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com