Skip to content

Commit

Permalink
feat: support the priority between 'AND' and 'OR'
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Feb 6, 2022
1 parent a91402c commit 0fa9b9d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This project is inspired by sqlite, has a similar architecture with sqlite (but
There are two version of `tinydb`:

- `main.c` - In this program, I parse the SQL statements by spliting strings, thus the SQL statements are incomplete. And it does **NOT** support `where` keyword.
- `main2.c` - In this, I implement an tiny SQL Parser based on Flex & Bison, thus the SQL statements in this program are mostly like sqlite or MySQL. And it supports `where` conditions.
- `main2.c` - In this program, I implement an tiny SQL Parser based on Flex & Bison, thus the SQL statements in this program are mostly like sqlite or MySQL. And it supports `where` conditions.

For both of them, they have the same format of database file.

Expand Down Expand Up @@ -99,7 +99,9 @@ Please note that there is a `';'` after each SQL statement.
And the `conditions` support operators: `=, !=, >=, >, <=, <, AND, OR`, e.g.

```sql
SELECT * FROM table WHERE id < 10 OR (id > 100 AND username = 'sinkinben');
SELECT * FROM table WHERE id < 10 OR id > 100 AND username = 'sinkinben';
SELECT * FROM table WHERE id = 1 AND username='1' OR id >= 10 AND id < 20;
SELECT * FROM table WHERE id = 1 AND username='1' OR id >= 10 AND username < '20';
```

The keywords `AND, OR` can be `and, or`.
Expand Down
14 changes: 7 additions & 7 deletions docs/flex-and-bison.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Before you read this article, you should know the basis prerequisites knowledge:
Generally speaking, a compiler has 3 parts:

- Front-end: Lexer/Scanner, Parser, Semantic Analyzer
- Front-end will produce IR (Internal Result)
- Front-end will produce IR (Internal Result).
- Middle-end: Optimizer
- Back-end: Code Generator
- Back-end will generate the binary code related to specific machine
- Back-end will generate the binary code related to specific machines.

The tools "Flex & Bison" can help us to build the front-end of a compiler. In this article, we will introduce the basic usages of Flex and Bison via implementing a calculator.

Expand Down Expand Up @@ -162,7 +162,7 @@ From this example, we can know that `flex` can help us to generate the code of a

### Tokenizer

Let's see some advaned usages. Now, we will tokenizer a expression in C language.
Let's see some advaned usages, we will tokenize an expression in this section.

```c
%{
Expand Down Expand Up @@ -235,7 +235,7 @@ number = 1000, type = 258

## Bison

Suppose we have got all the `<token, type>` pairs, and we want to convert them into an AST, that what Bison can help us do.
Suppose we have got all the `<token, type>` pairs, and we want to convert them into an AST, that's what Bison can help us do.

```cpp
1 * 2 + 3 * 4 + 5
Expand All @@ -253,7 +253,7 @@ Suppose we have got all the `<token, type>` pairs, and we want to convert them i

### BNF

Backus-Naur Form (BNF), is called "BNF 范式" in some Chinese textbooks. Our BNF example here is very simple (and naivce):
Backus-Naur Form (BNF), is called "BNF 范式" in some Chinese textbooks. Our BNF example here is very simple (and naive):

```
<exp> ::= <factor>
Expand All @@ -268,7 +268,7 @@ In BNF, `::=` can be read as "is a", and `|` can be read as "or". For example,

### Let's Build a Calculator

> The source code of this part can be found in `sqlparser/calcdemo` of the github repo [tinydb](https://github.com/sinkinben/tinydb.git).
> The source code of this part can be found in `sql-parser/calcdemo` of the github repo [tinydb](https://github.com/sinkinben/tinydb.git).
In this part, we will show the basic usages of Bison via building a calculator (support `+, -, *, /` ) example.

Expand Down Expand Up @@ -333,7 +333,7 @@ term: NUMBER { $$ = $1; }
%%
```
And we make a simple modification on the flex source file above:
And we make some simple modifications on the flex source file above:
```c
// file: calc.l
Expand Down
25 changes: 17 additions & 8 deletions sql-parser/sqlparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ void yyerror(const char *msg) { fprintf(stderr, "[tinydb] SQL Parser: %s\n", msg
%type <update_node> updateitem
%type <update_node> updateitemlist

%type <intval> cmp_op logic_op
%type <intval> cmp_op
%type <condition_tree> conditionitem
%type <condition_tree> conditionfactor
%type <condition_tree> conditions


Expand Down Expand Up @@ -189,17 +190,14 @@ selectitemlist:
}
;

logic_op:
AND { $$ = OP_AND; }
| OR { $$ = OP_OR; }

cmp_op:
LESS { $$ = OP_LESS; }
| LESS_EQUAL { $$ = OP_LESS_EQUAL; }
| GREAT { $$ = OP_GREAT; }
| GREAT_EQUAL { $$ = OP_GREAT_EQUAL; }
| EQUAL { $$ = OP_EQUAL; }
| NOT_EQUAL { $$ = OP_NOT_EQUAL; }
;

conditionitem:
columnitem cmp_op NUMBER
Expand All @@ -212,18 +210,29 @@ conditionitem:
}
;

conditions:
conditionfactor:
conditionitem
{
$$ = $1;
}
| conditionfactor AND conditionitem
{
$$ = alloc_condition((uint64_t)($1), OP_AND, (uint64_t)($3), false, COLUMN_DUMMY);
}
;

conditions:
conditionfactor
{
$$ = $1;
}
| '(' conditions ')'
{
$$ = $2;
}
| conditions logic_op conditions
| conditions OR conditionfactor
{
$$ = alloc_condition((uint64_t)($1), $2, (uint64_t)($3), false, COLUMN_DUMMY);
$$ = alloc_condition((uint64_t)($1), OP_OR, (uint64_t)($3), false, COLUMN_DUMMY);
}
;

Expand Down

0 comments on commit 0fa9b9d

Please sign in to comment.