# along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. # from RuleItems import Rule import CompileGrammar ruleSet=[ #Rule("Start", ['S','$'], 0, 'noop'), Rule('S', ['RuleList'], 0, 'noop'), Rule('RuleList', ['Rule', 'RuleList'], 1, 'noop'), Rule('RuleList', [], 2, 'noop'), Rule('Rule', ['COLON', 'id', 'COLON', 'TokItem', 'COLON', 'TokList'], 3,'RuleLine'), Rule('TokList', ['Token', 'TokList'], 4, 'TTtoTokList'), Rule('TokList', ['id', 'TokList'], 5, 'TTtoTokList'), Rule('TokList', [], 6, 'NullToTokList'), Rule('TokItem', ['Token',], 7, 'TokenToTokItem'), Rule('TokItem', ['id',], 8, 'idToTokItem'), ] print 'Rules' print '--------------------' for i in ruleSet: print i grammar = CompileGrammar.compileGrammar('LALR1', ruleSet, showStates = 1, showItems = 1) gf = open ('ParGenGrammar.py', 'w') for i in grammar.items(): gf.write('%s = %s\n' % i) gf.close()
import CompileGrammar ruleSet = [ Rule('S', ['E'], 0), Rule('E', ['E', '+', 'T'], 1), Rule('E', ['T'], 2), Rule('T', ['T', '*', 'F'], 3), Rule('T', ['F'], 4), Rule('F', ['(', 'E', ')'], 5), Rule('F', ['id'], 6), ] import Parser tokenList = [ Parser.Token('id'), Parser.Token('+'), Parser.Token('id'), Parser.Token('*'), Parser.Token('id'), ] results = [] for method in 'LALR1', 'LALR0':#'LR1', 'LALR1', 'LALR0', 'LR0': cdg = CompileGrammar.compileGrammar(method, ruleSet, 1, 1) #print cdg p = Parser.ListTokenSource(tokenList) results.append(Parser.Parse(cdg, p))#, debug=1)) #print Parser.Parse(cdg, p) for i in results: print print i
# Public License or the SkunkWeb License, as specified in the # README file. # from RuleItems import Rule import CompileGrammar ruleSet=[ #Rule("Start", ['S','$'], 0, 'noop'), Rule('S', ['RuleList'], 0, 'noop'), Rule('RuleList', ['Rule', 'RuleList'], 1, 'noop'), Rule('RuleList', [], 2, 'noop'), Rule('Rule', ['COLON', 'id', 'COLON', 'TokItem', 'COLON', 'TokList'], 3,'RuleLine'), Rule('TokList', ['Token', 'TokList'], 4, 'TTtoTokList'), Rule('TokList', ['id', 'TokList'], 5, 'TTtoTokList'), Rule('TokList', [], 6, 'NullToTokList'), Rule('TokItem', ['Token',], 7, 'TokenToTokItem'), Rule('TokItem', ['id',], 8, 'idToTokItem'), ] print 'Rules' print '--------------------' for i in ruleSet: print i grammar = CompileGrammar.compileGrammar('LALR1', ruleSet, showStates = 1, showItems = 1) gf = open ('ParGenGrammar.py', 'w') for i in grammar.items(): gf.write('%s = %s\n' % i) gf.close()