next up previous contents
Next: Integrating the code into Up: The Bloch Sphere Previous: libplot   Contents

Bison/Flex - Adding a new statement to the language

The Quantum Computing language uses the GNU Flex and GNU Bison tools to provide a correct syntax for the language. Bison is a parser generator that converts a grammar description for an LALR(1) context-free grammar into a C/C++ program to parse that grammar. A context-free grammar is one where one or more syntactic groupings are specified, with rules for constructing them from their parts. An LALR(1) grammar means that it must be possible to tell how to parse any portion of an input string with just a single token of look-ahead. Bison is used in the QCL to ensure that whatever is typed in at the command line is syntactically correct in accordance with the grammar of the Quantum Computing Language. The grammar for the QCL is stored in the file "qcl.y".

GNU Flex is a tool for generating scanners. A scanner is a program that recognizes patterns in text. In the QCL, Flex reads the "qcl.lex" file, for a description of a scanner to generate. The description in that file is in the form of pairs of regular expressions and C++ code, called rules. Flex generates as output a C source file, `lex.cc'. When the code is compiled and linked with the bison parser, Flex analyzes the input on the command line input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C++ code.

To enable the user to call the bloch sphere function on a qubit from the command line, a new statement was inserted into the Quantum Computing Language. The syntax of this statement is simply; bloch a where a is a quantum register containing exactly one qubit. To enable the lexical scanner to identify this statement, the following line was inserted into the "qcl.lex" file;
\begin{lstlisting}[frame=trbl, caption=Extract from qcl.lex]{}
\textsl{''bloch'' yylval.OBJ=0; return tokBLOCH;} \\
\end{lstlisting}


To enable the parser to correctly parse this statement, the following code must be inserted into the "qcl.y" file;
\begin{lstlisting}[frame=trbl, caption=Extract from qcl.y]{}
\%token tokBLOCH 60...
...H expr ',' expr ';' \{ \$\$ = YYNEW(sBloch(\$2,\$4)); \} $\ \\
\end{lstlisting}


The above code ensures that what is typed in is syntactically correct. It starts the sBloch() class which draws the quantum register argument. The semantics of the command, ie. insuring that there is only one qubit in the quantum register argument, is handled further in in the code, it is not handled in the grammar itself.


next up previous contents
Next: Integrating the code into Up: The Bloch Sphere Previous: libplot   Contents
Colm O hEigeartaigh 2003-05-30