Skip to content
realistschuckle edited this page Sep 14, 2010 · 12 revisions

Overall File Format

The general format for an goll1e input grammar file has three sections, similar to what you find with YACC input files.

Definition Section
%%
Rules Section
%%
Code Section

Definition Section

The definition section allows you to set use the following directives.

%package

Sets the name of the package to which the resulting parser file belongs.

For example, with the line

%package main

in your grammar file, the resulting parser will start with the declaration

package main

%dev

The %dev instruction turns on development reporting during parser generation and parsing. This is very verbose, so turn it on only if you need to.

%defaultcode

The %defaultcode instruction allows you to change the default production transition from $$ = $1 to whatever you provide. For example, the following block will also print a message as well as assign the value.

%defaultcode {
  fmt.Println("Default code. Assigning", $1, "to LHS")
  $$ = $1
}

%union

Creates the struct named yystype that goll1e uses to preserve values in the syntax-directed transitions. The declarations follow the Go language’s style of declaration with the name of the “variable” followed by its type.

For example, with the declaration

%union {
  op func(float)float
  fval float
}

in your grammar file, the resulting parser will contain the declaration

type yystype struct {
  op func(float)float
  fval float
}

%import

Provides a list of packages that the resulting file will use in its code. The generated parser file will contain the specified packages. The generated file contains container/vector because the parser requires it to operate.

For example, the following line included in your grammar file

%import os container/heap container/vector strings bytes

will result with the following import statement in the generated parser

import (
  "container/vector"
  "os"
  "container/heap"
  "strings"
  "bytes"
)

%type

Associates a type defined in %union with a nonterminal value in the grammar.

Assume that your grammar has the nonterminal Add in it. By specifying type<fval> Add in the declaration section, the Add grammar symbol will take on the type specified by the declaration in the %union directive with the fval which, in the example described above, would result in Add taking on a float value.

%token

Associates a type defined in %union with a terminal value in the grammar.

Same sort of thing as %type, but for termianl symbols.

Rules Section

The Rules Section must contain left-factored grammars. Otherwise, goll1e will report conflicts in the grammar.

  • Comments begin with the hash mark “#” and extend to the next newline
  • Terminal symbols must begin with a lower-case letter
  • Nonterminal symbols must begin with an upper-case letter
  • Character literals must have surrounding apostrophes “’”
  • goll1e assumes that the first nonterminal symbol in the grammar represents the start symbol for the grammar
  • A colon “:” separates the left-hand and right-hand sides of a rule
  • A pipe “|” separates alternate right-hand sides of the rule
  • A semi-colon “;” terminates a rule definition
  • Any production rule followed by a code block delimited by “{” and “}” will have that code run when parser recognizes the rule.

For example, a grammar that can recognize addition statements might look like the following:

Expression : number ExpressionA
           ;

ExpressionA : '+' Expression
            | # Empty string
            ;

Code Section

The Code Section will embed the code specified into the resulting parser by appending it to the generated file.

Example Grammar File

The input.y file included in the project shows a real grammar file that works with goll1e. You can find an explanation of the file’s contents on the Example Grammar File page.