User Manual for Ox: an Attribute-Grammar Compiling System Based on Yacc, Lex, and C Kurt M
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science Technical Reports Computer Science 12-1992 User Manual for 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., "User Manual for Ox: An Attribute-Grammar Compiling System based on Yacc, Lex, and C" (1992). Computer Science Technical Reports. 21. http://lib.dr.iastate.edu/cs_techreports/21 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]. User Manual for 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 document is the main reference for using Ox. Disciplines Programming Languages and Compilers This article is available at Iowa State University Digital Repository: http://lib.dr.iastate.edu/cs_techreports/21 User Manual for Ox:AnAttribute-Grammar Compiling System based on Yacc, Lex, and C by Kurt M. Bischo TR92-30 Decemb er, 1992 Department of Computer Science 226 Atanaso Hall Iowa State University Ames, Iowa 50011 c 1992 Kurt M. Bischo CONTENTS 1 Contents 1 Overview of Use 3 2 Preliminary 4 3 Attribute Declarations 5 3.1 Semantics of Attribute Declarations :: :: :: ::: :: :: :: 6 4 Rules and Attribute Occurrences 6 5 Ox Attribute De nitions 7 5.1 Inherited vs. Synthesized Attributes :: :: :: ::: :: :: :: 7 5.2 Attribute Reference Sections in the Y- le : :: ::: :: :: :: 8 5.2.1 Explicit Mo de : :: ::: :: :: :: :: ::: :: :: :: 8 5.2.2 Implicit Mo de : :: ::: :: :: :: :: ::: :: :: :: 10 5.2.3 Mixed Mo de :: :: ::: :: :: :: :: ::: :: :: :: 10 5.3 Attribute Reference Sections in the L- les : ::: :: :: :: 10 5.4 Cycles ::: :: :: :: :: ::: :: :: :: :: ::: :: :: :: 11 6 Translation into C Co de 12 7 Temp oral Behavior of the Ox-generated Evaluator 12 7.1 Stack Op erations : :: :: ::: :: :: :: :: ::: :: :: :: 12 7.2 Placement of Generated Co de :::::::::::::::::: 13 7.3 Decoration and the Ready Set :::::::::::::::::: 14 8 Programming Style 14 9 Postdecoration Traversals 15 9.1 Example: In x to Pre x Translation : :: :: ::: :: :: :: 15 9.2 General Description :: :: ::: :: :: :: :: ::: :: :: :: 18 9.2.1 Traversal Sp eci cations :::::::::::::::::: 18 9.2.2 Traversal Action Sp eci cations :: :: ::: :: :: :: 18 9.2.3 Traversal Semantics ::: :: :: :: :: ::: :: :: :: 20 CONTENTS 2 10 Macros 22 10.1 Macro De nitions : :: :: ::: :: :: :: :: ::: :: :: :: 22 10.2 Macro Uses :: :: :: :: ::: :: :: :: :: ::: :: :: :: 23 10.3 Example :: :: :: :: :: ::: :: :: :: :: ::: :: :: :: 23 11 Command Sequences for Evaluator Development 24 11.1 Conventions of Naming Ox Output Files : :: ::: :: :: :: 24 11.2 Review: Combining the Outputs of Yacc and Lex :: :: :: 24 11.3 Combined Use of Ox, Yacc, and Lex :: :: ::: :: :: :: 25 11.4 Typical Command Sequences :::::::::::::::::: 25 12 Other Points and Features 26 12.1 Error Recovery :: :: :: ::: :: :: :: :: ::: :: :: :: 26 12.2 Memory Alignment :: :: ::: :: :: :: :: ::: :: :: :: 26 12.3 Stripping Ox Constructs : ::: :: :: :: :: ::: :: :: :: 26 12.4 Preventing Execution of Attribute De nition Co de :: :: :: 27 12.5 Control of Storage Allo cation in the Generated Evaluator ::: 27 12.6 Parse Tree Statistics : :: ::: :: :: :: :: ::: :: :: :: 28 12.7 Adjusting the Sizes of Ox's Data Structures : ::: :: :: :: 29 13 Example: An Integer Calculator 29 14 Example: A Binary Number Translator 31 15 Example: Translation from In x to Post x and Pre x 35 1 OVERVIEW OF USE 3 1 Overview of Use Lex and Yacc are p owerful and widely-used to ols for the automatic gen- eration of language recognizers. Lex accepts a set of user-written regular expressions and writes a C program that p erforms lexical analysis accord- ing to those expressions. Yacc translates user-written grammar rules into C source co de for a syntax analyzer. While they a ord \ho oks" for execution of hand-co ded C-language semantic actions, Lex and Yacc provide little other facility for automatic implementation of language semantics. Attributed parse trees are often used as data structures in evaluators for languages. Often the language implementer hand-crafts co de for building, traversing, and evaluating attributes of parse trees, and for parse-tree-related memory management. A Yacc sp eci cation de nes a context-free language and a mapping from the set of legal sentences to the set of parse trees, but co de for parse-tree management is not generated automatically by Yacc. 1 The Ox user sp eci es a language using the familiar languages of Lex and Yacc,ortakes an existing Lex/Yacc sp eci cation, and adds semantics to the language by augmenting the sp eci cation les with declarations and de nitions of typ ed attributes of parse-tree no des. That sp eci cation constitutes an attribute grammar, and from it Ox au- tomatically generates an evaluator written in Yacc, Lex, and C.For a given input, the evaluator builds a parse tree, determines an order of evaluation for attributes of the tree, and p erforms, for each attribute, the semantic action required to evaluate it. This parse tree is managed indep endently of any trees managed by hand-written C co de, but information maybemoved b etween the evaluator-managed tree and any global data structure. Additionally, the Ox user may easily sp ecify parse-tree traversals that are p erformed after evaluation of the tree's attributes and that refer to those attributes. Such traversals greatly simplify tasks such as co de generation and the gathering of compilation statistics. The language designer is freed from the tedious and error-prone details of writing co de for parse-tree management. Ox-generated evaluators use memory-management techniques that bring large time-eciency gains over 1 The name \Ox" originated as a homophone for an acronym for \An Attribute Gram- mar Compiling System". It was noticed later that every yak is an ox and that Ox gener- alizes the function of Yacc. 2 PRELIMINARY 4 hand-built evaluators that use the common technique of calling malloc once for each parse-tree no de. Also, Ox provides securityby testing the de nition for consistency and completeness, and the Ox-generated evaluator p erforms tests to ensure that a circular de nition has not prevented evaluation of attributes. Ox is a prepro cessor that accepts two or more les, and translates these into les suitable for input to Lex and Yacc. With few exceptions, all Lex- input/Yacc-input pairs of les that constitute recognizers or translators are legal inputs to Ox.Thus much existing software is amenable to mo di cation using Ox, and implementations that use Ox may b e converted stepwise by hand to \pure" Lex/Yacc implementations. This makes Ox well-suited to language designers, exp erimenters, and implementers already familiar with Lex, Yacc, and C. 2 Preliminary It is assumed that the reader is familiar with the use of Yacc [Johnson 75 ], Lex [Lesk 75], and C [KR 88 ]; Ox syntactic constructs are describ ed mainly as augmentations of the languages accepted by those to ols. Prior acquain- tance with the basic ideas of attribute grammars for instance, as found in [Waite 84 ] or [Aho 86 ] is helpful. An Ox input sp eci cation consists of at least two les: a syntactic sp ec- i cation which resembles a Yacc input sp eci cation and is called a Y- le that Ox translates into a Yacc input sp eci cation, and one or more lex- ical sp eci cations which resemble Lex input sp eci cations and are called L- les that Ox translates into Lex input sp eci cations. Usually there is ex- actly one L- le, but an evaluator that uses more than one scanner [Lesk 75] may b e constructed by submitting to Ox more than one L- le. This man- ual presents descriptions of the Ox-sp eci c constructs that may app ear in these les, as well as p ertinent underlying concepts. These constructs are illustrated mainly by using fragments of three examples of Ox input sp eci - cations, the complete texts of which app ear in sections 13, 14 and 15. Within Ox-sp eci c constructs, C-style comments may app ear anywhere whitespace may app ear. The global identi ers of Ox-generated C co de, like those generated by Yacc and Lex, are pre xed by yy, so the Ox user can avoid name con icts in the generated evaluator by abstaining from the use 3 ATTRIBUTE DECLARATIONS 5 of global identi ers that b egin with yy.