def _ifTest ( negn , rs ): """ add conditional test at start of block according to type of arguments arguments: negn - any negation of test rs - argument string returns: True on success, False otherwise """ # print "ifTest" , len(backl) , rs if len(rs) == 0: return _err('incomplete IF or ELIF or WHILE') if rs[0] == '[': # testing semantic feature? k = rs.find(']') if k < 0 or negn > 0: return _err('malformed semantic features: ' + rs) # print 'features=' , rs[0:k+1] fs = stb.getFeatureSet(rs[1:k].lower(),True) if fs == None: return _err('bad semantic features') # print 'bits=' , unicode(fs[0]) , unicode(fs[1]) test = ellyBits.join(fs[0],fs[1]) # print 'test=' , test store.extend([ semanticCommand.Gchkf , test ]) else: # testing local variable ar = _eqsplit(rs) # print 'ar=' , len(ar) , ar if len(ar) < 2: return _err('missing conditional test comparison') ls = ar[1].split(', ') # separator must be a comma followed by space! store.extend([ semanticCommand.Gchck+negn , ar[0].lower() , ls ]) return True
def _leftside ( stb , txt ): """ process conditions for a clause and store arguments: stb - symbol table txt - string input for single clause returns: predicate list on success, None otherwise """ # print "left side" pred = [ ] txt = txt.strip() while len(txt) > 0: side = txt[0] txt = txt[1:].lstrip() if len(txt) == 0: _err('malformed clause condition') return None k = 0 if txt[0] == '[': # semantic feature check? k = txt.find(']') # if so, look for closing bracket if k < 0: return _err('incomplete semantic features to check') p = txt[:k+1] # get semantic features # print "condition:",p try: f = featureSpecification.FeatureSpecification(stb,p,'semantic') except ellyException.FormatFailure: return _err('bad semantic features') op = semanticCommand.Crhtf if side == 'r' else semanticCommand.Clftf # print 'test:' , f.positive.hexadecimal() , f.negative.hexadecimal() test = ellyBits.join(f.positive,f.negative) # print test pred.append([ op , test ]) elif txt[0] == '(': # semantic concept check? # print "txt=\"" + txt +"\"" k = txt.find(')') # if so, look for closing parenthesis if k < 0: return _err('incomplete concept check') s = txt[1:k].strip().upper() # normalize concepts p = s.split(',') # allow for multiple disjunctive checks # print "p=\"" + p + "\"" op = semanticCommand.Crhtc if side == 'r' else semanticCommand.Clftc pred.append([ op , p ]) txt = txt[k+1:].lstrip() # advance to next predicate # print "NEXT" return pred
def _ifTest ( negn , rs ): """ add conditional test at start of block according to type of arguments arguments: negn - any negation of test rs - argument string returns: True on success, False otherwise """ # print ( "ifTest" , len(backl) , rs ) if len(rs) == 0: return _err('incomplete IF or ELIF or WHILE') if rs[0] == '[': # testing semantic feature? k = rs.find(']') if k < 0 or negn > 0: return _err('malformed semantic features: ' + rs) # print ( 'features=' , rs[0:k+1] ) fs = stb.getFeatureSet(rs[1:k].lower(),True) if fs == None: return _err('bad semantic features') # print ( 'bits=' , str(fs[0]) , str(fs[1]) ) test = ellyBits.join(fs[0],fs[1]) # print ( 'test=' , test ) store.extend([ semanticCommand.Gchkf , test ]) else: # testing local variable ar = _eqsplit(rs) # print ( 'ar=' , len(ar) , ar ) if len(ar) < 2: return _err('missing conditional test comparison') ls = ar[1].split(', ') # separator must be a comma followed by space! store.extend([ semanticCommand.Gchck+negn , ar[0].lower() , ls ]) return True
def makeTest ( self ): """ concatenate positive and negative bits of feature set for testing arguments: self returns: joined bit string to be applied in testing """ return ellyBits.join(self.positive,self.negative)
def _leftside ( stb , txt , sta ): """ process conditions for a clause and store arguments: stb - symbol table txt - string input for left side of single clause sta - for status reporting returns: predicate list on success, None otherwise """ # print "left side" pred = [ ] txt = txt.rstrip() while len(txt) > 0: txt = txt.lstrip() # print 'clause=' , txt if len(txt) <= 1: _err('malformed conditions for clause') return None side = txt[0] txt = txt[1:] if side in [ 'n' , 'p' , 'c' ]: sns = txt[0] txt = txt[1:] if sns != '<' and sns != '>': _err('invalid comparison in clause condition=' + sns) return None if side == 'n': op = semanticCommand.Cngt if sns == '>' else semanticCommand.Cnlt elif side == 'p': op = semanticCommand.Cpgt if sns == '>' else semanticCommand.Cplt else: op = semanticCommand.Ccgt if sns == '>' else semanticCommand.Cclt nd = 0 lt = len(txt) while nd < lt: if not ellyChar.isDigit(txt[nd]): break nd += 1 if nd == 0: _err('no token count for condition') return None test = int(txt[:nd]) txt = txt[nd:] pred.append([ op , test ]) continue if not side in [ 'l' , 'r' ]: _err('invalid side for test=' + side) return None k = 0 if txt[0] == '[': # semantic feature check? k = txt.find(']') # if so, look for closing bracket if k < 0: return _err('incomplete semantic features to check') p = txt[:k+1] # get semantic feature string # print "side:" , side , "test:" , p try: f = featureSpecification.FeatureSpecification(stb,p,semantic=True) except ellyException.FormatFailure: return _err('bad semantic features to check') if side == 'l': if sta.id[lS] == None: sta.id[lS] = f.id elif f.id != sta.id[lS]: _err('inconsistency: left features=' + p) return None else: if sta.id[rS] == None: sta.id[rS] = f.id elif f.id != sta.id[rS]: _err('inconsistency: right features=' + p) return None op = semanticCommand.Crhtf if side == 'r' else semanticCommand.Clftf if side == 'r': sta.rht = f else: sta.lft = f # print 'test:' , f.positive.hexadecimal() , f.negative.hexadecimal() test = ellyBits.join(f.positive,f.negative) # print test pred.append([ op , test ]) elif txt[0] == '(': # semantic concept check? # print "txt=\"" + txt +"\"" k = txt.find(')') # if so, look for closing parenthesis if k < 0: return _err('incomplete concept check') s = txt[1:k].strip().upper() # normalize concepts p = s.split(',') # allow for multiple disjunctive checks # print "p=\"" + p + "\"" op = semanticCommand.Crhtc if side == 'r' else semanticCommand.Clftc pred.append([ op , p ]) else: _err('unknown test in clause=' + side + txt) return None txt = txt[k+1:].lstrip() # advance to next predicate # print "NEXT" return pred
def __init__(self, syms, defn=None): """ initialization arguments: self - syms - symbol table for grammar defn - EllyDefinitionReader grammar definition exceptions: TableFailure on error """ self.initzn = [] # preset global variables self.proc = {} # named semantic procedures self.dctn = {} # builtin words and semantics self.pndx = {} # standalone procedures self.extens = [] # 1-branch rule self.splits = [] # 2-branch rule for _ in range(symbolTable.NMAX): self.extens.append( []) # list of 1-branch rules for each syntax type self.splits.append( []) # list of 2-branch rules for each syntax type` self.mat = derivabilityMatrix.DerivabilityMatrix(symbolTable.NMAX) # coding of predefined syntax types self.START = syms.getSyntaxTypeIndexNumber('sent') self.END = syms.getSyntaxTypeIndexNumber('end') self.UNKN = syms.getSyntaxTypeIndexNumber('unkn') self.SEPR = syms.getSyntaxTypeIndexNumber('sepr') self.XXX = syms.getSyntaxTypeIndexNumber('...') # special rule for ... type going to null fets = ellyBits.EllyBits(symbolTable.FMAX) self.arbr = grammarRule.ExtendingRule(self.XXX, fets) self.arbr.cogs = None self.arbr.gens = compile(syms, 'g', []) # special rule for SENT->SENT END ru = grammarRule.SplittingRule(self.START, fets) ru.rtyp = self.END ru.ltfet = ru.rtfet = ellyBits.join(fets, fets) ru.cogs = None ru.gens = None self.splits[self.START].append(ru) # special rule for RS (ASCII record separator) ru = grammarRule.ExtendingRule(self.SEPR, fets) ru.cogs = None ru.gens = None self.dctn[ellyChar.RS] = [ru] # should be only rule here ever # predefined generative semantic procedures self.pndx['defl'] = compile(syms, 'g', ['left']) self.pndx['defr'] = compile(syms, 'g', ['right']) self.pndx['deflr'] = compile(syms, 'g', ['left', 'right']) self.d1bp = self.pndx['defl'] # default 1-branch generative semantics self.d2bp = self.pndx['deflr'] # default 2-branch if defn != None: if not self.define(syms, defn): print >> sys.stderr, 'grammar table definition FAILed' raise ellyException.TableFailure
def __init__ ( self , syms , defn=None ): """ initialization arguments: self - syms - symbol table for grammar defn - EllyDefinitionReader grammar definition exceptions: TableFailure on error """ self.initzn = [ ] # preset global variables self.proc = { } # named semantic procedures self.dctn = { } # builtin words and semantics self.pndx = { } # standalone procedures self.extens = [ ] # 1-branch rule self.splits = [ ] # 2-branch rule for _ in range(symbolTable.NMAX): self.extens.append([ ]) # list of 1-branch rules for each syntax type self.splits.append([ ]) # list of 2-branch rules for each syntax type` self.mat = derivabilityMatrix.DerivabilityMatrix(symbolTable.NMAX) # coding of predefined syntax types self.START = syms.getSyntaxTypeIndexNumber('sent') self.END = syms.getSyntaxTypeIndexNumber('end') self.UNKN = syms.getSyntaxTypeIndexNumber('unkn') self.XXX = syms.getSyntaxTypeIndexNumber('...') # special rule for ... type going to null fets = ellyBits.EllyBits(symbolTable.FMAX) self.arbr = grammarRule.ExtendingRule(self.XXX,fets) self.arbr.cogs = None self.arbr.gens = compile(syms,'g',[ ]) # special rule for SENT->SENT END ru = grammarRule.SplittingRule(self.START,fets) ru.rtyp = self.END ru.ltfet = ru.rtfet = ellyBits.join(fets,fets) ru.cogs = None ru.gens = None self.splits[self.START].append(ru) # predefined generative semantic procedures self.pndx['defl'] = compile(syms,'g',['left']) self.pndx['defr'] = compile(syms,'g',['right']) self.pndx['deflr'] = compile(syms,'g',['left','right']) self.d1bp = self.pndx['defl'] # default 1-branch generative semantics self.d2bp = self.pndx['deflr'] # default 2-branch if defn != None: if not self.define(syms,defn): print >> sys.stderr , 'grammar table definition FAILed' raise ellyException.TableFailure
def _leftside(stb, txt, sta): """ process conditions for a clause and store arguments: stb - symbol table txt - string input for left side of single clause sta - for status reporting returns: predicate list on success, None otherwise """ # print ( "left side" ) pred = [] txt = txt.rstrip() while len(txt) > 0: txt = txt.lstrip() # print ( 'clause=' , txt ) if len(txt) <= 1: _err('malformed conditions for clause') return None side = txt[0] txt = txt[1:] if side in ['n', 'p', 'c']: sns = txt[0] txt = txt[1:] if sns != '<' and sns != '>': _err('invalid comparison in clause condition=' + sns) return None if side == 'n': op = semanticCommand.Cngt if sns == '>' else semanticCommand.Cnlt elif side == 'p': op = semanticCommand.Cpgt if sns == '>' else semanticCommand.Cplt else: op = semanticCommand.Ccgt if sns == '>' else semanticCommand.Cclt nd = 0 lt = len(txt) while nd < lt: if not ellyChar.isDigit(txt[nd]): break nd += 1 if nd == 0: _err('no token count for condition') return None test = int(txt[:nd]) txt = txt[nd:] pred.append([op, test]) continue if not side in ['l', 'r']: _err('invalid side for test=' + side) return None k = 0 if txt[0] == '[': # semantic feature check? k = txt.find(']') # if so, look for closing bracket if k < 0: return _err('incomplete semantic features to check') p = txt[:k + 1] # get semantic feature string # print ( "side:" , side , "test:" , p ) try: f = featureSpecification.FeatureSpecification(stb, p, semantic=True) except ellyException.FormatFailure: return _err('bad semantic features to check') if side == 'l': if sta.id[lS] == None: sta.id[lS] = f.id elif f.id != sta.id[lS]: _err('inconsistency: left features=' + p) return None else: if sta.id[rS] == None: sta.id[rS] = f.id elif f.id != sta.id[rS]: _err('inconsistency: right features=' + p) return None op = semanticCommand.Crhtf if side == 'r' else semanticCommand.Clftf if side == 'r': sta.rht = f else: sta.lft = f # print ( 'test:' , f.positive.hexadecimal() , f.negative.hexadecimal() ) test = ellyBits.join(f.positive, f.negative) # print ( test ) pred.append([op, test]) elif txt[0] == '(': # semantic concept check? # print ( "txt=\"" + txt +"\"" ) k = txt.find(')') # if so, look for closing parenthesis if k < 0: return _err('incomplete concept check') s = txt[1:k].strip().upper() # normalize concepts p = s.split(',') # allow for multiple disjunctive checks # print ( "p=\"" + p + "\"" ) op = semanticCommand.Crhtc if side == 'r' else semanticCommand.Clftc pred.append([op, p]) else: _err('unknown test in clause=' + side + txt) return None txt = txt[k + 1:].lstrip() # advance to next predicate # print ( "NEXT" ) return pred