SML# Document Version 3.7.1
30 A parser generator smlyacc and smllex

30.3 The structure of a smlyacc output file and the interface file specification

smlyacc generates a file YaccInputFile.grm.sml containing one structure of the following signature.

    signature ML_LRVALS =
      sig
         structure Tokens : ML_TOKENS
         structure Parser : PARSER
         sharing type Parser.token = Tokens.token
      end

This signature and other supporting library for smlyacc are collected in smlyacc-lib.smi. In order to use the generated parser program file, the following interface file must be written.

    _require "basis.smi"
    _require "ml-yacc-lib.smi"

    structure <Name> =
    struct
      structure Parser =
      struct
        type token (= boxed)
        type stream (= boxed)
        type result = Absyn.parseresult
        type pos = int
        type arg = unit
        exception ParseError
        val makeStream : {lexer:unit -> token} -> stream
        val consStream : token * stream -> stream
        val getStream : stream -> token * stream
        val sameToken : token * token -> bool
        val parse : {lookahead:int,
                     stream:stream,
                     error: (string * pos * pos -> unit),
                     arg: arg}
                    -> result * stream
      end
      structure Tokens =
      struct
        type pos = Parser.pos
        type token = Parser.token
        <the set of Token forming functions>
         ...
        val EOF: word *  pos * pos -> token
        val CHAR: string *  pos * pos -> token
        ....
      end
    end

In Parser structure, posargresult types are those that are specified in YaccInputFileName.grm. The other components are fixed and should be specified as above. To write the Token structure,copy the contents of the YaccInputFileName.grm.sig signature generated by smlyacc.

The components are described below.

  • token type. The abstract type for the token returned by the lexer generated by smllex.

  • stream type. The abstract type for the input stream of the parser.

  • result type. The type of the output of the parser. It should be the same as the type of the attribute of the top-level grammar rule specified in the YaccInputFile.grm file. の属の性の型と同一である.

  • pos type. The position data type used by the lexer.

  • arg type. The additional argument type for the parser specified in the YaccInputFile.grm file.

  • ParseError exception. Parse error exception generated by the parser.

  • makeStream function. It takes a lexer and return the input stream for the parser.

  • consStream function. This up-push one token to the given input stream.

  • getStream function. It returns the first token in the current input stream.

  • sameToken function. Token equality checking function.

  • parse function. The parser function. It takes the current input stream and returns the generated abstract syntax tree and the rest of the input stream.