Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grammar algol_starter;
- options{backtrack=true;}
- program: block | compound_statement;
- empty: ;
- basic_symbol: LETTER
- | DIGIT
- | logical_value
- | delimiter;
- LETTER: 'a' .. 'z'
- | 'A' .. 'Z';
- DIGIT: '0'..'9';
- // logical_values
- logical_value: 'true' | 'false';
- // Delimiters
- delimiter: operator
- | separator
- | bracket
- | declarator
- | specificator;
- operator:
- arithmetic_operator
- | relational_operator
- | logical_operator
- | sequential_operator;
- arithmetic_operator: '+'
- | '−'
- | '×'
- | '/'
- | '÷'
- | '↑';
- logical_operator: '≡'
- | '⊃'
- | '∨'
- | '∧'
- | '¬';
- sequential_operator: 'go to'
- | 'if'
- | 'then'
- | 'else'
- | 'for'
- | 'do';
- separator: ','
- | '.'
- | '⏨'
- | ':'
- | ';'
- | ':='
- | '␣'
- | 'step'
- | 'until'
- | 'while'
- | 'comment';
- bracket: '('
- | ')'
- | '['
- | ']'
- | '`'
- | '\''
- | 'begin'
- | 'end';
- declarator: 'own'
- | 'Boolean'
- | 'integer'
- | 'real'
- | 'array'
- | 'switch'
- | 'procedure';
- specificator: 'string'
- | 'label'
- | 'value';
- /*
- ; comment <any sequence of zero or more characters not
- containing ;> ;
- equivalent to:
- ;
- begin comment <any sequence of zero or more characters
- not containing ;> ;
- equivalent to:
- begin
- */
- //Identifiers
- identifier: (LETTER) (LETTER | DIGIT)*;
- // Numbers
- unsigned_integer: DIGIT+;
- integer: unsigned_integer
- | '+' unsigned_integer
- | '−' unsigned_integer;
- decimal_fraction: '.' unsigned_integer;
- exponential_part: '⏨' integer;
- decimal_number: unsigned_integer
- | decimal_fraction
- | unsigned_integer decimal_fraction;
- unsigned_number: decimal_number
- | decimal_fraction
- | decimal_number exponential_part;
- number: unsigned_number
- | '+' unsigned_number
- | '−' unsigned_number;
- //WORD : ~('\r' | '\n' | WS | '`' | '\'')+;
- proper_string: LETTER*; //todo: put word*
- //<any sequence of characters not containing ` or '>
- open_string: proper_string
- | proper_string closed_string open_string;
- closed_string:
- '`' open_string '\'';
- string: closed_string
- | closed_string string;
- //Expressions
- expression: arithmetic_expression
- | boolean_expression
- | designational_expression;
- // Variables
- variable_identifier: identifier;
- simple_variable: variable_identifier;
- subscript_expression: arithmetic_expression;
- /*
- subscript_list: subscript_expression
- | subscript_list ',' subscript_expression;
- */
- subscript_list: subscript_expression (',' subscript_expression)*;
- array_identifier: identifier;
- subscripted_variable: array_identifier '[' subscript_list ']';
- variable: simple_variable
- | subscripted_variable;
- //Function designators
- procedure_identifier: identifier;
- actual_parameter: string
- | expression
- | array_identifier
- | switch_identifier
- | procedure_identifier;
- letter_string: LETTER+;
- parameter_delimiter: ','
- | ')' letter_string ':' '(';
- /*
- actual_parameter_list: actual_parameter
- | actual_parameter_list parameter_delimiter actual_parameter;
- */
- actual_parameter_list: actual_parameter (parameter_delimiter actual_parameter)*;
- actual_parameter_part: empty
- | '(' actual_parameter_list ')';
- function_designator: procedure_identifier actual_parameter_part;
- //Arithmetic expressions
- adding_operator: '+' | '−';
- multiplying_operator: '×'
- | '/'
- | '÷';
- primary: unsigned_number
- | variable
- | function_designator
- | '(' arithmetic_expression ')';
- /*
- factor: primary
- | factor
- | factor '↑' primary;
- */
- factor: primary ('↑' primary)*;
- /*
- term: factor
- | term multiplying_operator factor;
- */
- term: factor (multiplying_operator factor)*;
- /*
- simple_arithmetic_expression: term
- | adding_operator term
- | simple_arithmetic_expression adding_operator term;
- */
- simple_arithmetic_expression: adding_operator? term (adding_operator term)*;
- arithmetic_expression: simple_arithmetic_expression
- | if_clause simple_arithmetic_expression 'else' arithmetic_expression;
- //Boolean expressions
- relational_operator: '<'
- | '≤'
- | '='
- | '≥'
- | '>'
- | '≠';
- relation: simple_arithmetic_expression relational_operator simple_arithmetic_expression;
- boolean_primary: logical_value
- | variable
- | function_designator
- | relation
- | '(' boolean_expression ')';
- boolean_secondary: boolean_primary
- | '¬' boolean_primary;
- /*
- boolean_factor: boolean_secondary
- | boolean_factor '∧' boolean_secondary;
- */
- boolean_factor: boolean_secondary ('∧' boolean_secondary)*;
- /*
- boolean_term: boolean_factor
- | boolean_term '∨' boolean_factor;
- */
- boolean_term: boolean_factor ('∨' boolean_factor)*;
- implication: boolean_term ('⊃' boolean_term)*;
- /*
- simple_boolean: implication
- | simple_boolean '≡' implication;
- */
- simple_boolean: implication ('≡' implication)*;
- boolean_expression: simple_boolean
- | if_clause simple_boolean 'else' boolean_expression;
- //Designational expressions
- label: identifier
- | unsigned_integer;
- switch_identifier: identifier;
- switch_designator: switch_identifier '[' subscript_expression ']';
- simple_designational_expression: label
- | switch_designator
- | '(' designational_expression ')';
- designational_expression: simple_designational_expression
- | if_clause simple_designational_expression 'else' designational_expression;
- // Compound statements and blocks
- unlabelled_basic_statement: assignment_statement
- | go_to_statement
- | dummy_statement
- | procedure_statement;
- basic_statement:
- unlabelled_basic_statement
- | label ':' basic_statement;
- statement: unconditional_statement
- | conditional_statement
- | for_statement;
- compound_tail: statement 'end'
- | statement ';' compound_tail;
- /*
- block_head: 'begin' declaration
- | block_head ';' declaration;
- */
- block_head: 'begin' declaration (';' declaration)*;
- unlabelled_block: block_head ';' compound_tail;
- unlabelled_compound: 'begin' compound_tail;
- compound_statement: unlabelled_compound
- | label ':' compound_statement;
- block: unlabelled_block
- | label ':' block;
- //Assignment statements
- destination: variable
- | procedure_identifier;
- left_part: destination ':=';
- /*
- left_part_list: left_part
- | left_part_list left_part;
- */
- left_part_list: left_part+;
- assignment_statement: left_part_list arithmetic_expression
- | left_part_list boolean_expression;
- // Go to statements
- go_to_statement: 'go' 'to' designational_expression;
- // Dummy statements
- dummy_statement: empty;
- // Conditional statements
- if_clause: 'if' boolean_expression 'then';
- unconditional_statement: basic_statement
- | compound_statement
- | block;
- if_statement: if_clause unconditional_statement;
- conditional_statement: if_statement
- | if_statement 'else' statement
- | if_clause for_statement
- | block ':' conditional_statement;
- //<label> : <conditional_statement> dans BNF
- // For statements
- for_list_element: arithmetic_expression
- | arithmetic_expression 'step' arithmetic_expression 'until' arithmetic_expression
- | arithmetic_expression 'while' boolean_expression;
- /*
- for_list: for_list_element
- | for_list ',' for_list_element;
- */
- for_list: for_list_element (',' for_list_element)*;
- for_clause:
- 'for' variable ':=' for_list 'do';
- for_statement: for_clause statement
- | label ':' for_statement;
- // Procedure statements
- procedure_statement: procedure_identifier actual_parameter_part;
- // Declarations
- declaration: type_declaration
- | array_declaration
- | switch_declaration
- | procedure_declaration;
- // Type declarations
- type_list: simple_variable
- | simple_variable ',' type_list;
- type: 'real'
- | 'integer'
- | 'Boolean';
- local_or_own: empty
- | 'own';
- type_declaration: local_or_own type type_list;
- // Array declarations
- lower_bound: arithmetic_expression;
- upper_bound: arithmetic_expression;
- bound_pair: lower_bound ':' upper_bound;
- /*
- bound_pair_list: bound_pair
- | bound_pair_list ',' bound_pair;
- */
- bound_pair_list: bound_pair (',' bound_pair)*;
- array_segment: array_identifier '[' bound_pair_list ']'
- | array_identifier ',' array_segment;
- /*
- array_list: array_segment
- | array_list ',' array_segment;
- */
- array_list: array_segment (',' array_segment)*;
- array_declarer:type 'array'
- | 'array';
- array_declaration: local_or_own array_declarer array_list;
- // Switch declarations
- /*
- switch_list: designational_expression
- | switch_list ',' designational_expression;
- */
- switch_list: designational_expression (',' designational_expression)*;
- switch_declaration: 'switch' switch_identifier ':=' switch_list;
- // Procedure declarations
- formal_parameter: identifier;
- /*
- formal_parameter_list: formal_parameter
- | formal_parameter_list parameter_delimiter formal_parameter;
- */
- formal_parameter_list: formal_parameter (parameter_delimiter formal_parameter)*;
- formal_parameter_part: empty
- | '(' formal_parameter_list ')';
- /*
- identifier_list: identifier
- | identifier_list ',' identifier;
- */
- identifier_list: identifier (',' identifier)*;
- value_part: 'value' identifier_list ';'
- | empty;
- specifier: 'string'
- | type
- | array_declarer
- | 'label'
- | 'switch'
- | 'procedure'
- | type 'procedure';
- /*
- specification_part: empty
- | specifier identifier_list ';'
- | specification_part specifier identifier_list;
- */
- specification_part: (specifier identifier_list ';')? (specifier identifier_list)*;
- procedure_heading: procedure_identifier formal_parameter_part ';' value_part specification_part;
- procedure_body: statement;
- procedure_declaration: 'procedure' procedure_heading procedure_body
- | type 'procedure' procedure_heading procedure_body;
- WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ {$channel=HIDDEN;} ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement