grammar
index
/Users/dcavar/Documents/Teaching/DGfS Herbstschule 2005/Code/grammar.py

Filename: grammar.py
Author: Damir Cavar
Date: 19. Sept. 2005
 
(C) 2005 by Damir Cavar
 
        This code is free; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.
 
This is a simple implementation of a context free grammar parser that
reads in files of the format:
 
-----------  begin file example  -----------
 
# my small example grammar
S -> NP VP
 
NP -> N
NP -> Art N
NP -> Art Adj N
 
VP -> V
VP -> V NP
 
# lexical rules
Art -> the
Art -> a
Adj -> green
Adj -> big
N -> dog
N -> cat
N -> mouse
V -> chase
V -> ignore
 
------------  end file example  ------------

 
Modules
       
sys

 
Classes
       
PSG

 
class PSG
    Grammar class:
Internal data structures:
 
LHS: dictionary with left-hand-side symbols as keys and a list
of possible right-hand-sides as values.
 
RHS: dictionary with right-hand-side symbol tuples as keys and a list
of possible left-hand-sides.
 
  Methods defined here:
__init__(self, filename)
Constructor.
__read__(self, filename)
Read in a CFG and return a grammar representation. This is a
hidden method.
__str__(self)
Generates a string representation of the grammar such that the grammar
is dumped in a phrase structure rule format.
getLHS(self, right)
Return LHS for a RHS.
getRHS(self, left)
Return the RHS for a LHS.