def InterpRule(list, ObjectGram): ''' InterpRule should receive list of form [keyword RSKEY, RuleNameStr, keyword COLKEY, Nontermtoken, keyword LTKEY, Bodylist] ''' # check keywords: if len(list)!=6 or list[0]!=RSKEY or list[2]!=COLKEY or list[4]!=LTKEY: raise FlowError, "unexpected meta rule reduction form" ruleName = list[1] ruleNonterm = list[3] ruleBody = list[5] # upcase the the representation of keywords if needed if not ObjectGram.LexD.isCaseSensitive(): for i in range(0,len(ruleBody)): (flag, name) = ruleBody[i] if flag == KEYFLAG: ruleBody[i] = (KEYFLAG, string.upper(name)) elif not flag in (TERMFLAG, NONTERMFLAG): raise FlowError, "unexpected rule body member" rule = kjParser.ParseRule( ruleNonterm, ruleBody ) rule.Name = ruleName return rule
def ruleGrammar(): ''' function to generate a grammar for parsing grammar rules ''' LexD = kjParser.LexDictionary() # use SQL/Ansi style comments LexD.comment( COMMENTFORM ) # declare keywords RStart = LexD.keyword( RSKEY ) TwoColons = LexD.keyword( COLKEY ) LeadsTo = LexD.keyword( LTKEY ) # declare terminals ident = LexD.terminal(IDNAME, IDFORM, IdentFun ) # declare nonterminals Root = kjParser.nonterminal("Root") Rulelist = kjParser.nonterminal("RuleList") Rule = kjParser.nonterminal("Rule") RuleName = kjParser.nonterminal("RuleName") NonTerm = kjParser.nonterminal("NonTerm") Body = kjParser.nonterminal("Body") # declare rules # Root >> NonTerm :: Rulelist InitRule = kjParser.ParseRule( Root, \ [NonTerm, TwoColons, Rulelist], RootReduction ) # Rulelist >> RLNull = kjParser.ParseRule( Rulelist, [], NullRuleList) # Rulelist >> Rule Rulelist RLFull = kjParser.ParseRule( Rulelist, [Rule,Rulelist], FullRuleList) # Rule >> "@R :: NonTerm >> Body RuleR = kjParser.ParseRule( Rule, \ [RStart, RuleName, TwoColons, NonTerm, LeadsTo, Body],\ InterpRule) # Rulename >> ident RuleNameR = kjParser.ParseRule( RuleName, [ident], InterpRuleName) # NonTerm >> ident NonTermR = kjParser.ParseRule( NonTerm, [ident], InterpNonTerm) # Body >> BodyNull = kjParser.ParseRule( Body, [], NullBody) # Body >> ident Body BodyFull = kjParser.ParseRule( Body, [ident,Body], FullBody) # declare Rules list and Associated Name dictionary Rules = [RLNull, RLFull, RuleR, RuleNameR, NonTermR,\ BodyNull, BodyFull, InitRule] RuleDict = \ { "RLNull":0, "RLFull":1, "RuleR":2, "RuleNameR":3, \ "NonTermR":4, "BodyNull":5, "BodyFull":6 , "InitRule":7 } # make the RuleSet and compute the associate DFA RuleSet = Ruleset( Root, Rules ) RuleSet.DoSLRGeneration() # construct the Grammar object Result = kjParser.Grammar( LexD, RuleSet.DFA, Rules, RuleDict ) return Result
def GRAMMAR(): # ******************************BEGIN RECONSTRUCTION # Python declaration of Grammar variable LispG. # automatically generated by module kjParser. # Altering this sequence by hand will probably # leave it unusable. # import kjParser # variable declaration: LispG = kjParser.NullGrammar() # case sensitivity behavior for keywords. LispG.SetCaseSensitivity(0) # declaration of lexical dictionary. # EXCEPT FOR TERMINALS LispG.LexD.punctuationlist = '().' # now comment patterns LispG.LexD.comment(';.*') # now define tokens LispG.IndexToToken[0] = kjParser.nonterminal('ListTail') LispG.IndexToToken[1] = kjParser.termrep('int') LispG.IndexToToken[2] = kjParser.nonterminal('Value') LispG.IndexToToken[3] = LispG.LexD.keyword('(') LispG.IndexToToken[4] = kjParser.termrep('*') LispG.IndexToToken[5] = LispG.LexD.keyword(')') LispG.IndexToToken[6] = LispG.LexD.keyword('SETQ') LispG.IndexToToken[7] = LispG.LexD.keyword('PRINT') LispG.IndexToToken[8] = kjParser.termrep('str') LispG.IndexToToken[9] = kjParser.termrep('var') # declaration of rule list with names. # EXCEPT FOR INTERP FUNCTIONS LispG.RuleL = [None] * 8 rule = kjParser.ParseRule((-7, 'Value'), [(-6, '('), (-6, 'SETQ'), (-8, 'var'), (-7, 'Value'), (-6, ')')]) name = 'SetqRule' rule.Name = name LispG.RuleL[0] = rule LispG.RuleNameToIndex[name] = 0 rule = kjParser.ParseRule((-7, 'Value'), [(-6, '('), (-7, 'ListTail')]) name = 'ListRule' rule.Name = name LispG.RuleL[1] = rule LispG.RuleNameToIndex[name] = 1 rule = kjParser.ParseRule((-7, 'ListTail'), [(-7, 'Value'), (-7, 'ListTail')]) name = 'TailFull' rule.Name = name LispG.RuleL[2] = rule LispG.RuleNameToIndex[name] = 2 rule = kjParser.ParseRule((-7, 'ListTail'), [(-6, ')')]) name = 'TailEmpty' rule.Name = name LispG.RuleL[3] = rule LispG.RuleNameToIndex[name] = 3 rule = kjParser.ParseRule((-7, 'Value'), [(-8, 'var')]) name = 'Varrule' rule.Name = name LispG.RuleL[4] = rule LispG.RuleNameToIndex[name] = 4 rule = kjParser.ParseRule((-7, 'Value'), [(-8, 'int')]) name = 'Intrule' rule.Name = name LispG.RuleL[5] = rule LispG.RuleNameToIndex[name] = 5 rule = kjParser.ParseRule((-7, 'Value'), [(-8, 'str')]) name = 'Strrule' rule.Name = name LispG.RuleL[6] = rule LispG.RuleNameToIndex[name] = 6 rule = kjParser.ParseRule((-7, 'Value'), [(-6, '('), (-6, 'PRINT'), (-7, 'Value'), (-6, ')')]) name = 'PrintRule' rule.Name = name LispG.RuleL[7] = rule LispG.RuleNameToIndex[name] = 7 # DFA root nonterminal. LispG.DFA.root_nonTerminal = (-7, 'Value') # DFA state declarations. LispG.DFA.States[1] = [-5] LispG.DFA.States[2] = [-5] LispG.DFA.States[3] = [-5] LispG.DFA.States[4] = [-5] LispG.DFA.States[5] = [-5] LispG.DFA.States[6] = [-5] LispG.DFA.States[7] = [-5] LispG.DFA.States[8] = [-5] LispG.DFA.States[9] = [-5] LispG.DFA.States[10] = [-5] LispG.DFA.States[11] = [-5] LispG.DFA.States[12] = [-5] LispG.DFA.States[13] = [-5] LispG.DFA.States[14] = [-5] LispG.DFA.States[15] = [-5] LispG.DFA.States[16] = [-5] LispG.DFA.States[17] = [-5] LispG.DFA.maxState = 17 # DFA transition declarations. LispG.DFA.SetReduction(15, LispG.IndexToToken[3], 7) LispG.DFA.SetReduction(11, LispG.IndexToToken[5], 3) LispG.DFA.SetMap(7, LispG.IndexToToken[3], 3) LispG.DFA.SetReduction(14, LispG.IndexToToken[3], 2) LispG.DFA.SetMap(13, LispG.IndexToToken[3], 3) LispG.DFA.SetReduction(8, LispG.IndexToToken[4], 1) LispG.DFA.SetMap(3, LispG.IndexToToken[0], 8) LispG.DFA.SetMap(10, LispG.IndexToToken[5], 11) LispG.DFA.SetReduction(6, LispG.IndexToToken[1], 5) LispG.DFA.SetReduction(11, LispG.IndexToToken[3], 3) LispG.DFA.SetMap(7, LispG.IndexToToken[1], 6) LispG.DFA.SetReduction(6, LispG.IndexToToken[3], 5) LispG.DFA.SetMap(10, LispG.IndexToToken[3], 3) LispG.DFA.SetMap(3, LispG.IndexToToken[1], 6) LispG.DFA.SetReduction(11, LispG.IndexToToken[1], 3) LispG.DFA.SetReduction(11, LispG.IndexToToken[9], 3) LispG.DFA.SetReduction(4, LispG.IndexToToken[1], 6) LispG.DFA.SetReduction(8, LispG.IndexToToken[3], 1) LispG.DFA.SetReduction(5, LispG.IndexToToken[1], 4) LispG.DFA.SetMap(13, LispG.IndexToToken[9], 5) LispG.DFA.SetMap(1, LispG.IndexToToken[1], 6) LispG.DFA.SetMap(9, LispG.IndexToToken[9], 13) LispG.DFA.SetReduction(17, LispG.IndexToToken[8], 0) LispG.DFA.SetMap(10, LispG.IndexToToken[9], 5) LispG.DFA.SetMap(3, LispG.IndexToToken[7], 7) LispG.DFA.SetReduction(15, LispG.IndexToToken[1], 7) LispG.DFA.SetMap(1, LispG.IndexToToken[9], 5) LispG.DFA.SetMap(10, LispG.IndexToToken[0], 14) LispG.DFA.SetReduction(5, LispG.IndexToToken[4], 4) LispG.DFA.SetReduction(5, LispG.IndexToToken[5], 4) LispG.DFA.SetReduction(4, LispG.IndexToToken[5], 6) LispG.DFA.SetReduction(14, LispG.IndexToToken[9], 2) LispG.DFA.SetReduction(8, LispG.IndexToToken[1], 1) LispG.DFA.SetMap(13, LispG.IndexToToken[1], 6) LispG.DFA.SetReduction(15, LispG.IndexToToken[9], 7) LispG.DFA.SetMap(3, LispG.IndexToToken[5], 11) LispG.DFA.SetReduction(4, LispG.IndexToToken[3], 6) LispG.DFA.SetMap(3, LispG.IndexToToken[3], 3) LispG.DFA.SetMap(10, LispG.IndexToToken[1], 6) LispG.DFA.SetMap(7, LispG.IndexToToken[9], 5) LispG.DFA.SetReduction(8, LispG.IndexToToken[5], 1) LispG.DFA.SetMap(3, LispG.IndexToToken[9], 5) LispG.DFA.SetReduction(17, LispG.IndexToToken[4], 0) LispG.DFA.SetReduction(15, LispG.IndexToToken[4], 7) LispG.DFA.SetReduction(4, LispG.IndexToToken[9], 6) LispG.DFA.SetMap(3, LispG.IndexToToken[6], 9) LispG.DFA.SetMap(7, LispG.IndexToToken[2], 12) LispG.DFA.SetReduction(6, LispG.IndexToToken[9], 5) LispG.DFA.SetReduction(6, LispG.IndexToToken[5], 5) LispG.DFA.SetMap(16, LispG.IndexToToken[5], 17) LispG.DFA.SetReduction(5, LispG.IndexToToken[8], 4) LispG.DFA.SetReduction(17, LispG.IndexToToken[3], 0) LispG.DFA.SetReduction(4, LispG.IndexToToken[8], 6) LispG.DFA.SetMap(3, LispG.IndexToToken[2], 10) LispG.DFA.SetReduction(15, LispG.IndexToToken[5], 7) LispG.DFA.SetMap(10, LispG.IndexToToken[8], 4) LispG.DFA.SetReduction(6, LispG.IndexToToken[4], 5) LispG.DFA.SetMap(12, LispG.IndexToToken[5], 15) LispG.DFA.SetReduction(8, LispG.IndexToToken[8], 1) LispG.DFA.SetReduction(8, LispG.IndexToToken[9], 1) LispG.DFA.SetMap(13, LispG.IndexToToken[8], 4) LispG.DFA.SetReduction(6, LispG.IndexToToken[8], 5) LispG.DFA.SetReduction(11, LispG.IndexToToken[8], 3) LispG.DFA.SetMap(13, LispG.IndexToToken[2], 16) LispG.DFA.SetReduction(15, LispG.IndexToToken[8], 7) LispG.DFA.SetReduction(14, LispG.IndexToToken[8], 2) LispG.DFA.SetReduction(17, LispG.IndexToToken[1], 0) LispG.DFA.SetMap(10, LispG.IndexToToken[2], 10) LispG.DFA.SetMap(1, LispG.IndexToToken[8], 4) LispG.DFA.SetReduction(4, LispG.IndexToToken[4], 6) LispG.DFA.SetReduction(5, LispG.IndexToToken[3], 4) LispG.DFA.SetReduction(17, LispG.IndexToToken[9], 0) LispG.DFA.SetReduction(14, LispG.IndexToToken[4], 2) LispG.DFA.SetReduction(14, LispG.IndexToToken[1], 2) LispG.DFA.SetReduction(5, LispG.IndexToToken[9], 4) LispG.DFA.SetMap(3, LispG.IndexToToken[8], 4) LispG.DFA.SetMap(1, LispG.IndexToToken[3], 3) LispG.DFA.SetReduction(17, LispG.IndexToToken[5], 0) LispG.DFA.SetMap(7, LispG.IndexToToken[8], 4) LispG.DFA.SetReduction(14, LispG.IndexToToken[5], 2) LispG.DFA.SetReduction(11, LispG.IndexToToken[4], 3) # Clean up the grammar. LispG.CleanUp() # return the grammar. return LispG
def echo(x): return x # simple grammar stolen from a text LD0 = kjParser.LexDictionary() id = LD0.terminal("id","id",echo) plus = LD0.punctuation("+") star = LD0.punctuation("*") oppar = LD0.punctuation("(") clpar = LD0.punctuation(")") equals = LD0.punctuation("=") E = kjParser.nonterminal("E") T = kjParser.nonterminal("T") Tp = kjParser.nonterminal("Tp") Ep = kjParser.nonterminal("Ep") F = kjParser.nonterminal("F") rule1 = kjParser.ParseRule( E, [ T, Ep ] ) rule2 = kjParser.ParseRule( Ep, [ plus, T, Ep ] ) rule3 = kjParser.ParseRule( Ep, [ ] ) rule4 = kjParser.ParseRule( T, [ F, Tp ] ) rule5 = kjParser.ParseRule( Tp, [ star, F, Tp ] ) rule6 = kjParser.ParseRule( Tp, [ ] ) rule7 = kjParser.ParseRule( F, [ oppar, E, clpar ] ) rule8 = kjParser.ParseRule( F, [ id ] ) rl0 = [ rule1, rule2, rule3, rule4, rule5, rule6, rule7,rule8] rs0 = ruleset(E, rl0) rs0.CompFirst() Firstpairs = kjSet.GetPairs(rs0.First) rs0.CompFollow() Followpairs = kjSet.GetPairs(rs0.Follow) rs0.CompSLRNFA()