Added some grammar files

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@15 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2000-02-28 13:49:21 +00:00
parent 159a5d12db
commit d6507eddb9
3 changed files with 205 additions and 0 deletions

117
GML/GMLBaseParser.py Normal file
View file

@ -0,0 +1,117 @@
"""
GMLBaseParser.py -- created Mon Feb 28 14:20:00 2000
This file was automatically generated by the PyLR parser generator.
It defines the tables 'actiontable', 'gototable', and 'prodinfo'. These
tables are used to give functionality to a parsing engine. It also defines
A Parser class called GMLBaseParser which will use this engine. It's usage
is indicated in GMLBaseParser's doc-string.
"""
#
# this section contains source code added by the user
# plus 'import PyLR'
#
import GML
import PyLR
#
# the action table means
# ('s', -1) shift
# ('r', <n>) reduce with production n
# ('a', -1) accept
# ('', -1) error
# each row represents a state and each column a terminal lookahead symbol
# Lexer symbols are:
# ['EOF', 'REAL', 'INTEGER', 'LSQB', 'RSQB', 'STRING', 'KEY']
#
_actiontable = [
[('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('s', 6)],
[('a', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('', -1)],
[('r', 1), ('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('s', 6)],
[('', -1), ('', -1), ('', -1), ('', -1), ('s', 12), ('', -1), ('s', 6)],
[('r', 2), ('', -1), ('', -1), ('', -1), ('r', 2), ('', -1), ('r', 2)],
[('r', 3), ('', -1), ('', -1), ('', -1), ('r', 3), ('', -1), ('r', 3)],
[('', -1), ('s', 9), ('s', 8), ('s', 11), ('', -1), ('s', 10), ('', -1)],
[('r', 4), ('', -1), ('', -1), ('', -1), ('r', 4), ('', -1), ('r', 4)],
[('r', 5), ('', -1), ('', -1), ('', -1), ('r', 5), ('', -1), ('r', 5)],
[('r', 6), ('', -1), ('', -1), ('', -1), ('r', 6), ('', -1), ('r', 6)],
[('r', 7), ('', -1), ('', -1), ('', -1), ('r', 7), ('', -1), ('r', 7)],
[('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('', -1), ('s', 6)],
[('r', 8), ('', -1), ('', -1), ('', -1), ('r', 8), ('', -1), ('r', 8)]
]
#
# the goto table, each row represents a state
# and each column the nonterminal that was on the lhs of the
# reduction
#
_gototable = [
[1, 2, 5, None],
[None, None, None, None],
[None, None, 4, None],
[None, None, 4, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, 7],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, 3, 5, None],
[None, None, None, None]
]
#
# This is the prodinfo table. each row represents a production
# the entries are the length of the production, the name of a method
# in an instance of the GMLBaseParser class below that gets called
# when that production occurs, and the index of the lhs in the
# nonterminals (as in the gototable)
#
_prodinfo = [
(1, 'feddich', 0), # gml: list (feddich)
(2, 'gml_key_value', 1), # list: list keyvalue (gml_key_value)
(1, 'unspecified', 1), # list: keyvalue (unspecified)
(2, 'key_value', 2), # keyvalue: 6 value (key_value)
(1, 'unspecified', 3), # value: 2 (unspecified)
(1, 'unspecified', 3), # value: 1 (unspecified)
(1, 'unspecified', 3), # value: 5 (unspecified)
(3, 'gmllist', 3), # value: 3 list 4 (gmllist)
]
class GMLBaseParser(PyLR.Parser):
"""
this class was produced automatically by the PyLR parser generator.
It is meant to be subclassed to produce a parser for the grammar
gml: list (feddich);
list: list keyvalue (gml_key_value);
list: keyvalue (unspecified);
keyvalue: KEY value (key_value);
value: INTEGER (unspecified);
value: REAL (unspecified);
value: STRING (unspecified);
value: LSQB list RSQB (gmllist);
While parsing input, if one of the above productions is recognized,
a method of your sub-class (whose name is indicated in parens to the
right) will be invoked. Names marked 'unspecified' should be ignored.
usage:
class MyGMLBaseParser(GMLBaseParser):
# ...define the methods for the productions...
p = MyGMLBaseParser(); p.parse(text)
"""
def __init__(self):
lexer = GML.GMLLexer()
PyLR.Parser.__init__(self, lexer, _actiontable, _gototable, _prodinfo)

57
GML/__init__.py Normal file
View file

@ -0,0 +1,57 @@
from GMLLexer import GMLLexer
from GMLBaseParser import GMLBaseParser
import types
class GMLParser(GMLBaseParser):
def __init__(self):
GMLBaseParser.__init__(self)
self.result = []
def key_value(self, key, value):
return [(key,value)]
def gml_key_value(self, gml, key_value):
return gml + key_value
def gmllist(self, lsqb, gml, rsqb):
return gml
def feddich(self, l):
self.result = l
def __repr__(self):
return self.getRepr(self.result, 0)
def getRepr(self, lst, indent=0):
indentStr = " "*indent
res = indentStr
lenlst = len(lst)
i=0
for i in range(lenlst):
item = lst[i]
res = res+item[0]+" "
if type(item[1])==types.ListType:
res = res+"[\n"+self.getRepr(item[1], indent+2)+"\n"+indentStr+"]"
elif type(item[1])==types.StringType:
res = res +'"'+item[1]+'"'
else:
res = res+`item[1]`
if i != lenlst-1:
res = res+"\n"+indentStr
return res
def _test():
p =GMLParser()
text = """AINS 1
graph [
innen 1
innen 2
]
ZWAI 2
"""
p.parse(text, 1)
print p
if __name__=='__main__':
_test()

31
PyLR/GrammarLexer.py Normal file
View file

@ -0,0 +1,31 @@
"""
this file contains the Lexer that is used in parsing Grammar specifications
"""
import re,PyLR
def _retlex(mo):
return mo.group("lex")
def _retcode(mo):
return mo.group("code")
def _retclass(mo):
return mo.group("class")
class GrammarLexer(PyLR.Lexer):
def __init__(self):
PyLR.Lexer.__init__(self)
self.addpat(r"_lex\s+(?P<lex>[^\n]*)", "LEX", _retlex)
self.addpat(r"_code\s+(?P<code>[^\n]*)", "CODE", _retcode)
self.addpat(r"_class\s+(?P<class>[a-zA-Z_][a-zA-Z_0-9]*)", "CLASS", _retclass)
self.addpat(r"[a-zA-Z_][a-zA-Z_0-9]*", "ID")
self.addpat(r":", "COLON")
self.addpat(r";", "SCOLON")
self.addpat(r"\|", "OR")
self.addpat(r"\(", "LPAREN")
self.addpat(r"\)", "RPAREN")
self.addpat(r'"""', "GDEL")
self.addpat(r"#[^\n]*", "COMMENT", None, 1)
self.addpat(r"\s+", "WS", None, 1)