A PARSER FOR THE PROPOSITIONAL CALCULUS

RAUL H.C.LOPES

1. Introdution

This document introduces a parser for the Propositional Calculus written in the language of the program Hlex, a compiler-compiler that generates parsers in Haskell from the description of the grammar of the target language. Hlex reads the grammar (indeed a description of ) of some language L and generates a parser (a program that decides whether input sentences are L sentences.) It must be clear that input for the parser must be a sequence of tokens.

For example, if you’re dealing with Propositional Calculus formulae, the sentences should consist of disjuntions of formulae, conjunction, implications and so on.

The grammar is described by presenting • the set of amissible tokens;

• the actions to take when legal sentences (sequences of tokens) are identified in the input.

1.1. Generating the parser. Use the program lst2code.pl to extract a program in Hlexthat describes the syntax for the Propositional Calculus.

  ./lst2code.pl PCFormula.tex > PCFormula.y

A file named PCFormula.y will be created that will contain a Hlex program for parsing Propositional Calculus formulae. Use Hlex to produce a haskell parser for the Propositional Calculus. The line bellow will create a file named PCFormula.hs containing the Haskell encoded parser.

  happy PCFormula.y

1.2. Where to find Hlex. Follow the link

 http://www.haskell.org/happy/

to get sources and documentation for Hlex.

1.3. The program lst2code.pl. The program lst2code.pl extracts code from a L^{A}TEX document.

2. What the parser exports

The first thing to do is to introduce a module that will implement the parser. A sequence of lines preceded by ’{’ and followed by ’{’ introduces is literally inserted in the generated Haskell code. The code that follows declares the module PCFormula, and exports Formula, parseFormula, and parse. Notice that it imports the lexical analyser PCTokens.

−− −∗−Text −∗−

{

module PCFormula ( Formula ( . . ) , parseFormula , parse )

where

import Char
import PCTokens

}

3. Interfacing with pasrser

A name is declared for the function that will map a sequence of tokens to a parsing tree: it is named parseFormula. The Haskell data type of tokens is also declared: Token.

%name parseFormula
%tokentype { Token }

At last, the tokens are presented.

%token
not  { TokNeg }
or  { TokDisj }
var  { TokVar $$ }
and  { TokConj }
imp  { TokImp }
equiv  { TokEquiv}
’ ( ’  { TokOpen }
’ ) ’  { TokClose}

Notice the line

var { TokVar $$ }

that declares a name var for the token (TokVar $$$), where $$$ will stand for the identifier carried along with this kind of token.

4. The grammar

The target language grammar is presented next. First priorities are introduced for some tokens. In the following lines, imp and equiv have the same priority which is lower than the priority attibuted to

\ t e x t b f {and } and \ t e x t b f { or }.
−%l e f t not
−%l e f t and or
%l e f t imp equiv
%l e f t and or
%l e f t not

%%

The syntax of formulae is presented next.

Formula : var  { Var $1 }
| not Formula  { Neg $2 }
| ’ ( ’ not Formula ’ ) ’  { Neg $3 }
| ’ ( ’ Formula and Formula ’ ) ’  { Conj $2 $4 }
| Formula and Formula  { Conj $1 $3 }
| ’ ( ’ Formula or Formula ’ ) ’  { D i s j $2 $4 }
| Formula or Formula  { D i s j $1 $3 }
| ’ ( ’ Formula imp Formula ’ ) ’  { Imp $2 $4 }
| Formula imp Formula  { Imp $1 $3 }
| ’ ( ’ Formula equiv Formula ’ ) ’ { Equiv $2 $4 }
| Formula equiv Formula  { Equiv $1 $3 }

For example the line

| '(' Formula and Formula ')'{ Conj $2 $4 }

states if a disjunction of formulae enclose by parentheses is seen on input, then the constructor Conj should be called with second and fourth tokens of the input sentence as arguments. Notice that in such a sentence the first token is ’(’, the second token is a formula, the third one the conjunction token, and the fourth one is ’)’.

A last stretch of Haskell code is introduced to declared an error function, the type of formulas, and the function parse, a composition of parseFormula and alexScanToken, capable of mapping String to a Formula.

{

happyError : : [ Token] −> a
happyError = e r r o r ( ” Parse e r r o r \n”)

data Formula =
Var S t r i n g |
Neg Formula |
D i s j Formula Formula |
Conj Formula Formula |
Imp Formula Formula |
Equiv Formula Formula
d e r i v i n g (Eq , Read , Show)
parse = parseFormula . alexScanTokens

}

5. Hlex, yacc, bison

Hlex is Haskell implementation of a compiler-compiler based on the ideas introduced in seventies by yacc. Another descendant of yacc is bison, found in systems like Linux and FreeBSD. If you’re interested in parsers you should also take a look at Antlr.