Omccp: a Metamodelica Based Parser Generator Applied to Modelica
Total Page:16
File Type:pdf, Size:1020Kb
Institutionen f¨orDatavetenskap Department of Computer and Information Science Master's thesis OMCCp: A MetaModelica Based Parser Generator Applied to Modelica by Edgar Alonso Lopez-Rojas LIU-IDA/LITH-EX-A{11/019{SE 2011-05-31 ' $ & Link¨opingsuniversitet Link¨opingsuniversitet % SE-581 83 Link¨oping, Sweden 581 83 Link¨oping Institutionen f¨orDatavetenskap Department of Computer and Information Science Master's thesis OMCCp: A MetaModelica Based Parser Generator Applied to Modelica by Edgar Alonso Lopez-Rojas LIU-IDA/LITH-EX-A{11/019{SE 2011-05-31 Supervisors: Martin Sj¨olundand Mohsen Torabzadeh-Tari Dept. of Computer and Information Science Examiner: Prof. Peter Fritzson Dept. of Computer and Information Science Upphovsr¨att Detta dokument h˚allstillg¨angligtp˚aInternet ^aeller dess framtida ers¨attare ^a under en l¨angre tid fr˚anpubliceringsdatum under f¨oruts¨attningatt inga extra-ordin¨araomst¨andigheteruppst˚ar. Tillg˚angtill dokumentet inneb¨artillst˚andf¨orvar och en att l¨asa, ladda ner, skriva ut enstaka kopior f¨orenskilt bruk och att anv¨anda det of¨or¨andratf¨orickekommersiell forskning och f¨orundervisning. ¨overf¨oringav upphovsr¨atten vid en senare tidpunkt kan inte upph¨ava detta tillst˚and. All annan anv¨andningav dokumentet kr¨aver up- phovsmannens medgivande. F¨oratt garantera ¨aktheten,s¨akerheten och tillg¨anglighetenfinns det l¨osningarav teknisk och administrativ art. Upphovsmannens ideella r¨attinnefattar r¨attatt bli n¨amndsom up- phovsman i den omfattning som god sed kr¨aver vid anv¨andningav dokumentet p˚aovan beskrivna s¨attsamt skydd mot att dokumentet ¨andraseller presenteras i s˚adanform eller i s˚adant sammanhang som ¨arkr¨ankande f¨orupphovsmannens litter¨araeller konstn¨arliga anseende eller egenart. F¨or ytterligare information om Link¨oping University Electronic Press se f¨orlagetshemsida http://www.ep.liu.se/ Copyright The publishers will keep this document online on the Internet - or its possible replacement - for a considerable time from the date of publication barring exceptional circumstances. The online availability of the document implies a permanent per- mission for anyone to read, to download, to print out single copies for your own use and to use it unchanged for any non-commercial research and educational purpose. Subsequent transfers of copyright cannot revoke this permission. All other uses of the document are conditional on the consent of the copyright owner. The publisher has taken technical and administrative measures to assure authenticity, security and accessibility. According to intellectual property law the author has the right to be mentioned when his/her work is accessed as described above and to be protected against infringement. For additional information about the Link¨opingUniversity Elec- tronic Press and its procedures for publication and for assurance of document integrity, please refer to its WWW home page: http://www.ep.liu.se/ c Edgar Alonso Lopez-Rojas To Isabella, my new project in life Abstract The OpenModelica Compiler-Compiler parser generator (OMCCp) is an LALR(1) parser generator implemented in the MetaModelica language with parsing tables generated by the tools Flex and GNU Bison. The code gener- ated for the parser is in MetaModelica 2.0 language which is the OpenMod- elica compiler implementation language and is an extension of the Modelica 3.2 language. OMCCp uses as input an LALR(1) grammar that specifies the Modelica language. The generated Parser can be used inside the OpenMod- elica Compiler (OMC) as a replacement for the current parser generated by the tool ANTLR from an LL(k) Modelica grammar. This report explains the design and implementation of this novel Lexer and Parser Generator called OMCCp. Modelica and its extension MetaModelica are both languages used in the OpenModelica environment. Modelica is an Object-Oriented Equation- Based language for Modeling and Simulation. v vi Acknowledgements It is an honor for me to be able to culminate this work with the guidance of remarkable computer scientists. This thesis would not have been possible unless the clear vision of my examiner, professor Peter Fritzson. As the director of the Open Source Modelica Consortium (OSMC) he presented this great opportunity to me. Together with him, I have to thank my supervisors Martin Sj¨olundand Mohsen Torabzadeh-Tari. Martin has made available his support and guidance in a number of ways that I cannot count and Mohsen has always been keeping track of my progress and helping me with the difficulties I found. I am pleased to be part, learn and contribute to this great open source project called OpenModelica. Nevertheless, To IDA (Department of Computer and Information Sci- ence) for offering its locations and resources for my daily work. I cannot forget to thank my family. My parents Jesus and Soledad for supporting me since the beginning in this project to become a Master in Computer Science. My fianc´ee Helena, who has all the time been encour- aging me to give my best in every step of this journey. I am delighted to include my future daughter Isabella here; who is been my biggest motivation to complete this work before the day she step for the first time in this world. Last, but not less important my financial sponsors from Colombia: Fun- dacion Colfuturo1 and EAFIT University2. They believed in my talent and provided the financial resources to achieve this goal. 1http://www.colfuturo.org/ 2http://www.eafit.edu.co/ vii viii Contents 1 Introduction 1 1.1 Background . .1 1.2 Project Goal . .2 1.3 Methodology . .2 1.4 Intended Readers . .3 1.5 Thesis Outline . .3 2 Theoretical Background 5 2.1 Compilers . .5 2.1.1 Fundamentals . .6 2.1.2 Lexical Analysis . .8 2.1.3 Syntax Analysis . 10 2.1.4 Parser LALR(1) . 13 2.2 Error Handling in Syntax Analysis . 15 2.2.1 Error Recovery . 16 2.2.2 Error Messages . 17 2.3 The OpenModelica Project . 17 2.3.1 The Modelica Language . 18 2.3.2 MetaModelica extension . 18 2.3.3 Abstract Syntax Tree - AST . 21 3 Existing Technologies 23 3.1 OpenModelica Compiler (OMC) . 23 3.1.1 Architecture and Components . 23 3.1.2 ANTLR . 24 3.1.3 Current state . 26 3.2 Flex . 27 3.2.1 Input file lexer.l . 27 3.2.2 Output file lexer.c . 27 3.3 GNU Bison . 28 3.3.1 Input file parser.y . 29 3.3.2 Output file parser.c . 29 ix x CONTENTS 4 Implementation 33 4.1 Proposed Solution . 33 4.2 OMCCp Design . 34 4.2.1 Lexical Analyser . 35 4.2.2 Syntax Analyser . 39 4.3 OpenModelica Compiler-Compiler Parser (OMCCp) . 44 4.3.1 Lexer Generator . 44 4.3.2 Parser Generator . 46 4.4 Error handling . 49 4.4.1 Error recovery . 49 4.4.2 Error messages . 50 4.5 Integration OMC . 54 5 Discussion 57 5.1 Analysis of Results . 57 5.1.1 Lexer and Parser . 57 5.1.2 OMCCp Construction . 58 5.1.3 Implementation of a subset of Modelica and Meta- Modelica grammar . 61 5.2 OpenModelica Compiler . 64 5.3 Limitations . 64 6 Related Work 66 6.1 OpenModelica Development . 66 6.2 Compiler-Compiler Construction . 67 7 Conclusions 69 7.1 Accomplishments . 69 7.2 Future Work . 70 Bibliography 73 Appendices 80 A OMC Compiler Commands 80 A.1 Parameters - MetaModelica Parser Generator . 80 A.1.1 Generate compilerName . 80 A.1.2 Run compilerName, fileName . 80 A.2 OMC Commands . 80 B Lexer Generator 83 B.1 Lexer.mo . 83 B.2 LexerGenerator.mo . 92 B.3 LexerCode.tmo . 100 B.4 Types.mo . 102 CONTENTS xi C Parser Generator 107 C.1 Parser.mo . 107 C.2 ParserGenerator.mo . 126 C.3 ParseCode.tmo . 143 D Sample Input 146 D.1 lexer10.l . 146 D.2 parser10.y . 147 E Sample Output 152 E.1 ParseTable10.mo . 152 E.2 ParseCode10.mo . 157 E.3 Token10.mo . 168 E.4 LexTable10.mo . 168 E.5 LexerCode10.mo . 171 F Modelica Grammar 176 F.1 lexerModelica.l . 176 F.2 parserModelica.y . 180 G Additional Files 205 G.1 SCRIPT.mos . 205 G.2 Main.mo . 206 Glossary 209 Acronyms 211 xii CONTENTS List of Figures 2.1 Compiler Phases . .6 2.2 Compiler Front-End . .8 2.3 Parser components . 12 2.4 OpenModelica Environment [Fritzson et al., 2009] . 18 3.1 OMC simplified overall structure [Fritzson et al., 2009] . 24 3.2 OMC Language Grammars . 24 4.1 OMCCp (OpenModelica Compiler - Compiler) Lexer and Parser Generator . 34 4.2 OMCCp Lexer and Parser Generator Architecture Design . 36 4.3 OMC-Lexer design . 37 4.4 OMC-Parser design . 39 4.5 OMC-Parser LALR(1) . 40 5.1 OMCCp - Time Parsing . 63 xiii xiv LIST OF FIGURES List of Tables 2.1 LR(1) parsing table [Aho et al., 2006] . 14 2.2 LR(1) parsing table rearranged [Aho et al., 2006] . 14 2.3 LALR(1) parsing table [Aho et al., 2006] . 15 5.1 OMCCp Files Implementation . 60 5.2 Test Suite - Compiler . 63 5.3 OMCCp - Time Parsing . 63 xv xvi LIST OF TABLES Listings 2.1 MetaModelica uniontype . 19 2.2 MetaModelica matchcontinue . 20 2.3 MetaModelica list . 20 3.1 ANTLR grammar file structure . 25 3.2 Flex file structure . 27 3.3 Bison file structure . 29 4.1 Lexer.mo function scan . 37 4.2 Parser.mo function parse . 41 4.3 MultiTypedStack AstStack . 43 4.4 ParseCode.mo case reduce action . 43 4.5 ParseCode.mo function getAST . 44 4.6 Modifications in the Bison Epilogue . 46 4.7 Modifications in the Rules section in Bison . 47 4.8 List of semantic values of tokens .