eiffel.y

The geyacc grammar provided here is a possible implementation for the Eiffel syntax. It is given as an example for a better understanding of the Eiffel syntax specification but is not complete in the sense that it does not generate any abstract syntax tree. The lexical analyzer description associated with this grammar is also available.


%{
indexing

	description:

		"Eiffel parsers"

	author:     "Eric Bezault <ericb@gobosoft.com>"
	copyright:  "Copyright (c) 1997, Eric Bezault"

class EIFFEL_PARSER

inherit

	YY_PARSER_SKELETON [STRING]
		rename
			make as make_parser_skeleton
		end

	EIFFEL_SCANNER
		rename
			make as make_eiffel_scanner
		end

creation

	make

%}

%token E_CHARACTER E_INTEGER E_REAL E_IDENTIFIER E_STRING E_BIT E_BITTYPE
%token E_BANGBANG E_ARROW E_DOTDOT E_LARRAY E_RARRAY E_ASSIGN E_REVERSE
%token E_ALIAS E_ALL E_AS E_CHECK E_CLASS E_CREATION E_DEBUG E_DEFERRED
%token E_DO E_ELSE E_ELSEIF E_END E_ENSURE E_EXPANDED E_EXPORT
%token E_EXTERNAL E_FALSE E_FEATURE E_FROM E_FROZEN E_IF E_INDEXING
%token E_INFIX E_INHERIT E_INSPECT E_INVARIANT E_IS E_LIKE E_LOCAL
%token E_LOOP E_OBSOLETE E_ONCE E_PREFIX E_REDEFINE E_RENAME E_REQUIRE
%token E_RESCUE E_RETRY E_SELECT E_SEPARATE E_STRIP E_THEN E_TRUE
%token E_UNDEFINE E_UNIQUE E_UNTIL E_VARIANT E_WHEN E_CURRENT E_RESULT
%token E_PRECURSOR

%left E_IMPLIES
%left E_OR E_XOR
%left E_AND
%left '=' E_NE '<' '>' E_LE E_GE
%left '+' '-'
%left '*' '/' E_DIV E_MOD
%right '^'
%left E_FREEOP
%right E_NOT E_OLD

%expect 354
%start Class_declaration

%%
--------------------------------------------------------------------------------

Class_declaration: Indexing_opt Class_header Formal_generics_opt Obsolete_opt
		Inheritance_opt Creators_opt Features_opt Invariant_opt E_END
	;

		-- Note: Does not support the ending comment.

--------------------------------------------------------------------------------

Indexing_opt: -- /* empty */
	| E_INDEXING Index_list
	;

Index_list: -- /* empty */
	| Index_clause
	| Index_list Index_clause
	| Index_list ';' Index_clause
	;

Index_clause: Index_terms
	| E_IDENTIFIER ':' Index_terms
	;

Index_terms: Index_value
	| Index_terms ',' Index_value
	;

Index_value: E_IDENTIFIER
	| Manifest_constant
	;

--------------------------------------------------------------------------------

Class_header: Header_mark_opt E_CLASS E_IDENTIFIER
	;

Header_mark_opt: -- /* empty */
	| E_DEFERRED
	| E_EXPANDED
	| E_SEPARATE
	;

--------------------------------------------------------------------------------

Formal_generics_opt: -- /* empty */
	| '[' Formal_generic_list ']'
	;

Formal_generic_list: -- /* empty */
	| E_IDENTIFIER Constraint_opt
	| Formal_generic_list ',' E_IDENTIFIER Constraint_opt
	;

Constraint_opt: -- /* empty */
	| E_ARROW Class_type
	;

--------------------------------------------------------------------------------

Obsolete_opt: -- /* empty */
	| E_OBSOLETE E_STRING
	;

--------------------------------------------------------------------------------

Inheritance_opt: -- /* empty */
	| E_INHERIT Parent_list
	;

Parent_list: -- /* empty */
	| Parent
	| Parent_list Parent
	| Parent_list ';' Parent
	;

Parent: Class_type Feature_adaptation_opt
	;

