bison2py.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. """
  3. Utility which creates a boilerplate pybison-compatible
  4. python file from a yacc file and lex file
  5. Run it with 2 arguments - filename.y and filename.l
  6. Output is filename.py
  7. """
  8. import sys
  9. from bison import bisonToPython
  10. def usage(s=None):
  11. """
  12. Display usage info and exit
  13. """
  14. progname = sys.argv[0]
  15. if s:
  16. print progname+": "+s
  17. print "\n".join([
  18. "Usage: %s [-c] basefilename" % progname,
  19. " or: %s [-c] grammarfile.y lexfile.l pyfile.py" % progname,
  20. "(generates a boilerplate python file from a grammar and lex file)",
  21. "The first form uses 'basefilename' as base for all files, so:",
  22. " %s fred" % progname,
  23. "is equivalent to:",
  24. " %s fred.y fred.l fred.py" % progname,
  25. '',
  26. 'The "-c" argument causes the creation of a unique node class',
  27. 'for each parse target - highly recommended for complex grammars',
  28. ])
  29. sys.exit(1)
  30. def main():
  31. """
  32. Command-line interface for bison2py
  33. """
  34. argv = sys.argv
  35. argc = len(argv)
  36. if '-c' in argv:
  37. generateClasses = 1
  38. argv.remove('-c')
  39. argc = argc - 1
  40. else:
  41. generateClasses = 0
  42. if argc == 2:
  43. basename = argv[1]
  44. bisonfile = basename+".y"
  45. lexfile = basename+".l"
  46. pyfile = basename+".py"
  47. elif argc == 4:
  48. bisonfile, lexfile, pyfile = argv[1:4]
  49. else:
  50. usage("Bad argument count")
  51. bisonToPython(bisonfile, lexfile, pyfile, generateClasses)
  52. if __name__ == '__main__':
  53. main()