bison2py 1.6 KB

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