The Parser Stage

The parser stage consists of two parts:

Parser

The parser has to check the query string (which arrives as plain ASCII text) for valid syntax. If the syntax is correct, a parse tree is built up and handed back; otherwise, an error is returned. The UNIX tools lex and yacc are used for this implementation.

The lexer, defined in the file scan.l, is responsible for recognizing identifiers, SQL keywords, and so on. The lexer generates a token for every keyword or identifier, and gives it to the parser.

The parser, defined in the file gram.y, consists of a set of grammar rules and actions that are executed whenever a rule is fired. These actions build up the parse tree.

Transformation Process

The transformation process takes the tree handed back by the parser as input and steps recursively through it. It then transforms the tree into the internal formula used by the database backend.