bison2py.py 1.9 KB

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