! PATDATA.REQ ! IDENT('X2.0-001') ! ! ! ! COPYRIGHT (c) 1982 BY ! DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. ! ! THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED ! ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE ! INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER ! COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY ! OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY ! TRANSFERRED. ! ! THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE ! AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT ! CORPORATION. ! ! DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS ! SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL. ! !++ ! FACILITY: PAT Parser ! ! ABSTRACT: ! ! PATDATA.REQ is the specification for the PAT/BLISS interface. ! It defines literals for grammar symbols (such as terminals) ! and defines the interface to the parse tables and text ! tables. (The tables are allocated in PATDATA.BLI.) ! ! This file is language independent for most applications ! that use the PAT parser. (See notes on language independence ! that follow.) ! ! This file should be libraried by the lexical analyzer, the ! semantic action processor, and other modules that need to refer ! to symbols in the grammar. ! ! Note that all access to the parse tables and symbol text ! tables should only be made via macros and routines defined ! in this module. ! ! ENVIRONMENT: VAX/VMS user mode ! ! AUTHOR: C. Mitchell, CREATION DATE: 18-Feb-80 ! ! MODIFIED BY: ! ! Charlie Mitchell, 02-Sep-81 : VERSION X2.001 ! 001 - Define the parse tables as an abstract data type to ! facilitate future optimizations of the table organization. ! Modify to use the new LALRPAT external names for terminal ! symbols. !-- require 'PATREQPRO_REQ'; ! !+ ! ! To define the literal definitions for grammar symbols, this file ! requires logical name 'PAT_REQUIRE_FILE' which should be defined ! as the require file output by PAT. ! ! The expansion of the macros called in PAT_REQUIRE_FILE results ! in literal definitions for terminals, non-terminals, and semantics ! actions. ! ! Example. For the grammar ! ! terminal ! ! loop, t_loop, = 1; ! t_loop is the external name ! ',', t_comma, = 2; ! goalsy, t_goalsy, = 3; ! ! end; ! ! begin ! ! SENTENCE = NONSENSE goalsy /; ! ! NONSENSE = loop ',' /CRAZY ; ! ! end. ! ! the following literals are defined: ! ! literal ! for terminals and non-terminals ! T_LOOP = 1, ! external name ! T_COMMA = 2, ! T_GOALSY = 3, ! NT_SENTENCE = 4, ! Note NT_ prefix for non-terminals ! NT_NONSENSE = 5; ! ! literal ! for semantics actions ! PAT$DATA_NULL_SEMACT = 0, ! no action specified ! SENTENCE01 = 1, ! CRAZY = 2; ! ! ! PAT_REQUIRE_FILE is required again in PATDATA.BLI to define ! (allocate and initialize) the parse tables. ! ! ! Notes on Language Independence of PATDATA ! ! This file is generally language independent. Modifications will ! be required for the following reasons: ! ! 1. To obtain grammar symbol text from "foreign tables" that ! are already defined somewhere in your code (e.g., tables ! that are defined for your lexical analyzer so that it can ! recognize reserved words). ! ! 2. To change the allocation units for the parse tables. ! ! 3. To permit multiple grammars to be parsed. This can be ! done by modifying references to the parse tables to be ! done indirectly though a pointer. This can also ! be accomplished by combining the multiple grammars in ! one PAT intput file and defining a special lexical token that ! is passed to the parser as the first token. Example: ! ! TERMINAL ! parse_grammar_1 = 0; ! parse_grammar_2 = 1; ! goalsy = 2; ! ... ! END; ! BEGIN ! ! SENTENCE = WHOLETHING goalsy; ! ! WHOLETHING = parse_grammar_1 GRAMMAR_1 ! | parse_grammar_2 GRAMMAR_2; ! ! GRAMMAR_1 = {definition of one grammar} ... ! ! GRAMMAR_2 = {definition of another grammar} ... ! ! END. ! !- ! ! ! This file is divided into 6 sections: ! ! 1. Macros for obtaining text associated with grammar symbols. ! ! 2. Allocation units for the parse tables. ! ! 3. Routines and macros for accessing and interpreting the parse ! tables. ! ! 4. Literal definitions for grammar symbols. ! ! 5. Other literal definitions. ! ! 6. Clean up. ! !+ ! Section 1 ! ! Macros for obtaining text of terminal, non-terminal, and ! semantics action symbols. !- macro PAT$DATA_SYMBOL_TEXT (SYMBOL_NUMBER) = !+ ! This macro returns the address of a string descriptor ! for the terminal or non-terminal symbol represented ! by SYMBOL_NUMBER. !- begin external PAT$DATA_SYMBOL_TEXT_TABLE : vector; ! of string descriptors PAT$DATA_SYMBOL_TEXT_TABLE [(SYMBOL_NUMBER)*SD_SIZE/%upval] end %; %if PATBLSEXT_DEBUGGING %then macro PAT$DATA_SEMACT_TEXT (ACTION_NUMBER) = !+ ! This macro returns the address of a string descriptor ! for the semantic action represented by ACTION_NUMBER. !- begin external PAT$DATA_SEMACT_TEXT_TABLE : vector; ! of string descriptors PAT$DATA_SEMACT_TEXT_TABLE [(ACTION_NUMBER)*SD_SIZE/%upval] end %; %fi ! !+ ! Section 2 ! ! Define allocation units for parsing tables. Note that these ! definitions are not intended for direct use outside PATDATA. ! !- macro PAT_STATE_SYMBOL_TABLE_ALLOC = ! for state symbol table word %, PAT_STATE_ACTION_TABLE_ALLOC = ! for state action table word %, PAT_RHS_COUNT_TABLE_ALLOC = ! for rhs count table byte %, PAT_LHS_TABLE_ALLOC = ! for lhs table word %, PAT_SEMACT_TABLE_ALLOC = ! for semantics action table word %; ! !+ ! Section 3 ! ! Routines and macros for accessing and interpreting the parse tables. ! !- !++ ! ! Macro PAT$DATA_MOVE_ACTION ! ! FUNCTIONAL DESCRIPTION: ! ! When called with STATE indicating the parse state and ! SYMBOL containing an input symbol to that state, this routine ! returns the action code for the move action that will be performed. ! (See PAT$DATA_ACTION_IS) ! ! FORMAL PARAMETERS: ! ! STATE - Parse state. Use PAT$DATA_INITIAL_STATE ! to start parsing. ! ! SYMBOL - Terminal or non-terminal symbol ! ! VALUE: ! ! ACTION_CODE - Action code of the action that will be performed ! !-- literal PAT$DATA_INITIAL_STATE = 0; ! Initial parse state macro PAT$DATA_MOVE_ACTION (STATE, SYMBOL) = begin external PAT$DATA_STATE_SYMBOL_TABLE : vector [, PAT_STATE_SYMBOL_TABLE_ALLOC], PAT$DATA_STATE_ACTION_TABLE : vector [, PAT_STATE_ACTION_TABLE_ALLOC, signed]; local STATE_INDEX; STATE_INDEX = (STATE); while ((.PAT$DATA_STATE_SYMBOL_TABLE [.STATE_INDEX] neq (SYMBOL)) and ! (.PAT$DATA_STATE_SYMBOL_TABLE [.STATE_INDEX] neq PAT$DATA_ELSE_CODE)) do if .PAT$DATA_STATE_SYMBOL_TABLE [.STATE_INDEX] eql PAT$DATA_CONT_CODE then STATE_INDEX = .PAT$DATA_STATE_ACTION_TABLE [.STATE_INDEX] else STATE_INDEX = .STATE_INDEX + 1; .PAT$DATA_STATE_ACTION_TABLE [.STATE_INDEX] end %; ! !+ ! The macros that follow test and interpret the action code returned by ! PAT$DATA_MOVE_ACTION. ! ! The action to be taken (shift to state n, reduce to rule m) ! is encoded in one value. PAT$DATA_ACTION_IS can be used ! to test if the action code represents a shift, reduction, etc, ! and then PAT$DATA_AC_TO_SHIFT_STATE, PAT$DATA_AC_TO_PRODUCTION_NO, or ! PAT$DATA_AC_TO_LA_PRODUCTION_NO can be used to obtain the shift ! state or the production number in the case of reductions. !- ! macro PAT$DATA_ACTION_IS (ACTION_CODE, CLASS) = !+ ! This macro is used to test the action code returned ! by routine PAT$DATA_MOVE_ACTION. Note that the macros ! assume that the tests are made in a specific order: ! 'ERROR', 'SHIFT', 'LOOK_AHEAD'. ! ! ! CLASS is specified by one of the following quoted strings: ! ! CLASS meaning assumption ! ! 'ERROR' error ! ! 'SHIFT' shift Not an ERROR ! ! 'LOOK_AHEAD' lookahead ! reduction ! !- %if %identical (CLASS,'ERROR') %then ! Error ((ACTION_CODE) eql PAT$DATA_ERROR_CODE) %else %if %identical (CLASS,'SHIFT') %then ! Shift ((ACTION_CODE) geq 0) %else %if %identical (CLASS,'LOOK_AHEAD') %then ! Look-ahead reduction ((ACTION_CODE) leq -PAT$DATA_SCAN_CODE) %fi %fi %fi %; ! macro PAT$DATA_AC_TO_SHIFT_STATE (ACTION_CODE) = !+ ! Return the goto state for a shift. ACTION_CODE is ! the value returned by PAT$DATA_MOVE_ACTION. !- (ACTION_CODE) %; macro PAT$DATA_AC_TO_PRODUCTION_NO (ACTION_CODE) = !+ ! Return the production number for a non-lookahead ! reduction. ACTION_CODE is the value returned by ! PAT$DATA_MOVE_ACTION. !- (-(ACTION_CODE)) %; macro PAT$DATA_AC_TO_LA_PRODUCTION_NO (ACTION_CODE) = !+ ! Return the production number for a lookahead ! reduction. ACTION_CODE is the value returned by ! PAT$DATA_MOVE_ACTION. !- (-((ACTION_CODE) + PAT$DATA_SCAN_CODE)) %; ! external routine PAT$DATA_NEXT_TRANSITION : novalue; !++ ! FUNCTIONAL DESCRIPTION: ! ! This routine is called to fetch the next symbol which is ! the next legal transition from a state. If ONLY_TERMINALS is TRUE ! the routine will only return terminals, otherwise it will return ! terminals and non-terminals. ! ! FORMAL PARAMETERS: ! ! STATE Parse state ! ! ONLY_TERMINALS Boolean--if TRUE, only terminals are ! returned ! ! REF_LAST_TRANSITION Pointer to the last transition. ! To find the first transition in ! a state, pass PAT$DATA_FIND_FIRST_TRANSITION. ! This is an output parameter that is ! updated to indicate the last transition ! returned. ! ! REF_ACTION_CODE Pointer to an action code. This is ! an output parameter that is set only ! when this routine returns ! PAT$DATA_NO_MORE_TRANSITIONS ! ! REF_SYMBOL Pointer to a location to receive a ! symbol which is a legal transition or ! or PAT$DATA_NO_MORE_TRANSITIONS if all ! have been returned. ! !-- !+ ! Define constants for PAT$DATA_NEXT_TRANSITION. !- literal PAT$DATA_NO_MORE_TRANSITIONS = -1, ! Used as symbol to indicate ! no more transition in a state PAT$DATA_FIND_FIRST_TRANSITION = -1; ! Used to indicate desire to find ! the first transition in a ! state. ! !+ ! Macro for accessing the parse reduction table !- macro PAT$DATA_GET_REDUCTION_INFO ! (PRODUCTION_NUMBER, REF_LHS, REF_RHS_COUNT, REF_SEMACT) = !+ ! This macro accesses the parse reduction table for ! production PRODUCTION_NUMBER and assigns values ! to ! ! REF_LHS address of location to be assigned ! the symbol number of the symbol ! (non-terminal) on the right hand side ! of the production. ! ! REF_RHS_COUNT address of location to be assigned the ! number of symbols (terminal and non- ! terminal) on the right hand side of ! the production. ! ! REF_SEMACT address of location to be assigned the ! semantic action number for the ! production. !- begin external PAT$DATA_LHS_TABLE : vector [, PAT_LHS_TABLE_ALLOC], PAT$DATA_RHS_COUNT_TABLE : vector [, PAT_RHS_COUNT_TABLE_ALLOC], PAT$DATA_SEMACT_TABLE : vector [, PAT_SEMACT_TABLE_ALLOC]; (REF_LHS) = .PAT$DATA_LHS_TABLE [PRODUCTION_NUMBER]; (REF_RHS_COUNT) = .PAT$DATA_RHS_COUNT_TABLE [PRODUCTION_NUMBER]; (REF_SEMACT) = .PAT$DATA_SEMACT_TABLE [PRODUCTION_NUMBER] end %; ! !+ ! Section 4 ! ! Literal definitions for grammar symbols. ! ! This section requires PAT_REQUIRE_FILE which is the require file output ! by PAT and which contains: ! ! 1. Symbolic definitions (using macro calls) for all terminals, ! non-terminals, and semantics actions. ! ! 2. Symbolic definitions of the parsing tables. ! ! Definitions in the require file PAT_REQUIRE_FILE have the form: ! ! PAT_DECTRMDEF('ABORT', T_ABORT, 0) ! PAT_DECTRMDEF('ACCEPT', T_ACCEPT, 1) ! ... for terminals ! ! PAT_NONTRMDEF('PROGRAM',106) ! ... for non-terminals ! ! PAT_SEMACTDEF('XXX',7) ! ... for semantics actions ! ! The expansion of these macros results in literal definitions for ! each terminal and non-terminal symbol and each semantics action. ! !- !+ ! Define general macros !- macro EXPAND_ARG_LIST (A) = ! Expand an argument list with comma separators A %if not %null (%remaining) %then , EXPAND_WORKER (%remaining) %fi %; macro EXPAND_WORKER [ARG] = ARG %; !+ ! Define macros to define literals for terminal and ! non-terminal symbols and semantics actions !- macro PAT_SYMBOL_DEFINITIONS (A) = literal EXPAND_ARG_LIST (A, %remaining); %; macro PAT_SEMACT_DEFINITIONS (A) = literal EXPAND_ARG_LIST (A, %remaining); %; macro PAT_INTERNAL_DEFINITIONS (A) = literal EXPAND_ARG_LIST (A, %remaining); %; macro PAT_DECTRMDEF (SYMBOL_NAME, EXT_NAME, VALUE) = ! Declared terminal definition %name (EXT_NAME) = VALUE %; macro PAT_UNDTRMDEF (SYMBOL_NAME, VALUE) = ! Undeclared terminal definition %warn ('Undefined terminal symbol: (It never appears on the left hand side of a production).')%name( EXT_NAME) = VALUE %; macro PAT_NONTRMDEF (NAME, VALUE) = ! Non-terminal definition %name ('NT_', NAME) = VALUE %; macro PAT_NONTRMREF (NAME) = ! Non-terminal reference %name ('NT_', NAME) %; macro PAT_SEMACTDEF (NAME, VALUE) = ! Semantics action definition %name (NAME) = VALUE %; macro PAT_SEMACTREF (NAME, VALUE) = ! Semantics action reference %name (NAME) %; macro PAT_INTRNLDEF (NAME, VALUE) = ! Internal definition %name (NAME) = VALUE %; macro PAT_UNUSEDNUM (NUM) = ! Unused number %name ('__unused_', NUM) = NUM %; ! ! Define macros in PAT_REQUIRE_FILE that must be defined for correct ! compilation but that aren't needed here. ! macro PAT_DECTRMREF (EXT_NAME) = %, ! null PAT_UNDTRMREF (EXT_NAME) = %, PAT_INTRNLREF (NAME) = %, PAT_STATE_SYMBOL_TABLE (ARG_LIST) = %, PAT_STATE_ACTION_TABLE (ARG_LIST) = %, PAT_RHS_COUNT_TABLE (ARG_LIST) = %, PAT_LHS_TABLE (ARG_LIST) = %, PAT_SEMACT_TABLE (ARG_LIST) = %; ! Require PAT_REQUIRE_FILE to define the literals. Don't define the parsing tables ! here though. literal PAT_DFS = 1; ! Declare PAT_DFS so that see definitions literal PAT_NO_TABLES = 1; ! Declare PAT_NO_TABLES so that don't define the parsing tables require 'PAT_REQUIRE_FILE'; ! !+ ! Section 5 ! ! Define misc. literals ! !- !+ ! Define exported literals. !- literal ! ! null action code PAT$DATA_NULL_SEMACT = PAT_SEMACTREF ('PAT_NULL'), ! first terminal symbol PAT$DATA_FIRST_TERM = PAT_FIRST_TERM, ! last terminal symbol PAT$DATA_LAST_TERM = PAT_LAST_TERM, ! number of terminals PAT$DATA_NUM_TERM = (PAT_LAST_TERM - PAT_FIRST_TERM) + 1, ! number of terminal and non-terminal symbols PAT$DATA_MAX_TOKEN = PAT_MAX_TOKEN, ! number of semantics actions PAT$DATA_MAX_SEMACT = PAT_MAX_SEMACT, ! top most production (non-terminal on lhs) PAT$DATA_SENTENCE_CODE = PAT_SENTENCE_CD; !+ ! Define private literals (those that should only be used by ! routines and macros defined in PATDATA). !- literal PAT$DATA_CONT_CODE = PAT_CONT_CODE, PAT$DATA_ELSE_CODE = PAT_ELSE_CODE, PAT$DATA_SCAN_CODE = PAT_SCAN_CODE, PAT$DATA_ERROR_CODE = PAT_ERROR_CODE; ! !+ ! Section 6 ! ! Clean up. ! ! Undeclare the macros and names that aren't used outside of PATDATA. ! !- undeclare PAT_DFS, PAT_NO_TABLES, PAT_CONT_CODE, PAT_ELSE_CODE, PAT_SCAN_CODE, PAT_ERROR_CODE, PAT_MAX_TOKEN, PAT_MAX_POP, PAT_MAX_REDUCT, PAT_MAX_TRANS, PAT_MAX_SEMACT, PAT_FIRST_TERM, PAT_LAST_TERM, PAT_SENTENCE_CD, %quote PAT_DECTRMDEF, %quote PAT_UNDTRMDEF, %quote PAT_NONTRMDEF, %quote PAT_SEMACTDEF, %quote PAT_INTRNLDEF, %quote PAT_DECTRMREF, %quote PAT_UNDTRMREF, %quote PAT_NONTRMREF, %quote PAT_SEMACTREF, %quote PAT_INTRNLREF, %quote PAT_UNUSEDNUM, %quote PAT_SYMBOL_DEFINITIONS, %quote PAT_SEMACT_DEFINITIONS, %quote PAT_INTERNAL_DEFINITIONS, %quote PAT_STATE_SYMBOL_TABLE, %quote PAT_STATE_ACTION_TABLE, %quote PAT_RHS_COUNT_TABLE, %quote PAT_LHS_TABLE, %quote PAT_SEMACT_TABLE, %quote EXPAND_ARG_LIST, %quote EXPAND_WORKER; ! End of PATDATA.REQ