Feature_adaptation_opt: -- /* empty */
	| Feature_adaptation1
	| Feature_adaptation2
	| Feature_adaptation3
	| Feature_adaptation4
	| Feature_adaptation5
	;

		-- Note: This is not standard Eiffel but it has
		-- the advantage of making the grammar LR (1).

Feature_adaptation1: Rename New_exports_opt Undefine_opt Redefine_opt
		Select_opt E_END
	;

Feature_adaptation2: New_exports Undefine_opt Redefine_opt
		Select_opt E_END
	;

Feature_adaptation3: Undefine Redefine_opt Select_opt E_END
	;

Feature_adaptation4: Redefine Select_opt E_END
	;

Feature_adaptation5: Select E_END
	;

--------------------------------------------------------------------------------

Rename: E_RENAME Rename_list
	;

Rename_list: -- /* empty */
	| Feature_name E_AS Feature_name
	| Rename_list ',' Feature_name E_AS Feature_name
	;

--------------------------------------------------------------------------------

New_exports: E_EXPORT New_export_list
	;

New_exports_opt: -- /* empty */
	| New_exports
	;

New_export_list: -- /* empty */
	| New_export_item
	| New_export_list New_export_item
	| New_export_list ';' New_export_item
	;

New_export_item: Clients Feature_set
	;

Feature_set: Feature_list
	| E_ALL
	;

Feature_list: -- /* empty */
	| Feature_name
	| Feature_list ',' Feature_name
	;

--------------------------------------------------------------------------------

Clients: '{' Class_list '}'
	;

Clients_opt: -- /* empty */
	| Clients
	;

Class_list: -- /* empty */
	| E_IDENTIFIER
	| Class_list ',' E_IDENTIFIER
	;

--------------------------------------------------------------------------------

Redefine: E_REDEFINE Feature_list
	;

Redefine_opt: -- /* empty */
	| Redefine
	;

Undefine: E_UNDEFINE Feature_list
	;

Undefine_opt: -- /* empty */
	| Undefine
	;

Select: E_SELECT Feature_list
	;

Select_opt: -- /* empty */
	| Select
	;

--------------------------------------------------------------------------------

Creators_opt: -- /* empty */
	| Creation_clause
	| Creators_opt Creation_clause
	;

Creation_clause: E_CREATION Clients_opt Procedure_list
	;

		-- Note: Does not support 'Header_comment'.

Procedure_list: -- /* empty */
	| E_IDENTIFIER
	| Procedure_list ',' E_IDENTIFIER
	;

--------------------------------------------------------------------------------

Features_opt: -- /* empty */
	| Feature_clause
	| Features_opt Feature_clause
	;

Feature_clause: E_FEATURE Clients_opt Feature_declaration_list
	;

		-- Note: Does not support 'Header_comment'.

Feature_declaration_list: -- /* empty */
	| Feature_declaration
	| Feature_declaration_list Feature_declaration
	| Feature_declaration_list ';' Feature_declaration
	;

--------------------------------------------------------------------------------

Feature_declaration: New_feature_list Declaration_body
	;

Declaration_body: Formal_arguments_opt Type_mark_opt Constant_or_routine_opt
	;

Constant_or_routine_opt: -- /* empty */
	| E_IS Feature_value
	;

Feature_value: Manifest_constant
	| E_UNIQUE
	| Routine
	;

--------------------------------------------------------------------------------

New_feature_list: New_feature
	| New_feature_list ',' New_feature
	;

New_feature: Feature_name
	| E_FROZEN Feature_name
	;

--------------------------------------------------------------------------------

Feature_name: E_IDENTIFIER
	| E_PREFIX E_STRING
	| E_INFIX E_STRING
	;

		-- Note: The manifest string has still to be checked
		-- against the valid infix and prefix operators.

--------------------------------------------------------------------------------

Formal_arguments_opt: -- /* empty */
	| '(' Entity_declaration_list ')'
	;

Entity_declaration_list: -- /* empty */
	| Entity_declaration_group
	| Entity_declaration_list Entity_declaration_group
	| Entity_declaration_list ';' Entity_declaration_group
	;

Entity_declaration_group: Identifier_list ':' Type
	;

