An Attribute- Grammar Compiling System Based on Yacc, Lex, and C Kurt M
Total Page:16
File Type:pdf, Size:1020Kb
View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Digital Repository @ Iowa State University Computer Science Technical Reports Computer Science 12-1992 Design, Implementation, Use, and Evaluation of Ox: An Attribute- Grammar Compiling System based on Yacc, Lex, and C Kurt M. Bischoff Iowa State University Follow this and additional works at: http://lib.dr.iastate.edu/cs_techreports Part of the Programming Languages and Compilers Commons Recommended Citation Bischoff, Kurt M., "Design, Implementation, Use, and Evaluation of Ox: An Attribute- Grammar Compiling System based on Yacc, Lex, and C" (1992). Computer Science Technical Reports. 23. http://lib.dr.iastate.edu/cs_techreports/23 This Article is brought to you for free and open access by the Computer Science at Iowa State University Digital Repository. It has been accepted for inclusion in Computer Science Technical Reports by an authorized administrator of Iowa State University Digital Repository. For more information, please contact [email protected]. Design, Implementation, Use, and Evaluation of Ox: An Attribute- Grammar Compiling System based on Yacc, Lex, and C Abstract Ox generalizes the function of Yacc in the way that attribute grammars generalize context-free grammars. Ordinary Yacc and Lex specifications may be augmented with definitions of synthesized and inherited attributes written in C syntax. From these specifications, Ox generates a program that builds and decorates attributed parse trees. Ox accepts a most general class of attribute grammars. The user may specify postdecoration traversals for easy ordering of side effects such as code generation. Ox handles the tedious and error-prone details of writing code for parse-tree management, so its use eases problems of security and maintainability associated with that aspect of translator development. The translators generated by Ox use internal memory management that is often much faster than the common technique of calling malloc once for each parse-tree node. Ox is a Yacc/Lex/C preprocessor, and is designed to bring attribute grammars closer to the mainstream of Unix-based language development. Ox inherits all of the familiar syntax and semantics of Yacc, Lex, and C. It is relatively easy to convert programs between Ox code and "pure Yacc/Lex/C" code. Ox has been used to build a compiler for a small (eighty grammar rules) block-structured imperative programming language. This paper considers Ox's design, implementation, and use, evaluates the performance of Ox-generated evaluators, and makes recommendations for improvements in Ox. Disciplines Programming Languages and Compilers This article is available at Iowa State University Digital Repository: http://lib.dr.iastate.edu/cs_techreports/23 Design, Implementation, Use, and Evaluation of Ox:AnAttribute-Grammar Compiling System based on Yacc, Lex, and C by Kurt M. Bischo TR92-31 Decemb er, 1992 Department of Computer Science 226 Atanaso Hall Iowa State University Ames, Iowa 50011 c 1992 Kurt M. Bischo Contents 1 General Intro duction and User Manual 5 1.1 Overview of Use :: :: :: ::: :: :: :: :: ::: :: :: :: 5 1.2 Preliminary :: :: :: :: ::: :: :: :: :: ::: :: :: :: 6 1.3 Attribute Declarations :: ::: :: :: :: :: ::: :: :: :: 7 1.3.1 Semantics of Attribute Declarations :: ::: :: :: :: 8 1.4 Rules and Attribute Occurrences : :: :: :: ::: :: :: :: 8 1.5 Ox Attribute De nitions : ::: :: :: :: :: ::: :: :: :: 9 1.5.1 Inherited vs. Synthesized Attributes : ::: :: :: :: 9 1.5.2 Attribute Reference Sections in the Y- le :: :: :: :: 10 1.5.3 Attribute Reference Sections in the L- les :: :: :: 13 1.5.4 Cycles : :: :: :: ::: :: :: :: :: ::: :: :: :: 14 1.6 Translation into C Co de : ::: :: :: :: :: ::: :: :: :: 14 1.7 Temp oral Behavior of the Ox-generated Evaluator ::::::: 15 1.7.1 Stack Op erations : ::: :: :: :: :: ::: :: :: :: 15 1.7.2 Placement of Generated Co de : :: :: ::: :: :: :: 15 1.7.3 Decoration and the Ready Set : :: :: ::: :: :: :: 16 1.8 Programming Style :: :: ::: :: :: :: :: ::: :: :: :: 17 1.9 Postdecoration Traversals ::: :: :: :: :: ::: :: :: :: 17 1.9.1 Example: In x to Pre x Translation : ::: :: :: :: 18 1.9.2 General Description ::: :: :: :: :: ::: :: :: :: 20 1.10 Macros ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: 24 1.10.1 Macro De nitions : ::: :: :: :: :: ::: :: :: :: 24 1.10.2 Macro Uses :: :: ::: :: :: :: :: ::: :: :: :: 25 1.10.3 Example :: :: :: ::: :: :: :: :: ::: :: :: :: 25 1.11 Shell Command Sequences for Evaluator Development ::::: 26 1.11.1 Conventions of Naming Ox Output Files :: :: :: :: 26 1 1.11.2 Review: Combining the Outputs of Yacc and Lex :: 26 1.11.3 Combined Use of Ox, Yacc, and Lex ::: :: :: :: 27 1.11.4 Typical Command Sequences : :: :: ::: :: :: :: 27 1.12 Other Points and Features ::: :: :: :: :: ::: :: :: :: 28 1.12.1 Error Recovery :: ::: :: :: :: :: ::: :: :: :: 28 1.12.2 Memory Alignment ::: :: :: :: :: ::: :: :: :: 28 1.12.3 Stripping Ox Constructs :: :: :: :: ::: :: :: :: 28 1.12.4 Preventing Execution of Attribute De nition Co de :: 29 1.12.5 Control of Storage Allo cation in the Generated Evaluator 29 1.12.6 Parse Tree Statistics :: :: :: :: :: ::: :: :: :: 30 1.12.7 Adjusting the Sizes of Ox's Data Structures :: :: :: 31 1.13 Example: An Integer Calculator :: :: :: :: ::: :: :: :: 31 1.14 Example: A Binary Number Translator :: :: ::: :: :: :: 33 1.15 Example: Translation from In x to Post x and Pre x ::::: 37 2 General Design Criteria and Comparison with Other Sys- tems 40 2.1 Generalization of the Function of Yacc : :: ::: :: :: :: 40 2.2 Generality of the Class of Attribute Grammars Accepted ::: 41 2.3 Ox as a Prepro cessor : :: ::: :: :: :: :: ::: :: :: :: 41 2.4 Portability : :: :: :: :: ::: :: :: :: :: ::: :: :: :: 41 2.5 Comp eting Systems :: :: ::: :: :: :: :: ::: :: :: :: 42 2.5.1 The Delft System : ::: :: :: :: :: ::: :: :: :: 42 2.6 Performance :: :: :: :: ::: :: :: :: :: ::: :: :: :: 42 3 Design of the Ox-generated Evaluator 44 3.1 Top ological Sort Using Dep endee Counts : :: ::: :: :: :: 44 3.2 Static Phase of the Top ological Sort :: :: :: ::: :: :: :: 45 3.3 Parse-Tree No des : :: :: ::: :: :: :: :: ::: :: :: :: 47 3.4 Items on the Parse-Tree Stack :::::::::::::::::: 48 3.5 Addressing Attribute Instances and Dep endee Counts ::::: 48 3.6 Static Data Structures of the Generated Evaluator ::::::: 49 3.6.1 Table of Dep endee-Count-Initialization Lists :: :: :: 49 3.6.2 Table of Decoration-time-executed Co de Fragments :: 49 3.6.3 Table of Lists of Dep endents :: :: :: ::: :: :: :: 49 3.7 Solved Instances Belonging to Parentless Nonterminal No des : 50 3.8 Op erations on the Parse-Tree Stack :: :: :: ::: :: :: :: 50 2 3.8.1 Synchronization of Stacks : :: :: :: ::: :: :: :: 50 3.8.2 Bu ering of Images of New Terminal No des: Lo okahead 51 3.8.3 Creation of Terminal No des: Shifting : ::: :: :: :: 51 3.8.4 Creation of Nonterminal No des: Reduction ::::::: 51 3.9 Dynamic Phases of the Top ological Sort : :: ::: :: :: :: 52 3.9.1 Decoration : :: :: ::: :: :: :: :: ::: :: :: :: 52 3.10 Stack Implementation of the Ready Set :: :: ::: :: :: :: 53 3.11 Storage ManagementScheme :::::::::::::::::: 54 3.11.1 Parse-Tree Storage ::: :: :: :: :: ::: :: :: :: 54 3.11.2 Dep endee-Count Storage :: :: :: :: ::: :: :: :: 55 3.11.3 Pruning :: :: :: ::: :: :: :: :: ::: :: :: :: 55 3.11.4 Example: Stack Op erations and Pruning :: :: :: :: 56 4 Design and Implementation of the Evaluator Generator 59 4.1 Size of Source Co de :: :: ::: :: :: :: :: ::: :: :: :: 59 4.2 Lexical Analysis :: :: :: ::: :: :: :: :: ::: :: :: :: 59 4.2.1 Limits of Lex :: ::: :: :: :: :: ::: :: :: :: 59 4.2.2 Use of Several Scanners :::::::::::::::::: 60 4.2.3 Macro Pro cessing : ::: :: :: :: :: ::: :: :: :: 61 4.3 Syntax Analysis :: :: :: ::: :: :: :: :: ::: :: :: :: 61 4.4 Semantic Analysis : :: :: ::: :: :: :: :: ::: :: :: :: 61 4.4.1 Top ological Sort :: ::: :: :: :: :: ::: :: :: :: 62 4.4.2 Pruning Conditions ::: :: :: :: :: ::: :: :: :: 62 4.5 Co de Generation : :: :: ::: :: :: :: :: ::: :: :: :: 64 4.6 Validation of Ox : :: :: ::: :: :: :: :: ::: :: :: :: 64 5 Exp erience with Ox 65 5.1 Small Languages : :: :: ::: :: :: :: :: ::: :: :: :: 65 5.2 GPPL: A Blo ck-structured Imp erative Programming Language 65 5.2.1 General Description ::: :: :: :: :: ::: :: :: :: 65 5.2.2 Implementation: gc :: :: :: :: :: ::: :: :: :: 66 5.2.3 Performance of gc ::: :: :: :: :: ::: :: :: :: 67 5.2.4 Remark :: :: :: ::: :: :: :: :: ::: :: :: :: 70 6 Future Work 71 6.1 Easy Improvements in Storage Eciency : :: ::: :: :: :: 71 6.1.1 Dep endee Counts as Unsigned Nybbles ::: :: :: :: 71 3 6.1.2 Eliminating Attribute-less Leaf No des : ::: :: :: :: 72 6.1.3 Removing Indirection from Parse-Tree No des :: :: :: 72 6.1.4 Calculation of Space Savings :: :: :: ::: :: :: :: 73 6.2 Grammar-wide Semantic Analysis : :: :: :: ::: :: :: :: 74 6.2.1 Static CircularityTest :::::::::::::::::: 74 6.2.2 Solving More Instances at Reduction Time ::::::: 74 6.2.3 Global Static Top ological Sorting : :: ::: :: :: :: 74 6.3 Extensions to Ox Syntax and Semantics : :: ::: :: :: :: 75 6.3.1 Dep endees for Synthesized Occurrences of Tokens ::: 75 6.3.2 L- les not Based on Lex :: :: :: ::: :: :: :: 76 6.3.3 Defaults for Attribute De nitions : :: ::: :: :: :: 76 6.3.4 Automatic Construction of Syntax Trees :: :: :: :: 76 6.3.5 Traversals at Reduction Time : :: :: ::: :: :: :: 77 6.3.6 Rep etition of Traversals :: :: :: :: ::: :: :: :: 77 6.4 Facility for Interactive Debugging : :: :: :: ::: :: :: :: 77 6.5 Supp ort for Other Languages ::::::::::::::::::