Syntactic Manipulation Systems for Context-Dependent Languages
Total Page:16
File Type:pdf, Size:1020Kb
SYNTACTIC MANIPULATION SYSTEMS FOR CONTEXT-DEPENDENT LANGUAGES Michael Dyck B.Sc., University of Winnipeg, 1984 THESIS SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENTS FOR THE DEGREE OF MASTER OF SCIENCE in the -School of Computing Science @ Michael Dyck 1990 SIMON FRASER UNIVERSITY August 1990 All rights reserved. This work may not be reproduced in whole or in part, by photocopy or other means, without permission of the author. APPROVAL Name: Michael Dyck Degree: Master of Science Title of thesis: Syntactic Manipulation Systems for Context-Dependent Languages Examining Committee: Chair: Dr. Binay Bhattacharya Dr. Rob Cameron Senior Supervisor ~ommittceMember Dr. Nick Cercone Committee Member Dr. Warren Burton Examiner Date Approved: 1990 August 10 PARTIAL COPYRIGHT LICENSE I hereby grant to Slmon Fraser Unlverslty the right to lend my thesis, project or extended essay (the title of which is shown below) to users of the Simon Fraser University Llbrary, and to make partial or single copies only for such users or in response to a request from the library of any other universlty, or other educational Institution, on its own behalf or for one of Its users. 1 further agree that permission for multlple copying of this work for scholarly purposes may be granted by me or the Dean of Graduate Studies. It is understood that copying or publication of this work for financial gain shall not be allowed without my written permission. T i t l e of Thes i s/Project/Extended Essay Syntactic Manipulation Systems for Context-Dependent Languages. Author : (s,t&ature) John Michael Dyck ( name (date ABSTRACT Software developers use many tools to increase their efficiency and improve their software. A syntactic manipulation system (SMS) is one such tool. An SMS is a set of programming-language routines that can be used to create, manipulate, and modify the syntactic structures defined by a grammar. An SMS is useful whenever a program manipulates data objects that are described well by a grammar; programs, structured documents, musical scores, software specifications, mathematical formulae, and recursive data structures are all objects that could be manipulated with an SMS. Previous research has established a method for deriving an SMS from a context-free grammar. Thus, this method produces SMSs that deal abstractly only with those aspects of languages that are captured by context-free grammars. However, experience shows that programs using such SMSs routinely require more complex, context-dependent information. For example, if a program were using an SMS to manipulate Pascal code, it might need answers to the following questions: "What is the type of this variable?" "Has NEW been redefined for this scope?" "Will this identifier conflict with any existing identifiers?" "Where is the resolution for this forward declaration?" "Is this argument compatible with that parameter?" "What is the value of this constant-expression?" 0 "Where are all the calls to this procedure?" In augmenting context-free SMSs with routines to answer such questions, previous approaches have been ad hoc, with neither a formal strategy to determine what routines should be added, nor a method for deriving them from a description of the target-language. This thesis provides a systematic approach to replace the previous ad hoc approaches. Specifically, I have devised NURN, a notation for specifying context-dependent languages in terms of relations between the nodes of context-free abstract syntax-trees. Moreover, I have implemented Ginger, a system that takes a NURN grammar and generates the corresponding context-dependent SMS. To demonstrate the usefulness of NURN and Ginger, I have used NURN to fully define the syntax of Standard Pascal, and written a Pascal syntax-checker using the resulting SMS. iii ACKNOWLEDGEMENTS Thanks: 0 To my Senior Supervisor Rob Cameron, for patience, availability, and insight, for dealing with the red tape, and for inventing GRAMPS, without which I might never have developed NURN. 0 To the other members of my Examining Committee (Lou Hafer, Nick Cercone and Warren Burton), for their time and effort in reading my thesis. 0 To Howard Hamilton in large quantities, the best editor a grad student could want. 0 To Ed Merks for technical discussions, to Cathy Levinson for cheerful legwork, and to both for their comments on the thesis, but especially for their help in preparing my seminar. 0 To Brian Terry, for developing GRAFS and entrusting it to my not-so-tender care. 0 To Yvonne Coady, for relentless encouragement and the occasional free lunch. 0 To Ranabir Gupta, Pat Pattabhiraman, and Fred Popowich, for various consultations. To the Natural Sciences and Engineering Research Council of Canada, the Province of British Columbia, Simon Fraser University, Rob Cameron (via the SFU Centre for Systems Science), and my dad Dave Dyck, for financial support during my career as a Masters student. Thanks again everyone! TABLE OF CONTENTS Approval .......................................................................................................................................................................... ii Abstract ........................................................................................................................................................................... iii Acknowledgements ........................................................................................................................................................ iv 1. Introduction .......................................................................................................................................................... 1 1.1 Problem Statement ............................................................................................................................ 1 1.2 Overview of this Thesis .................................................................................................................... 2 2. Context-Free Syntactic Manipulation Systems ............................................................................................. 3 2.1 GRAMPS-Style Grammars ............................................................................................................ 3 2.1.1 Construction rules ........................................................................................................6 2.1.2 Repetition rules ............................................................................................................. 7 2.1.3 Alternation rules ........................................................................................................... 8 2.1.4 Odds and Ends ..............................................................................................................9 2.2 GRAMPS-Style SMSs .....................................................................................................................9 2.2.1 Overall organization ................................................................................................... 10 2.2.2 The GRAMPS derivation scheme ...........................................................................11 3. NURN. A Notation for Defining Context-Dependent Languages ......................................................... 15 3.1 Describing Context-Dependent Languages: the NURN Approach ..................................... 15 3.2 Extended Example of the NURN Approach ............................................................................. 17 3.2.1 The declarationjoint and region entities ........................................ 18 3.2.2 The is- effect ive-over relation .................................................................... 19 3.2.3 The is- the . def ining-occurr ence-of relation ...................................... 20 3.2.4 The i s-a-conf licting-declaration-nt relation .......................... 20 3.2.5 Summary ...................................................................................................................... 21 3.3 Defining NURN Relations ............................................................................................................ 21 3.3.1 Predefined relations ................................................................................................... 23 3.3.2 Relation rules for i s-ef f ec t ive-over ........................................................... 24 3.3.3 Relation rules for i s-a-declarat iongoint and i s-a-r egion ........ 29 3.3.4 A relation rule for is-the-def ining-occurrence-of ........................... 30 3.3.5 Relation rules for is- a . conf licting-declaration-t and is. undefined ......................................................................................................... 32 3.3.6 Make Functions .......................................................................................................... 34 3.3.7 The IS-THE-EXPANS ION-OF relation ............................................................... 37 3.3.8 The IS- A -VIOLATION relation ............................................................................ 39 3.4 Pascal Types ..................................................................................................................................... 39 3.4.1 Multi-type literals: nil and [I ........................................................................... 39 3.4.2 Literal string-types ..................................................................................................... 40 3.4.3 Canonical set-types ...................................................................................................