xpathparser.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. # -*- test-case-name: twisted.words.test.test_xpath -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. # pylint: disable=W9401,W9402
  5. # DO NOT EDIT xpathparser.py!
  6. #
  7. # It is generated from xpathparser.g using Yapps. Make needed changes there.
  8. # This also means that the generated Python may not conform to Twisted's coding
  9. # standards, so it is wrapped in exec to prevent automated checkers from
  10. # complaining.
  11. # HOWTO Generate me:
  12. #
  13. # 1.) Grab a copy of yapps2:
  14. # https://github.com/smurfix/yapps
  15. #
  16. # Note: Do NOT use the package in debian/ubuntu as it has incompatible
  17. # modifications. The original at http://theory.stanford.edu/~amitp/yapps/
  18. # hasn't been touched since 2003 and has not been updated to work with
  19. # Python 3.
  20. #
  21. # 2.) Generate the grammar:
  22. #
  23. # yapps2 xpathparser.g xpathparser.py.proto
  24. #
  25. # 3.) Edit the output to depend on the embedded runtime, and remove extraneous
  26. # imports:
  27. #
  28. # sed -e '/^# Begin/,${/^[^ ].*mport/d}' -e 's/runtime\.//g' \
  29. # -e "s/^\(from __future\)/exec(r'''\n\1/" -e"\$a''')"
  30. # xpathparser.py.proto > xpathparser.py
  31. """
  32. XPath Parser.
  33. Besides the parser code produced by Yapps, this module also defines the
  34. parse-time exception classes, a scanner class, a base class for parsers
  35. produced by Yapps, and a context class that keeps track of the parse stack.
  36. These have been copied from the Yapps runtime module.
  37. """
  38. exec(
  39. r'''
  40. from __future__ import print_function
  41. import sys, re
  42. MIN_WINDOW=4096
  43. # File lookup window
  44. class SyntaxError(Exception):
  45. """When we run into an unexpected token, this is the exception to use"""
  46. def __init__(self, pos=None, msg="Bad Token", context=None):
  47. Exception.__init__(self)
  48. self.pos = pos
  49. self.msg = msg
  50. self.context = context
  51. def __str__(self):
  52. if not self.pos: return 'SyntaxError'
  53. else: return 'SyntaxError@%s(%s)' % (repr(self.pos), self.msg)
  54. class NoMoreTokens(Exception):
  55. """Another exception object, for when we run out of tokens"""
  56. pass
  57. class Token:
  58. """Yapps token.
  59. This is a container for a scanned token.
  60. """
  61. def __init__(self, type,value, pos=None):
  62. """Initialize a token."""
  63. self.type = type
  64. self.value = value
  65. self.pos = pos
  66. def __repr__(self):
  67. output = '<%s: %s' % (self.type, repr(self.value))
  68. if self.pos:
  69. output += " @ "
  70. if self.pos[0]:
  71. output += "%s:" % self.pos[0]
  72. if self.pos[1]:
  73. output += "%d" % self.pos[1]
  74. if self.pos[2] is not None:
  75. output += ".%d" % self.pos[2]
  76. output += ">"
  77. return output
  78. in_name=0
  79. class Scanner:
  80. """Yapps scanner.
  81. The Yapps scanner can work in context sensitive or context
  82. insensitive modes. The token(i) method is used to retrieve the
  83. i-th token. It takes a restrict set that limits the set of tokens
  84. it is allowed to return. In context sensitive mode, this restrict
  85. set guides the scanner. In context insensitive mode, there is no
  86. restriction (the set is always the full set of tokens).
  87. """
  88. def __init__(self, patterns, ignore, input="",
  89. file=None,filename=None,stacked=False):
  90. """Initialize the scanner.
  91. Parameters:
  92. patterns : [(terminal, uncompiled regex), ...] or None
  93. ignore : {terminal:None, ...}
  94. input : string
  95. If patterns is None, we assume that the subclass has
  96. defined self.patterns : [(terminal, compiled regex), ...].
  97. Note that the patterns parameter expects uncompiled regexes,
  98. whereas the self.patterns field expects compiled regexes.
  99. The 'ignore' value is either None or a callable, which is called
  100. with the scanner and the to-be-ignored match object; this can
  101. be used for include file or comment handling.
  102. """
  103. if not filename:
  104. global in_name
  105. filename="<f.%d>" % in_name
  106. in_name += 1
  107. self.input = input
  108. self.ignore = ignore
  109. self.file = file
  110. self.filename = filename
  111. self.pos = 0
  112. self.del_pos = 0 # skipped
  113. self.line = 1
  114. self.del_line = 0 # skipped
  115. self.col = 0
  116. self.tokens = []
  117. self.stack = None
  118. self.stacked = stacked
  119. self.last_read_token = None
  120. self.last_token = None
  121. self.last_types = None
  122. if patterns is not None:
  123. # Compile the regex strings into regex objects
  124. self.patterns = []
  125. for terminal, regex in patterns:
  126. self.patterns.append( (terminal, re.compile(regex)) )
  127. def stack_input(self, input="", file=None, filename=None):
  128. """Temporarily parse from a second file."""
  129. # Already reading from somewhere else: Go on top of that, please.
  130. if self.stack:
  131. # autogenerate a recursion-level-identifying filename
  132. if not filename:
  133. filename = 1
  134. else:
  135. try:
  136. filename += 1
  137. except TypeError:
  138. pass
  139. # now pass off to the include file
  140. self.stack.stack_input(input,file,filename)
  141. else:
  142. try:
  143. filename += 0
  144. except TypeError:
  145. pass
  146. else:
  147. filename = "<str_%d>" % filename
  148. # self.stack = object.__new__(self.__class__)
  149. # Scanner.__init__(self.stack,self.patterns,self.ignore,input,file,filename, stacked=True)
  150. # Note that the pattern+ignore are added by the generated
  151. # scanner code
  152. self.stack = self.__class__(input,file,filename, stacked=True)
  153. def get_pos(self):
  154. """Return a file/line/char tuple."""
  155. if self.stack: return self.stack.get_pos()
  156. return (self.filename, self.line+self.del_line, self.col)
  157. # def __repr__(self):
  158. # """Print the last few tokens that have been scanned in"""
  159. # output = ''
  160. # for t in self.tokens:
  161. # output += '%s\n' % (repr(t),)
  162. # return output
  163. def print_line_with_pointer(self, pos, length=0, out=sys.stderr):
  164. """Print the line of 'text' that includes position 'p',
  165. along with a second line with a single caret (^) at position p"""
  166. file,line,p = pos
  167. if file != self.filename:
  168. if self.stack: return self.stack.print_line_with_pointer(pos,length=length,out=out)
  169. print >>out, "(%s: not in input buffer)" % file
  170. return
  171. text = self.input
  172. p += length-1 # starts at pos 1
  173. origline=line
  174. line -= self.del_line
  175. spos=0
  176. if line > 0:
  177. while 1:
  178. line = line - 1
  179. try:
  180. cr = text.index("\n",spos)
  181. except ValueError:
  182. if line:
  183. text = ""
  184. break
  185. if line == 0:
  186. text = text[spos:cr]
  187. break
  188. spos = cr+1
  189. else:
  190. print >>out, "(%s:%d not in input buffer)" % (file,origline)
  191. return
  192. # Now try printing part of the line
  193. text = text[max(p-80, 0):p+80]
  194. p = p - max(p-80, 0)
  195. # Strip to the left
  196. i = text[:p].rfind('\n')
  197. j = text[:p].rfind('\r')
  198. if i < 0 or (0 <= j < i): i = j
  199. if 0 <= i < p:
  200. p = p - i - 1
  201. text = text[i+1:]
  202. # Strip to the right
  203. i = text.find('\n', p)
  204. j = text.find('\r', p)
  205. if i < 0 or (0 <= j < i): i = j
  206. if i >= 0:
  207. text = text[:i]
  208. # Now shorten the text
  209. while len(text) > 70 and p > 60:
  210. # Cut off 10 chars
  211. text = "..." + text[10:]
  212. p = p - 7
  213. # Now print the string, along with an indicator
  214. print >>out, '> ',text
  215. print >>out, '> ',' '*p + '^'
  216. def grab_input(self):
  217. """Get more input if possible."""
  218. if not self.file: return
  219. if len(self.input) - self.pos >= MIN_WINDOW: return
  220. data = self.file.read(MIN_WINDOW)
  221. if data is None or data == "":
  222. self.file = None
  223. # Drop bytes from the start, if necessary.
  224. if self.pos > 2*MIN_WINDOW:
  225. self.del_pos += MIN_WINDOW
  226. self.del_line += self.input[:MIN_WINDOW].count("\n")
  227. self.pos -= MIN_WINDOW
  228. self.input = self.input[MIN_WINDOW:] + data
  229. else:
  230. self.input = self.input + data
  231. def getchar(self):
  232. """Return the next character."""
  233. self.grab_input()
  234. c = self.input[self.pos]
  235. self.pos += 1
  236. return c
  237. def token(self, restrict, context=None):
  238. """Scan for another token."""
  239. while 1:
  240. if self.stack:
  241. try:
  242. return self.stack.token(restrict, context)
  243. except StopIteration:
  244. self.stack = None
  245. # Keep looking for a token, ignoring any in self.ignore
  246. self.grab_input()
  247. # special handling for end-of-file
  248. if self.stacked and self.pos==len(self.input):
  249. raise StopIteration
  250. # Search the patterns for the longest match, with earlier
  251. # tokens in the list having preference
  252. best_match = -1
  253. best_pat = '(error)'
  254. best_m = None
  255. for p, regexp in self.patterns:
  256. # First check to see if we're ignoring this token
  257. if restrict and p not in restrict and p not in self.ignore:
  258. continue
  259. m = regexp.match(self.input, self.pos)
  260. if m and m.end()-m.start() > best_match:
  261. # We got a match that's better than the previous one
  262. best_pat = p
  263. best_match = m.end()-m.start()
  264. best_m = m
  265. # If we didn't find anything, raise an error
  266. if best_pat == '(error)' and best_match < 0:
  267. msg = 'Bad Token'
  268. if restrict:
  269. msg = 'Trying to find one of '+', '.join(restrict)
  270. raise SyntaxError(self.get_pos(), msg, context=context)
  271. ignore = best_pat in self.ignore
  272. value = self.input[self.pos:self.pos+best_match]
  273. if not ignore:
  274. tok=Token(type=best_pat, value=value, pos=self.get_pos())
  275. self.pos += best_match
  276. npos = value.rfind("\n")
  277. if npos > -1:
  278. self.col = best_match-npos
  279. self.line += value.count("\n")
  280. else:
  281. self.col += best_match
  282. # If we found something that isn't to be ignored, return it
  283. if not ignore:
  284. if len(self.tokens) >= 10:
  285. del self.tokens[0]
  286. self.tokens.append(tok)
  287. self.last_read_token = tok
  288. # print repr(tok)
  289. return tok
  290. else:
  291. ignore = self.ignore[best_pat]
  292. if ignore:
  293. ignore(self, best_m)
  294. def peek(self, *types, **kw):
  295. """Returns the token type for lookahead; if there are any args
  296. then the list of args is the set of token types to allow"""
  297. context = kw.get("context",None)
  298. if self.last_token is None:
  299. self.last_types = types
  300. self.last_token = self.token(types,context)
  301. elif self.last_types:
  302. for t in types:
  303. if t not in self.last_types:
  304. raise NotImplementedError("Unimplemented: restriction set changed")
  305. return self.last_token.type
  306. def scan(self, type, **kw):
  307. """Returns the matched text, and moves to the next token"""
  308. context = kw.get("context",None)
  309. if self.last_token is None:
  310. tok = self.token([type],context)
  311. else:
  312. if self.last_types and type not in self.last_types:
  313. raise NotImplementedError("Unimplemented: restriction set changed")
  314. tok = self.last_token
  315. self.last_token = None
  316. if tok.type != type:
  317. if not self.last_types: self.last_types=[]
  318. raise SyntaxError(tok.pos, 'Trying to find '+type+': '+ ', '.join(self.last_types)+", got "+tok.type, context=context)
  319. return tok.value
  320. class Parser:
  321. """Base class for Yapps-generated parsers.
  322. """
  323. def __init__(self, scanner):
  324. self._scanner = scanner
  325. def _stack(self, input="",file=None,filename=None):
  326. """Temporarily read from someplace else"""
  327. self._scanner.stack_input(input,file,filename)
  328. self._tok = None
  329. def _peek(self, *types, **kw):
  330. """Returns the token type for lookahead; if there are any args
  331. then the list of args is the set of token types to allow"""
  332. return self._scanner.peek(*types, **kw)
  333. def _scan(self, type, **kw):
  334. """Returns the matched text, and moves to the next token"""
  335. return self._scanner.scan(type, **kw)
  336. class Context:
  337. """Class to represent the parser's call stack.
  338. Every rule creates a Context that links to its parent rule. The
  339. contexts can be used for debugging.
  340. """
  341. def __init__(self, parent, scanner, rule, args=()):
  342. """Create a new context.
  343. Args:
  344. parent: Context object or None
  345. scanner: Scanner object
  346. rule: string (name of the rule)
  347. args: tuple listing parameters to the rule
  348. """
  349. self.parent = parent
  350. self.scanner = scanner
  351. self.rule = rule
  352. self.args = args
  353. while scanner.stack: scanner = scanner.stack
  354. self.token = scanner.last_read_token
  355. def __str__(self):
  356. output = ''
  357. if self.parent: output = str(self.parent) + ' > '
  358. output += self.rule
  359. return output
  360. def print_error(err, scanner, max_ctx=None):
  361. """Print error messages, the parser stack, and the input text -- for human-readable error messages."""
  362. # NOTE: this function assumes 80 columns :-(
  363. # Figure out the line number
  364. pos = err.pos
  365. if not pos:
  366. pos = scanner.get_pos()
  367. file_name, line_number, column_number = pos
  368. print('%s:%d:%d: %s' % (file_name, line_number, column_number, err.msg), file=sys.stderr)
  369. scanner.print_line_with_pointer(pos)
  370. context = err.context
  371. token = None
  372. while context:
  373. print('while parsing %s%s:' % (context.rule, tuple(context.args)), file=sys.stderr)
  374. if context.token:
  375. token = context.token
  376. if token:
  377. scanner.print_line_with_pointer(token.pos, length=len(token.value))
  378. context = context.parent
  379. if max_ctx:
  380. max_ctx = max_ctx-1
  381. if not max_ctx:
  382. break
  383. def wrap_error_reporter(parser, rule, *args,**kw):
  384. try:
  385. return getattr(parser, rule)(*args,**kw)
  386. except SyntaxError as e:
  387. print_error(e, parser._scanner)
  388. except NoMoreTokens:
  389. print('Could not complete parsing; stopped around here:', file=sys.stderr)
  390. print(parser._scanner, file=sys.stderr)
  391. from twisted.words.xish.xpath import AttribValue, BooleanValue, CompareValue
  392. from twisted.words.xish.xpath import Function, IndexValue, LiteralValue
  393. from twisted.words.xish.xpath import _AnyLocation, _Location
  394. # Begin -- grammar generated by Yapps
  395. class XPathParserScanner(Scanner):
  396. patterns = [
  397. ('","', re.compile(',')),
  398. ('"@"', re.compile('@')),
  399. ('"\\)"', re.compile('\\)')),
  400. ('"\\("', re.compile('\\(')),
  401. ('"\\]"', re.compile('\\]')),
  402. ('"\\["', re.compile('\\[')),
  403. ('"//"', re.compile('//')),
  404. ('"/"', re.compile('/')),
  405. ('\\s+', re.compile('\\s+')),
  406. ('INDEX', re.compile('[0-9]+')),
  407. ('WILDCARD', re.compile('\\*')),
  408. ('IDENTIFIER', re.compile('[a-zA-Z][a-zA-Z0-9_\\-]*')),
  409. ('ATTRIBUTE', re.compile('\\@[a-zA-Z][a-zA-Z0-9_\\-]*')),
  410. ('FUNCNAME', re.compile('[a-zA-Z][a-zA-Z0-9_]*')),
  411. ('CMP_EQ', re.compile('\\=')),
  412. ('CMP_NE', re.compile('\\!\\=')),
  413. ('STR_DQ', re.compile('"([^"]|(\\"))*?"')),
  414. ('STR_SQ', re.compile("'([^']|(\\'))*?'")),
  415. ('OP_AND', re.compile('and')),
  416. ('OP_OR', re.compile('or')),
  417. ('END', re.compile('$')),
  418. ]
  419. def __init__(self, str,*args,**kw):
  420. Scanner.__init__(self,None,{'\\s+':None,},str,*args,**kw)
  421. class XPathParser(Parser):
  422. Context = Context
  423. def XPATH(self, _parent=None):
  424. _context = self.Context(_parent, self._scanner, 'XPATH', [])
  425. PATH = self.PATH(_context)
  426. result = PATH; current = result
  427. while self._peek('END', '"/"', '"//"', context=_context) != 'END':
  428. PATH = self.PATH(_context)
  429. current.childLocation = PATH; current = current.childLocation
  430. END = self._scan('END', context=_context)
  431. return result
  432. def PATH(self, _parent=None):
  433. _context = self.Context(_parent, self._scanner, 'PATH', [])
  434. _token = self._peek('"/"', '"//"', context=_context)
  435. if _token == '"/"':
  436. self._scan('"/"', context=_context)
  437. result = _Location()
  438. else: # == '"//"'
  439. self._scan('"//"', context=_context)
  440. result = _AnyLocation()
  441. _token = self._peek('IDENTIFIER', 'WILDCARD', context=_context)
  442. if _token == 'IDENTIFIER':
  443. IDENTIFIER = self._scan('IDENTIFIER', context=_context)
  444. result.elementName = IDENTIFIER
  445. else: # == 'WILDCARD'
  446. WILDCARD = self._scan('WILDCARD', context=_context)
  447. result.elementName = None
  448. while self._peek('"\\["', 'END', '"/"', '"//"', context=_context) == '"\\["':
  449. self._scan('"\\["', context=_context)
  450. PREDICATE = self.PREDICATE(_context)
  451. result.predicates.append(PREDICATE)
  452. self._scan('"\\]"', context=_context)
  453. return result
  454. def PREDICATE(self, _parent=None):
  455. _context = self.Context(_parent, self._scanner, 'PREDICATE', [])
  456. _token = self._peek('INDEX', '"\\("', '"@"', 'FUNCNAME', 'STR_DQ', 'STR_SQ', context=_context)
  457. if _token != 'INDEX':
  458. EXPR = self.EXPR(_context)
  459. return EXPR
  460. else: # == 'INDEX'
  461. INDEX = self._scan('INDEX', context=_context)
  462. return IndexValue(INDEX)
  463. def EXPR(self, _parent=None):
  464. _context = self.Context(_parent, self._scanner, 'EXPR', [])
  465. FACTOR = self.FACTOR(_context)
  466. e = FACTOR
  467. while self._peek('OP_AND', 'OP_OR', '"\\)"', '"\\]"', context=_context) in ['OP_AND', 'OP_OR']:
  468. BOOLOP = self.BOOLOP(_context)
  469. FACTOR = self.FACTOR(_context)
  470. e = BooleanValue(e, BOOLOP, FACTOR)
  471. return e
  472. def BOOLOP(self, _parent=None):
  473. _context = self.Context(_parent, self._scanner, 'BOOLOP', [])
  474. _token = self._peek('OP_AND', 'OP_OR', context=_context)
  475. if _token == 'OP_AND':
  476. OP_AND = self._scan('OP_AND', context=_context)
  477. return OP_AND
  478. else: # == 'OP_OR'
  479. OP_OR = self._scan('OP_OR', context=_context)
  480. return OP_OR
  481. def FACTOR(self, _parent=None):
  482. _context = self.Context(_parent, self._scanner, 'FACTOR', [])
  483. _token = self._peek('"\\("', '"@"', 'FUNCNAME', 'STR_DQ', 'STR_SQ', context=_context)
  484. if _token != '"\\("':
  485. TERM = self.TERM(_context)
  486. return TERM
  487. else: # == '"\\("'
  488. self._scan('"\\("', context=_context)
  489. EXPR = self.EXPR(_context)
  490. self._scan('"\\)"', context=_context)
  491. return EXPR
  492. def TERM(self, _parent=None):
  493. _context = self.Context(_parent, self._scanner, 'TERM', [])
  494. VALUE = self.VALUE(_context)
  495. t = VALUE
  496. if self._peek('CMP_EQ', 'CMP_NE', 'OP_AND', 'OP_OR', '"\\)"', '"\\]"', context=_context) in ['CMP_EQ', 'CMP_NE']:
  497. CMP = self.CMP(_context)
  498. VALUE = self.VALUE(_context)
  499. t = CompareValue(t, CMP, VALUE)
  500. return t
  501. def VALUE(self, _parent=None):
  502. _context = self.Context(_parent, self._scanner, 'VALUE', [])
  503. _token = self._peek('"@"', 'FUNCNAME', 'STR_DQ', 'STR_SQ', context=_context)
  504. if _token == '"@"':
  505. self._scan('"@"', context=_context)
  506. IDENTIFIER = self._scan('IDENTIFIER', context=_context)
  507. return AttribValue(IDENTIFIER)
  508. elif _token == 'FUNCNAME':
  509. FUNCNAME = self._scan('FUNCNAME', context=_context)
  510. f = Function(FUNCNAME); args = []
  511. self._scan('"\\("', context=_context)
  512. if self._peek('"\\)"', '"@"', 'FUNCNAME', '","', 'STR_DQ', 'STR_SQ', context=_context) not in ['"\\)"', '","']:
  513. VALUE = self.VALUE(_context)
  514. args.append(VALUE)
  515. while self._peek('","', '"\\)"', context=_context) == '","':
  516. self._scan('","', context=_context)
  517. VALUE = self.VALUE(_context)
  518. args.append(VALUE)
  519. self._scan('"\\)"', context=_context)
  520. f.setParams(*args); return f
  521. else: # in ['STR_DQ', 'STR_SQ']
  522. STR = self.STR(_context)
  523. return LiteralValue(STR[1:len(STR)-1])
  524. def CMP(self, _parent=None):
  525. _context = self.Context(_parent, self._scanner, 'CMP', [])
  526. _token = self._peek('CMP_EQ', 'CMP_NE', context=_context)
  527. if _token == 'CMP_EQ':
  528. CMP_EQ = self._scan('CMP_EQ', context=_context)
  529. return CMP_EQ
  530. else: # == 'CMP_NE'
  531. CMP_NE = self._scan('CMP_NE', context=_context)
  532. return CMP_NE
  533. def STR(self, _parent=None):
  534. _context = self.Context(_parent, self._scanner, 'STR', [])
  535. _token = self._peek('STR_DQ', 'STR_SQ', context=_context)
  536. if _token == 'STR_DQ':
  537. STR_DQ = self._scan('STR_DQ', context=_context)
  538. return STR_DQ
  539. else: # == 'STR_SQ'
  540. STR_SQ = self._scan('STR_SQ', context=_context)
  541. return STR_SQ
  542. def parse(rule, text):
  543. P = XPathParser(XPathParserScanner(text))
  544. return wrap_error_reporter(P, rule)
  545. if __name__ == '__main__':
  546. from sys import argv, stdin
  547. if len(argv) >= 2:
  548. if len(argv) >= 3:
  549. f = open(argv[2],'r')
  550. else:
  551. f = stdin
  552. print(parse(argv[1], f.read()))
  553. else: print ('Args: <rule> [<filename>]', file=sys.stderr)
  554. # End -- grammar generated by Yapps
  555. '''
  556. )