This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
acceptable = re.Scanner(patterns) | |
query = "SELECT * FROM table WHERE col_a = 1.5 AND col_b = 250;" | |
matched_tokens, uknown_tokens = acceptable.scan(query) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<query> ::= "SELECT " <columns> " FROM " <name> <terminal> | "SELECT " <columns> " FROM " <name> " WHERE " <conditionList> <terminal> | |
<columns> ::= (<name> ", ")+ | "*" | |
<name> ::= <letter>+ | <letter>+ "_" | <letter>+ "_" <digit>+ | |
<conditionList> ::= <condition> <comparator> <condition> | |
<comparator> ::= " AND " | " OR " | |
<condition> ::= <name> <operator> <term> | |
<operator> ::= " = " | " > " | " >= " | " < " | " <= " | |
<term> ::= <digit> | <digit> "." <digit> | <name> | |
<letter> ::= [a-z]+ | [A-Z]+ | |
<digit> ::= [1-9]+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParsingErrorException(Exception): | |
__module__ = 'builtins' | |
class LexingErrorException(Exception): | |
__module__ = 'builtins' | |
# Usage | |
raise LexingErrorException(f"Syntax error occured near: {}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node(object): | |
def __init__(self, val): | |
self.value = val # Value of node | |
self.children = [] # Can have multiple children | |
def add_child(self, node): | |
self.children.append(node) | |
return node | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show_tree(tree): | |
""" (Level order) Traverse and print parse tree | |
""" | |
queue = [] | |
queue.append(tree) | |
while(len(queue) != 0): | |
items = len(queue) | |
while(items > 0): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parser(self): | |
def advance(self): | |
""" Advance the tokens in use | |
""" | |
self.current_token, self.next_token = self.next_token, next(self.tokens, None) | |
def accept(self, expected, raise_err = True): | |
""" Helper function to check if the next token is what we expect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(): | |
""" <query> ::= "SELECT " <columns> " FROM " <name> <terminal> | "SELECT " <columns> " FROM " <name> " WHERE " <conditionList> <terminal> | |
""" | |
pass | |
def columns(): | |
""" <columns> ::= (<name> ", ")+ | "*" | |
Accepts: | |
- * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def term(): | |
if(accept("integer", False) or accept("float", False) or accept("name", False)): | |
return True | |
def terminal(): | |
if(accept("terminal")): | |
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def columns(): | |
if(accept("all_cols", False)): | |
return True | |
else: | |
if(accept("name")): | |
if(accept("punctuation", False)): | |
columns() # Recursively call self till next token is no longer a punctuation or name | |
else: | |
return True | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(): | |
if(accept("SELECT")): | |
if(columns()): | |
if(accept("FROM")): | |
if(name()): | |
if(terminal(False)): | |
show_tree(tree) | |
elif(accept("WHERE")): | |
if(condition_list()): | |
if(terminal()): |