Identifier_list: E_IDENTIFIER
	| Identifier_list ',' E_IDENTIFIER
	;

Type_mark_opt: -- /* empty */
	| ':' Type
	;

--------------------------------------------------------------------------------

Routine: Obsolete_opt Precondition_opt Local_declarations_opt
		Routine_body Postcondition_opt Rescue_opt E_END
	;

		-- Note: Does not support 'Header_comment'.

--------------------------------------------------------------------------------

Routine_body: E_DEFERRED
	| E_DO Compound
	| E_ONCE Compound
	| E_EXTERNAL E_STRING External_name_opt
	;

External_name_opt: -- /* empty */
	| E_ALIAS E_STRING
	;

--------------------------------------------------------------------------------

Local_declarations_opt: -- /* empty */
	| E_LOCAL Entity_declaration_list
	;

--------------------------------------------------------------------------------

Precondition_opt: -- /* empty */
	| E_REQUIRE Assertion
	| E_REQUIRE E_ELSE Assertion
	;

Postcondition_opt: -- /* empty */
	| E_ENSURE Assertion
	| E_ENSURE E_THEN Assertion
	;

Invariant_opt: -- /* empty */
	| E_INVARIANT Assertion
	;

Assertion: -- /* empty */
	| Assertion_clause
	| Assertion Assertion_clause
	| Assertion ';' Assertion_clause
	;

Assertion_clause: Expression
	| E_IDENTIFIER ':' Expression
	;

		-- Note: Does not support 'Comment' as assertion.

--------------------------------------------------------------------------------

Rescue_opt: -- /* empty */
	| E_RESCUE Compound
	;

--------------------------------------------------------------------------------

Type: Class_type
	| E_EXPANDED Class_type
	| E_SEPARATE Class_type
	| E_LIKE E_CURRENT
	| E_LIKE E_IDENTIFIER
	| E_BITTYPE Integer_constant
	| E_BITTYPE E_IDENTIFIER
	;

Class_type: E_IDENTIFIER Actual_generics_opt
	;

Actual_generics_opt: -- /* empty */
	| '[' Type_list ']'
	;

Type_list: -- /* empty */
	| Type
	| Type_list ',' Type
	;

--------------------------------------------------------------------------------

Compound: -- /* empty */
	| Instruction
	| Compound Instruction
	;

Instruction: Creation
	| Call
	| Assignment
	| Conditional
	| Multi_branch
	| Loop
	| Debug
	| Check
	| E_RETRY
	| ';'
	;

--------------------------------------------------------------------------------

Creation: '!' Type '!' Writable Creation_call_opt
	| E_BANGBANG Writable Creation_call_opt
	;

Creation_call_opt: -- /* empty */
	| '.' E_IDENTIFIER Actuals_opt
	;

--------------------------------------------------------------------------------

Assignment: Writable Assign_op Expression
	;

Assign_op: E_ASSIGN
	| E_REVERSE
	;

--------------------------------------------------------------------------------

Conditional: E_IF Expression E_THEN Compound Elseif_list Else_part E_END
	;

Else_part: -- /* empty */
	| E_ELSE Compound
	;

Elseif_list: -- /* empty */
	| E_ELSEIF Expression E_THEN Compound
	| Elseif_list E_ELSEIF Expression E_THEN Compound
	;

--------------------------------------------------------------------------------

Multi_branch: E_INSPECT Expression When_list Else_part E_END
	;

When_list: -- /* empty */
	| E_WHEN Choices E_THEN Compound
	| When_list E_WHEN Choices E_THEN Compound
	;

Choices: -- /* empty */
	| Choice
	| Choices ',' Choice
	;

Choice: Choice_constant
	| Choice_constant E_DOTDOT Choice_constant
	;

Choice_constant: E_IDENTIFIER
	| Integer_constant
	| E_CHARACTER
	;

--------------------------------------------------------------------------------

Loop: E_FROM Compound Invariant_opt Variant_opt E_UNTIL Expression
		E_LOOP Compound E_END
	;

Variant_opt: -- /* empty */
	| E_VARIANT			-- Not standard.
	| E_VARIANT Expression
	| E_VARIANT E_IDENTIFIER ':' Expression
	;

