GENERATING A MODELICA COMPILER FROM NATURAL SEMANTICS SPECIFICATIONS David Kågedal and Peter Fritzson PELAB, Dept. of Computer and Information Science Linköping University, S-58183, Linköping, Sweden davidk@lysator.liu.se petfr@ida.liu.se KEYWORDS Modelica, RML, formal semantics, simulation ABSTRACT The implementation of compilers and interpreters for non-trivial languages is a complex and error prone process, if done by hand. Therefore, formalisms and generator tools have been developed that allow automatic generation of compilers and interpreters from for- mal specifications. This offers two major advantages: High level descriptions of language properties, rather than detailed programming of the translation process High degree of correctness of generated implementations. The high level specifications are more concise and easier to read than a detailed implementation in some programming language Modelica is an object-oriented language for modeling of physical systems for the purpose of efficient simulation. The language uni- fies and generalizes previous object-oriented modeling languages. A Modelica model is defined in terms of classes containing equations and definitions. The semantics, i.e. the meaning of such a model is defined via translation of classes, instances and connec- tions into a flat set of constants, variables and equations. This paper describes and defines a formal semantics for Mod- elica expressed in a high-level specification notation called natural semantics. A compiler generation system called RML produces a a Mod- elica translator from such a language specification. The generated translator is produced in ANSI C and has comparable performance as hand-written translators. The RML tool has also been used to produce compilers for Ja- va, Pascal and few other languages. MODELICA OVERVIEW Modelica (Modelica 1998) is an object-oriented language for modeling of physical systems for the purpose of efficient simulation. The language unifies and generalizes previous object-oriented modeling languages. Compared with the widespread simulation languages available today this language offers three important advances: 1) non-causal modeling based on differential and algebraic equations; 2) multidomain modeling capability, i.e. it is possible to combine electrical, mechanical, thermodynamic, hydraulic etc. model components within the same application model; 3) a general type system that unifies object-orientation, multiple inheritance, and templates within a single class construct. A Modelica model is defined in terms of classes containing equations and definitions. The semantics, i.e. the meaning of such a model is defined via translation of classes, instances, connections and functions into a flat set of constants, variables and equations. Equations are sorted and converted to assignment statements when possible. Strongly connected sets of equations are solved by calling a symbolic and/or numeric solver. Modelica View of Object-orientation Traditional object-oriented languages like C++, Java and Simula support programming with operations on state. The state of the program includes variable values and object data, and the number of objects may change dynamically. The Modelica approach is different. The Modelica language em- phasizes structured mathematical modeling and uses the structural benefits of object orientation. A Modelica model is primarily a declarative mathematical description, which al- lows analysis and equational reasoning. For these reasons, dynamic object creation at runtime is usually not interesting from a mathematical modeling point of view, and is current- ly not supported by the Modelica language. For other reasons, and to compensate this missing feature arrays are provided by Modelica. An array is an indexed set of objects of equal type. The size of the set is determined once at runtime. This construct for example can be used to represent a set of similar rollers in a bearing, or a set of electrons around an atomic nucleus. Accepted for publication in Proceedings of The 1998 Summer Computer Simulation Conference (SCSC '98) July 19-22, 1998, Reno, Nevada