No Description

SQLParser.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from SQLLexer import *
  2. import ply.yacc as yacc
  3. class SQLParser(object):
  4. def __init__(self):
  5. self.sqllex = SQLLexer()
  6. self.sqllex.build()
  7. #lexer = lex.lex(object=sqllex)
  8. self.tokens = self.sqllex.tokens
  9. self.parser = yacc.yacc(module=self)
  10. def p_select(self, p):
  11. 'select : SELECT columns FROM tables WHERE where_expression SEMI'
  12. p[0] = ('select-statement', p[2], p[4], p[6])
  13. def p_columns_wild(self, p):
  14. '''columns : WILD
  15. | id_list'''
  16. p[0] = ('columns-list', p[1])
  17. def p_id_list(self, p):
  18. '''id_list '''
  19. # Use the following expressions to construct the AST as guide
  20. if len(p) > 2:
  21. p[0] = p[1] + ',' + p[3]
  22. else:
  23. p[0] = p[1]
  24. def p_tables(self, p):
  25. 'tables : id_list'
  26. p[0] = ('table-list', p[1])
  27. def p_where_expression(self, p):
  28. '''where_expression : expression'''
  29. p[0] = ('where-expression', p[1])
  30. def p_expression(self, p):
  31. '''expression : simple_expression AND expression
  32. | simple_expression OR expression
  33. | simple_expression'''
  34. if len(p) < 3:
  35. p[0] = p[1]
  36. else:
  37. p[0] = ('logical-expression', p[2], p[1], p[3])
  38. def p_simple_expression(self, p):
  39. '''simple_expression : factor EQ factor
  40. | factor LT factor
  41. | factor'''
  42. if len(p) < 3:
  43. p[0] = p[1]
  44. else:
  45. p[0] = ('comparison-expression', p[2], p[1], p[3])
  46. def p_factor_id(self, p):
  47. '''factor : '''
  48. p[0] = ('ID', p[1])
  49. def p_factor_lit(self, p):
  50. '''factor : '''
  51. # Note that we do not add the actual literal value because
  52. # for the purpose of this module the input literals and numbers
  53. # can change.
  54. p[0] = ('LITERAL')
  55. def p_factor_num(self, p):
  56. '''factor : '''
  57. # Note that we do not add the actual number value because
  58. # for the purpose of this module the input literals and numbers
  59. # can change.
  60. p[0] = ('NUMBER')
  61. def p_factor_expression(self, p):
  62. '''factor : '''
  63. p[0] = ('group-expression', p[2])
  64. def p_error(self, p):
  65. print("Syntax error in input!")
  66. raise
  67. def parse(self, text):
  68. return self.parser.parse(text, lexer=self.sqllex)
  69. def print_ast(self, ast):
  70. Q = ast
  71. while len(Q) > 0:
  72. NQ = []
  73. for node in Q:
  74. if type(node) == tuple:
  75. print node[0],
  76. for i in range(1, len(node)):
  77. NQ.append(node[i])
  78. else:
  79. print node,
  80. Q = NQ
  81. print
  82. if __name__ == '__main__':
  83. parser = SQLParser()
  84. while True:
  85. try:
  86. s = raw_input("sql> ")
  87. except EOFError:
  88. break
  89. if not s: continue
  90. try:
  91. ast = parser.parse(s)
  92. parser.print_ast(ast)
  93. except:
  94. print "Syntax error"