# Begin -- grammar generated by Yapps import sys, re from yapps import runtime class LinglishScanner(runtime.Scanner): patterns = [ ('","', re.compile(',')), ('"with"', re.compile('with')), ('"to"', re.compile('to')), ('"via"', re.compile('via')), ('"tell"', re.compile('tell')), ('"it"', re.compile('it')), ('"the result"', re.compile('the result')), ('"for"', re.compile('for')), ('"ask"', re.compile('ask')), ('"to be"', re.compile('to be')), ('"as"', re.compile('as')), ('"on the Session bus"', re.compile('on the Session bus')), ('"on Session"', re.compile('on Session')), ('"on the System bus"', re.compile('on the System bus')), ('"on System"', re.compile('on System')), ('"times for"', re.compile('times for')), ('"wait"', re.compile('wait')), ('"wait repeatedly for"', re.compile('wait repeatedly for')), ('"wait for"', re.compile('wait for')), ('"run"', re.compile('run')), ('"then"', re.compile('then')), ('\\s+', re.compile('\\s+')), ('END', re.compile('$')), ('WORD', re.compile('[a-zA-Z_.]+')), ('NUM', re.compile('[0-9]+')), ('POSSESSIVE', re.compile("[a-zA-Z_.]+'s")), ('SINGLEQUOTED', re.compile("'[^']*'")), ('DOUBLEQUOTED', re.compile('"[^"]*"')), ] def __init__(self, str,*args,**kw): runtime.Scanner.__init__(self,None,{'\\s+':None,},str,*args,**kw) class Linglish(runtime.Parser): Context = runtime.Context def script(self, _parent=None): _context = self.Context(_parent, self._scanner, 'script', []) scriptlist = [] while 1: _token = self._peek('END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) if _token != 'END': command = self.command(_context) scriptlist.append(command) if self._peek('"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '"then"': self._scan('"then"', context=_context) else: # == 'END' END = self._scan('END', context=_context) return scriptlist def command(self, _parent=None): _context = self.Context(_parent, self._scanner, 'command', []) _token = self._peek('"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) if _token == '"tell"': tell = self.tell(_context) return tell elif _token not in ['"ask"', '"run"']: wait = self.wait(_context) return wait elif _token == '"ask"': ask = self.ask(_context) return ask else: # == '"run"' run = self.run(_context) return run def run(self, _parent=None): _context = self.Context(_parent, self._scanner, 'run', []) dic = {"cmd": "run"} self._scan('"run"', context=_context) quoted = self.quoted(_context) dic["runnee"] = quoted return dic def wait(self, _parent=None): _context = self.Context(_parent, self._scanner, 'wait', []) dic = {"cmd": "wait"} _token = self._peek('"wait for"', '"wait repeatedly for"', '"wait"', context=_context) if _token == '"wait for"': self._scan('"wait for"', context=_context) dic["wait_count"] = 1 elif _token == '"wait repeatedly for"': self._scan('"wait repeatedly for"', context=_context) dic["wait_count"] = -1 else: # == '"wait"' self._scan('"wait"', context=_context) NUM = self._scan('NUM', context=_context) dic["wait_count"] = int(NUM) self._scan('"times for"', context=_context) possessive = self.possessive(_context) dic['application'] = possessive WORD = self._scan('WORD', context=_context) dic['signal'] = WORD dic["bus"] = None if self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', '"as"', '"to be"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) in ['"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"']: _token = self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', context=_context) if _token == '"on System"': self._scan('"on System"', context=_context) dic["bus"] = "System" elif _token == '"on the System bus"': self._scan('"on the System bus"', context=_context) dic["bus"] = "System" elif _token == '"on Session"': self._scan('"on Session"', context=_context) dic["bus"] = "Session" else: # == '"on the Session bus"' self._scan('"on the Session bus"', context=_context) dic["bus"] = "Session" dic["retvals"] = [] if self._peek('"as"', '"to be"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '"as"': self._scan('"as"', context=_context) paramlist = self.paramlist(_context) dic["retvals"] = paramlist dic["required_value"] = None if self._peek('"to be"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '"to be"': self._scan('"to be"', context=_context) rvalue = self.rvalue(_context) dic["required_value"] = rvalue return dic def ask(self, _parent=None): _context = self.Context(_parent, self._scanner, 'ask', []) self._scan('"ask"', context=_context) dic = {"cmd": "ask"} _token = self._peek('WORD', 'POSSESSIVE', context=_context) if _token == 'POSSESSIVE': possessive = self.possessive(_context) dic['application'] = possessive WORD = self._scan('WORD', context=_context) dic['obj'] = WORD else: # == 'WORD' WORD = self._scan('WORD', context=_context) dic['application'] = WORD dic['obj'] = None dic["bus"] = None if self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', '"for"', context=_context) != '"for"': _token = self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', context=_context) if _token == '"on System"': self._scan('"on System"', context=_context) dic["bus"] = "System" elif _token == '"on the System bus"': self._scan('"on the System bus"', context=_context) dic["bus"] = "System" elif _token == '"on Session"': self._scan('"on Session"', context=_context) dic["bus"] = "Session" else: # == '"on the Session bus"' self._scan('"on the Session bus"', context=_context) dic["bus"] = "Session" self._scan('"for"', context=_context) WORD = self._scan('WORD', context=_context) dic['prop'] = WORD return dic def rvalue(self, _parent=None): _context = self.Context(_parent, self._scanner, 'rvalue', []) _token = self._peek('NUM', 'WORD', '"the result"', '"it"', 'SINGLEQUOTED', 'DOUBLEQUOTED', context=_context) if _token in ['SINGLEQUOTED', 'DOUBLEQUOTED']: quoted = self.quoted(_context) return {"type": "string", "value": quoted} elif _token == 'NUM': NUM = self._scan('NUM', context=_context) return {"type": "integer", "value": NUM} elif _token == 'WORD': WORD = self._scan('WORD', context=_context) return {"type": "var", "name": WORD} elif _token == '"the result"': self._scan('"the result"', context=_context) return {"type": "var", "name": "the result"} else: # == '"it"' self._scan('"it"', context=_context) return {"type": "var", "name": "it"} def quoted(self, _parent=None): _context = self.Context(_parent, self._scanner, 'quoted', []) _token = self._peek('SINGLEQUOTED', 'DOUBLEQUOTED', context=_context) if _token == 'SINGLEQUOTED': SINGLEQUOTED = self._scan('SINGLEQUOTED', context=_context) return SINGLEQUOTED[1:-1] else: # == 'DOUBLEQUOTED' DOUBLEQUOTED = self._scan('DOUBLEQUOTED', context=_context) return DOUBLEQUOTED[1:-1] def tell(self, _parent=None): _context = self.Context(_parent, self._scanner, 'tell', []) self._scan('"tell"', context=_context) dic = { "cmd": "tell" } _token = self._peek('WORD', 'POSSESSIVE', context=_context) if _token == 'WORD': WORD = self._scan('WORD', context=_context) dic["application"] = WORD dic["obj"] = None else: # == 'POSSESSIVE' possessive = self.possessive(_context) dic["application"] = possessive WORD = self._scan('WORD', context=_context) dic["obj"] = WORD dic["interface"] = None if self._peek('"via"', '"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', '"to"', context=_context) == '"via"': self._scan('"via"', context=_context) WORD = self._scan('WORD', context=_context) dic["interface"] = WORD dic["bus"] = None if self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', '"to"', context=_context) != '"to"': _token = self._peek('"on System"', '"on the System bus"', '"on Session"', '"on the Session bus"', context=_context) if _token == '"on System"': self._scan('"on System"', context=_context) dic["bus"] = "System" elif _token == '"on the System bus"': self._scan('"on the System bus"', context=_context) dic["bus"] = "System" elif _token == '"on Session"': self._scan('"on Session"', context=_context) dic["bus"] = "Session" else: # == '"on the Session bus"' self._scan('"on the Session bus"', context=_context) dic["bus"] = "Session" self._scan('"to"', context=_context) WORD = self._scan('WORD', context=_context) dic["action"] = WORD dic["params"] = [] if self._peek('"with"', '"as"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '"with"': self._scan('"with"', context=_context) paramlist = self.paramlist(_context) dic["params"] = paramlist dic["retvals"] = [] if self._peek('"as"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '"as"': self._scan('"as"', context=_context) paramlist = self.paramlist(_context) dic["retvals"] = paramlist return dic def possessive(self, _parent=None): _context = self.Context(_parent, self._scanner, 'possessive', []) POSSESSIVE = self._scan('POSSESSIVE', context=_context) return POSSESSIVE[:-2] def paramlist(self, _parent=None): _context = self.Context(_parent, self._scanner, 'paramlist', []) params = [] rvalue = self.rvalue(_context) params.append(rvalue) while self._peek('","', '"to be"', '"as"', '"then"', 'END', '"tell"', '"wait for"', '"wait repeatedly for"', '"wait"', '"ask"', '"run"', context=_context) == '","': self._scan('","', context=_context) rvalue = self.rvalue(_context) params.append(rvalue) return params def parse(rule, text): P = Linglish(LinglishScanner(text)) return runtime.wrap_error_reporter(P, rule) if __name__ == '__main__': from sys import argv, stdin if len(argv) >= 2: if len(argv) >= 3: f = open(argv[2],'r') else: f = stdin print parse(argv[1], f.read()) else: print >>sys.stderr, 'Args: []' # End -- grammar generated by Yapps