Related articles |
---|
Simula polymorphism and Cim "virtual"-bug workaround janis_papanagnou@hotmail.com (2003-08-04) |
From: | janis_papanagnou@hotmail.com (Janis) |
Newsgroups: | comp.compilers |
Date: | 4 Aug 2003 00:10:42 -0400 |
Organization: | http://groups.google.com/ |
Keywords: | OOP |
Posted-Date: | 04 Aug 2003 00:10:42 EDT |
Simula polymorphism and Cim "virtual"-bug workaround
OO without polymorphism has only limited applications. The
language Simula has the virtual keyword to achieve polymorphism. The
cim compiler from University of Oslo supports "virtual", but
unfortunately it does not work properly and results in an compile
error when using it in conformance with the syntax defined by the
Simula standard. This posting presents a simple - though non-standard
- syntactic workaround for use with cim. (*)
The yacc grammar defined in the cim package for Simula language seems
to define the virtual construct properly. The standard conforming syntax
is:
CLASS Example;
VIRTUAL:
PROCEDURE print; (+)
INTEGER PROCEDURE size;
BEGIN
...
END;
Compiling such code using cim results in an "Parse error." at the line
marked with (+).
The problem seems to be a too complex grammar for yacc to be defined
without glitches. Though the compilation of the grammar does not show
any problem, somehow the relevant grammer alternatives are silenty not
accepted or ignored. (I don't know why.)
Inspecting the yacc grammar of cim seems to open an alternative syntax
for a virtual declaration:
ONE_SPEC : SPECIFIER IDENTIFIER_LIST HSTATEMENTSEPARATOR (1)
| NO_TYPE HPROCEDURE HIDENTIFIER HOBJRELOPERATOR (2)
{ if($4!=HIS) yerror (8);} (3)
PROC_DECL_IN_SPEC HSTATEMENTSEPARATOR (4)
| FPP_PROC_DECL_IN_SPEC HSTATEMENTSEPARATOR (5)
| MBEE_TYPE HPROCEDURE HIDENTIFIER HSTATEMENTSEPARATOR (6)
{ yerror (45);} (7)
| MBEE_TYPE HPROCEDURE HIDENTIFIER HPAREXPSEPARATOR (8)
IDENTIFIER_LIST HSTATEMENTSEPARATOR (9)
{ yerror (45);} (10)
While (6) resp. (8) and (9) define the non-compiling standard syntax, the
definition (2) - normally used for external declaration - is syntactically
correct with the virtual construct.
Since the code for the virtual feature is already present in cim, using
that alternative non-standard syntax could likely work. The above example
class would be defined as shown in the following code fragment:
CLASS Example;
VIRTUAL:
PROCEDURE print IS
PROCEDURE print;
;
PROCEDURE size IS
INTEGER PROCEDURE size;
;
BEGIN
...
END;
That workaround compiles with cim and shows the expected results with
polymorphism (see attached test program).
Janis Papanagnou
--
(*) Investigation and tests done using cim 3.30 on Intel/Linux 2.4
-- snip --
BEGIN
CLASS Base;
VIRTUAL:
! PROCEDURE print; ! standard ;
PROCEDURE print IS PROCEDURE print; ; ! workaround ;
BEGIN
PROCEDURE print;
BEGIN
outtext ("In Class Base"); outimage;
END;
END;
Base CLASS Derived;
BEGIN
PROCEDURE print;
BEGIN
outtext ("In Class Derived"); outimage;
END;
END;
REF (Base) b;
REF (Derived) d;
REF (Base) v;
b :- new Base;
d :- new Derived;
v :- new Derived;
b.print; ! In Class Base ;
d.print; ! In Class Derived ;
v.print; ! In Class Derived ;
END
-- snip --
Return to the
comp.compilers page.
Search the
comp.compilers archives again.