2000-02-27 20:45:44 +00:00
|
|
|
"""
|
2000-02-28 13:44:24 +00:00
|
|
|
tests/MathParser.py -- created Mon Feb 28 00:06:56 2000
|
2000-02-27 20:45:44 +00:00
|
|
|
|
|
|
|
|
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 MathParser which will use this engine. It's usage
|
|
|
|
|
is indicated in MathParser's doc-string.
|
|
|
|
|
"""
|
|
|
|
|
#
|
|
|
|
|
# this section contains source code added by the user
|
|
|
|
|
# plus 'import PyLR'
|
|
|
|
|
#
|
|
|
|
|
import MathLexer
|
|
|
|
|
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', 'INT', 'PLUS', 'TIMES', 'LPAR', 'RPAR']
|
|
|
|
|
#
|
|
|
|
|
_actiontable = [
|
|
|
|
|
[('', -1), ('s', 4), ('', -1), ('', -1), ('s', 5), ('', -1)],
|
|
|
|
|
[('a', -1), ('', -1), ('s', 6), ('', -1), ('', -1), ('', -1)],
|
|
|
|
|
[('r', 2), ('', -1), ('r', 2), ('s', 7), ('', -1), ('r', 2)],
|
|
|
|
|
[('r', 4), ('', -1), ('r', 4), ('r', 4), ('', -1), ('r', 4)],
|
|
|
|
|
[('r', 6), ('', -1), ('r', 6), ('r', 6), ('', -1), ('r', 6)],
|
|
|
|
|
[('', -1), ('s', 4), ('', -1), ('', -1), ('s', 5), ('', -1)],
|
|
|
|
|
[('', -1), ('s', 4), ('', -1), ('', -1), ('s', 5), ('', -1)],
|
|
|
|
|
[('', -1), ('s', 4), ('', -1), ('', -1), ('s', 5), ('', -1)],
|
|
|
|
|
[('', -1), ('', -1), ('s', 6), ('', -1), ('', -1), ('s', 11)],
|
|
|
|
|
[('r', 1), ('', -1), ('r', 1), ('s', 7), ('', -1), ('r', 1)],
|
|
|
|
|
[('r', 3), ('', -1), ('r', 3), ('r', 3), ('', -1), ('r', 3)],
|
|
|
|
|
[('r', 5), ('', -1), ('r', 5), ('r', 5), ('', -1), ('r', 5)]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# the goto table, each row represents a state
|
|
|
|
|
# and each column the nonterminal that was on the lhs of the
|
|
|
|
|
# reduction
|
|
|
|
|
#
|
|
|
|
|
_gototable = [
|
|
|
|
|
[1, 1, 2, 2, 3, 3],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[8, 8, 2, 2, 3, 3],
|
|
|
|
|
[None, None, 9, 9, 3, 3],
|
|
|
|
|
[None, None, None, None, 10, 10],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, None, None, None, None, None],
|
|
|
|
|
[None, 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 MathParser class below that gets called
|
|
|
|
|
# when that production occurs, and the index of the lhs in the
|
|
|
|
|
# nonterminals (as in the gototable)
|
|
|
|
|
#
|
|
|
|
|
_prodinfo = [
|
|
|
|
|
(3, 'addfunc', 0), # expression: expression 2 term (addfunc)
|
|
|
|
|
(1, 'unspecified', 0), # expression: term (unspecified)
|
|
|
|
|
(3, 'timesfunc', 2), # term: term 3 factor (timesfunc)
|
|
|
|
|
(1, 'unspecified', 2), # term: factor (unspecified)
|
|
|
|
|
(3, 'parenfunc', 4), # factor: 4 expression 5 (parenfunc)
|
|
|
|
|
(1, 'unspecified', 4), # factor: 1 (unspecified)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MathParser(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
|
|
|
|
|
|
|
|
|
|
expression: expression PLUS term (addfunc);
|
|
|
|
|
expression: term (unspecified);
|
|
|
|
|
term: term TIMES factor (timesfunc);
|
|
|
|
|
term: factor (unspecified);
|
|
|
|
|
factor: LPAR expression RPAR (parenfunc);
|
|
|
|
|
factor: INT (unspecified);
|
|
|
|
|
|
|
|
|
|
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 MyMathParser(MathParser):
|
|
|
|
|
# ...define the methods for the productions...
|
|
|
|
|
|
|
|
|
|
p = MyMathParser(); p.parse(text)
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
lexer = MathLexer.MathLexer()
|
|
|
|
|
PyLR.Parser.__init__(self, lexer, _actiontable, _gototable, _prodinfo)
|