使用yacc的模态解析器

I am writing my first parser in yacc. I would like to parse a file that has 3 "modes":

  • Statement mode
  • Table heading mode
  • Table row mode

I would like my parser to start out in statement mode, then when it sees a line consisting of minus signs, switch to table heading mode. When it sees another line of minus signs, switch to table row mode, and finally when it sees a third set of minus signs switch to statement mode:

statement...
statement...
statement...
----
table heading
----
table row
table row
table row
----
statement
statement
statement

One thing that occures to me, is that I could have 3 separate grammars which I would switch between in my line feed loop. However, I don't know how to create multiple grammars in one .y file.

The other thing that seems like a possibility is using "Lexical Tie-ins" (unfortunately, you'll have to search for that string in the document). However, the author of the yacc tutorial doesn't really tell me anything about these "lexical tie-ins" other than that "This kind of ``backdoor'' approach can be elaborated to a noxious degree. Nevertheless, it represents a way of doing some things that are difficult, if not impossible, to do otherwise." Which is hardly encouraging.

I have solved the problem by creating pseudo-symbols which I insert using the lexer:

line
    : TABLE_HEADING sentences ',' table_heading_columns ',' sentences
    {
      fmt.Println("TABLE_HEADING")
    }
    | TABLE_BODY table_body_columns
    {
      fmt.Println("TABLE_BODY")
    }
    | STATEMENT sentences
    {
      fmt.Println("STATEMENT")
    }
    ;