# Pascal EBNF Definition # http://www.lrz-muenchen.de/~bernhard/Pascal-EBNF.html # # This document is an Extended Backus-Naur Form definition of the # Pascal programming language; # # Notation: Square brackets [ ] delimit optional constructs; braces { } # indicate zero or more repetitions of the enclosed construct; parantheses # ( ) indicate simple grouping of constructs; a vertical bar | indicates # choice of one from many; literal text in definitions is denoted using # bold font or double quotation marks " "; # # Credits: Copyright Stephen Fitzpatrick, Department of Computer Science, # The Queen's University of Belfast, Northern Ireland. This document may # be used for non-profit purposes only; no part of this document may be # duplicated unless this copyright notice is included; # # References:I hope this definition is accurate (and would appreciate # any corrections), but it cannot be taken as being definitive. For more # information, see the ISO standard `ISO 7185:1990' or a good text book # such as "Introduction to Pascal" by Jim Welsh and John Elder, Prentice # Hall, International Series in Computer Science, ISBN 0-13-491549-6; # # Last major revision: October 1995 # Last updates: August 1996 (SF), May 2002 (BdT) # 2006-08-17 Modified by Umberto Salsi: # - hyphened words x-y-z converted to underscored x_y_z # - bolded literal text now rendered as double-quoted string # - rules ending marked by ; (semicolon) in place of . (period) # - removed spaces around "[ " and " ]" to read "[" and "]" # - list of digits "0"|"1"|"2"... converted to range "0".."9" # - list of capital letters converted to range "A".."Z" # Programs and Blocks program = program_heading block "." ; program_heading = "program" identifier "(" identifier_list ")" ";" ; block = declaration_part statement_part ; declaration_part = [ label_declaration_part ] [ constant_definition_part ] [ type_definition_part ] [ variable_declaration_part ] procedure_and_function_declaration_part ; label_declaration_part = "label" label { "," label } ";" ; constant_definition_part = "const" constant_definition ";" { constant_definition ";" } ; constant_definition = identifier "=" constant ; type_definition_part = "type" type_definition ";" { type_definition ";" } ; type_definition = identifier "=" type ; variable_declaration_part = "var" variable_declaration ";" { variable_declaration ";" } ; variable_declaration = identifier_list ":" type ; procedure_and_function_declaration_part = { (procedure_declaration | function_declaration) ";" } ; procedure_declaration = procedure_heading ";" procedure_body | procedure_heading ";" directive | procedure_identification ";" procedure_body ; procedure_body = block ; function_declaration = function_heading ";" function_body | function_heading ";" directive | function_identification ";" function_body ; function_body = block ; directive = "forward" | compiler_defined_directives ; statement_part = "begin" statement_sequence "end" ; # Procedure and Function Definitions procedure_heading = "procedure" identifier [ formal_parameter_list ] ; function_heading = "function" identifier [ formal_parameter_list ] ":" result_type ; result_type = type_identifier ; procedure_identification = "procedure" procedure_identifier ; function_identification = "function" function_identifier ; formal_parameter_list = "(" formal_parameter_section { ";" formal_parameter_section } ")" ; formal_parameter_section = value_parameter_section | variable_parameter_section | procedure_parameter_section | function_parameter_section ; value_parameter_section = identifier_list ":" parameter_type ; variable_parameter_section = "var" identifier_list ":" parameter_type ; procedure_parameter_section = procedure_heading ; function_parameter_section = function_heading ; parameter_type = type_identifier | conformant_array_schema ; conformant_array_schema = packed_conformant_array_schema | unpacked_conformant_array_schema ; packed_conformant_array_schema = "packed" "array" "[" bound_specification "]" "of" type_identifier ; unpacked_conformant_array_schema = "array" "[" bound_specification { ";" bound_specification } "]" "of" (type_identifier | conformant_array_schema) ; bound_specification = identifier ".." identifier ":" ordinal_type_identifier ; ordinal_type_identifier = type_identifier ; # Statements statement_sequence = statement { ";" statement } ; statement = [ label ":" ] (simple_statement | structured_statement) ; simple_statement = [ assignment_statement | procedure_statement | goto_statement ] ; assignment_statement = (variable | function_identifier) ":=" expression ; procedure_statement = procedure_identifier [ actual_parameter_list ] ; goto_statement = "goto" label ; structured_statement = compound_statement | repetitive_statement | conditional_statement | with_statement ; compound_statement = "begin" statement_sequence "end" ; repetitive_statement = while_statement | repeat_statement | for_statement ; while_statement = "while" expression "do" statement ; repeat_statement = "repeat" statement_sequence "until" expression ; for_statement = "for" variable_identifier ":=" initial_expression ("to" | "downto") final_expression "do" statement ; initial_expression = expression ; final_expression = expression ; conditional_statement = if_statement | case_statement ; if_statement = "if" expression "then" statement [ "else" statement ] ; case_statement = "case" expression "of" case_limb { ";" case_limb } [ ";" ] "end" ; case_limb = case_label_list ":" statement ; case_label_list = constant { "," constant } ; with_statement = "with" record_variable { "," record_variable } "do" statement ; actual_parameter_list = "(" actual_parameter { "," actual_parameter } ")" ; actual_parameter = actual_value | actual_variable | actual_procedure | actual_function ; actual_value = expression ; actual_procedure = procedure_identifier ; actual_function = function_identifier ; # Expressions expression = simple_expression [ relational_operator simple_expression ] ; simple_expression = [ sign ] term { addition_operator term } ; term = factor { multiplication_operator factor } ; factor = variable | number | string | set | "nil" | constant_identifier | bound_identifier | function_designator | "(" expression ")" | "not" factor ; relational_operator = "=" | "<>" | "<" | "<=" | ">" | ">=" | "in" ; addition_operator = "+" | "-" | "or" ; multiplication_operator = "*" | "/" | "div" | "mod" | "and" ; variable = entire_variable | component_variable | referenced_variable ; entire_variable = variable_identifier | field_identifier ; component_variable = indexed_variable | field_designator | file_buffer ; indexed_variable = array_variable "[" expression_list "]" ; field_designator = record_variable "." field_identifier ; set = "[" element_list "]" ; element_list = [ expression { "," expression } ] ; function_designator = function_identifier [ actual_parameter_list ] ; file_buffer = file_variable "^" ; # Types type = simple_type | structured_type | pointer_type | type_identifier ; simple_type = subrange_type | enumerated_type ; enumerated_type = "(" identifier_list ")" ; subrange_type = lower_bound ".." upper_bound ; lower_bound = constant ; upper_bound = constant ; structured_type = [ "packed" ] unpacked_structured_type ; unpacked_structured_type = array_type | record_type | set_type | file_type ; array_type = "array" "[" index_type { "," index_type } "]" "of" element_type ; index_type = simple_type ; element_type = type ; record_type = "record" field_list "end" ; set_type = "set" "of" base_type ; base_type = type ; file_type = "file" "of" file_component_type ; file_component_type = type ; pointer_type = "^" type_identifier ; # Record Fields field_list = [ (fixed_part [ ";" variant_part ] | variant_part) [ ";" ] ] ; fixed_part = record_section { ";" record_section } ; record_section = identifier_list ":" type ; variant_part = "case" tag_field type_identifier "of" variant { ";" variant } ; tag_field = [ identifier ":" ] ; variant = case_label_list ":" "(" field_list ")" ; # Input/Output output_list = output_value { "," output_value } ; output_value = expression [ ";" field_width [ ":" fraction_length ] ] ; field_width = expression ; fraction_length = expression ; # Variable and Identifier Categories identifier = letter { letter | digit } ; file_variable = variable ; referenced_variable = pointer_variable "^" ; record_variable = variable ; pointer_variable = variable ; actual_variable = variable ; array_variable = variable ; field_identifier = identifier ; constant_identifier = identifier ; variable_identifier = identifier ; type_identifier = identifier ; procedure_identifier = identifier ; function_identifier = identifier ; bound_identifier = identifier ; # Low Level Definitions variable_list = variable { "," variable } ":" ; identifier_list = identifier { "," identifier } ; expression_list = expression { "," expression } ; number = integer_number | real_number ; integer_number = digit_sequence ; real_number = digit_sequence "." [ unsigned_digit_sequence ] [ scale_factor ] | digit_sequence scale_factor ; scale_factor = ("E" | "e")digit_sequence ; unsigned_digit_sequence = digit { digit } ; digit_sequence = [ sign ] unsigned_digit_sequence ; sign = "+" | "-" ; letter = "A" .. "Z" ; digit = "0" .. "9" ; string = "'" string_character { string_character } "'" ; string_character = any_character_except_quote | "''" ; label = integer_number ; constant = [ sign ] (constant_identifier | number) | string ;