--------------------------------------------------------------------------------

Debug: E_DEBUG Debug_keys_opt Compound E_END
	;

Debug_keys_opt: -- /* empty */
	| '(' Debug_key_list ')'
	;

Debug_key_list: -- /* empty */
	| E_STRING
	| Debug_key_list ',' E_STRING
	;

--------------------------------------------------------------------------------

Check: E_CHECK Assertion E_END
	;

--------------------------------------------------------------------------------

Call: Call_chain
	| E_RESULT '.' Call_chain
	| E_CURRENT '.' Call_chain
	| '(' Expression ')' '.' Call_chain
	| E_PRECURSOR Actuals_opt
	| E_PRECURSOR Actuals_opt '.' Call_chain
	| '{' E_IDENTIFIER '}' E_PRECURSOR Actuals_opt
	| '{' E_IDENTIFIER '}' E_PRECURSOR Actuals_opt '.' Call_chain
	;

Call_chain: E_IDENTIFIER Actuals_opt
	| Call_chain '.' E_IDENTIFIER Actuals_opt
	;

--------------------------------------------------------------------------------

Actuals_opt: -- /* empty */
	| '(' Actual_list ')'
	;

Actual_list: -- /* empty */
	| Actual
	| Actual_list ',' Actual
	;

Actual:	Expression
	| '$' Address_mark
	;

Address_mark: Feature_name
	| E_CURRENT
	| E_RESULT
	;

Writable: E_IDENTIFIER
	| E_RESULT
	;

--------------------------------------------------------------------------------

Expression: Call
	| E_RESULT
	| E_CURRENT
	| '(' Expression ')'
	| Boolean_constant
	| E_CHARACTER
	| E_INTEGER
	| E_REAL
	| E_STRING
	| E_BIT
	| E_LARRAY Expression_list E_RARRAY
	| '+' Expression %prec E_NOT
	| '-' Expression %prec E_NOT
	| E_NOT Expression
	| E_FREEOP Expression %prec E_NOT
	| Expression E_FREEOP Expression
	| Expression '+' Expression
	| Expression '-' Expression
	| Expression '*' Expression
	| Expression '/' Expression
	| Expression '^' Expression
	| Expression E_DIV Expression
	| Expression E_MOD Expression
	| Expression '=' Expression
	| Expression E_NE Expression
	| Expression '<' Expression
	| Expression '>' Expression
	| Expression E_LE Expression
	| Expression E_GE Expression
	| Expression E_AND Expression
	| Expression E_OR Expression
	| Expression E_XOR Expression
	| Expression E_AND E_THEN Expression %prec E_AND
	| Expression E_OR E_ELSE Expression %prec E_OR
	| Expression E_IMPLIES Expression
	| E_OLD Expression
	| E_STRIP '(' Attribute_list ')'
	;

Attribute_list: -- /* empty */
	| E_IDENTIFIER
	| Attribute_list ',' E_IDENTIFIER
	;

Expression_list: -- /* empty */
	| Expression
	| Expression_list ',' Expression
	;

Manifest_constant: Boolean_constant
	| E_CHARACTER
	| Integer_constant
	| Real_constant
	| E_STRING
	| E_BIT
	;

		-- Note: Does not support 'Wide_character_constant'.
		-- Note: Does not support 'Wide_manifest_string'.
		-- Note: Does not support 'Hexadecimal_constant'.

Boolean_constant: E_TRUE
	| E_FALSE
	;

Integer_constant: E_INTEGER
	| '-' E_INTEGER
	| '+' E_INTEGER
	;

Real_constant: E_REAL
	| '-' E_REAL
	| '+' E_REAL
	;

--------------------------------------------------------------------------------
%%

feature {NONE} -- Initialization

	make is
			-- Create a new Eiffel parser.
		do
			make_eiffel_scanner
			make_parser_skeleton
		end

end -- class EIFFEL_PARSER

Copyright © 1999, Eric Bezault
mailto:
ericb@gobosoft.com
http:
//www.gobosoft.com
Last Updated: 2 October 1999

HomeHome