bison2py 1.9 KB

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