Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Roseseed
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wanxin Li
Roseseed
Commits
cae50ade
Commit
cae50ade
authored
6 years ago
by
Lu Wang
Browse files
Options
Downloads
Patches
Plain Diff
Added translate function to get a list-style parse tree for pattern matching
parent
f7924ced
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
SQLPGrammar.y
+4
-1
4 additions, 1 deletion
SQLPGrammar.y
util.c
+49
-1
49 additions, 1 deletion
util.c
util.h
+9
-1
9 additions, 1 deletion
util.h
with
62 additions
and
3 deletions
SQLPGrammar.y
+
4
−
1
View file @
cae50ade
...
...
@@ -32,6 +32,9 @@ SQLPProgram
n->children[0] = $1;
printf("Printing Tree\n");
print_tree(n, 0);
struct linked_node *linked_tree = malloc(sizeof(struct linked_node));
translate(linked_tree, n);
print_linked_tree(linked_tree, 0);
}
;
...
...
@@ -39,7 +42,7 @@ Identifier
: IDENTIFIER
{ printf("|%s| ", yytext);
$$ = new_node(1, Identifier);
$$->children[0] =
$1
;
$$->children[0] =
new_node(0, IDENTIFIER)
;
}
;
...
...
This diff is collapsed.
Click to expand it.
util.c
+
49
−
1
View file @
cae50ade
...
...
@@ -29,4 +29,52 @@ void destroy_tree(struct node *root) {
destroy_tree
(
root
->
children
[
i
]);
}
free
(
root
);
}
\ No newline at end of file
}
// translate the parse tree to a linked structure
// next points to its sibling
// children points to its first child
void
translate
(
struct
linked_node
*
linked_tree
,
struct
node
*
tree
)
{
if
(
tree
==
NULL
)
return
;
linked_tree
->
type
=
tree
->
type
;
int
count
=
tree
->
count
;
if
(
count
>
0
)
{
struct
linked_node
*
first_child
=
malloc
(
sizeof
(
struct
linked_node
));
linked_tree
->
children
=
first_child
;
}
else
{
linked_tree
->
children
=
NULL
;
}
struct
linked_node
*
current_node
=
linked_tree
->
children
;
for
(
int
i
=
1
;
i
<
count
;
i
++
)
{
current_node
->
next
=
malloc
(
sizeof
(
struct
linked_node
));
current_node
=
current_node
->
next
;
}
if
(
current_node
)
current_node
->
next
=
NULL
;
current_node
=
linked_tree
->
children
;
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
translate
(
current_node
,
tree
->
children
[
i
]);
current_node
=
current_node
->
next
;
}
}
void
print_linked_tree
(
struct
linked_node
*
root
,
int
level
)
{
if
(
root
==
NULL
)
{
return
;
}
if
(
root
->
children
!=
NULL
){
printf
(
"level: %d, type: %s
\n
"
,
indent
,
node_types
[
root
->
type
]);
}
print_linked_tree
(
root
->
next
,
level
);
print_linked_tree
(
root
->
children
,
level
+
1
);
}
// translated from PatMatch.cl
// bool match(struct node *pat, struct node *tree) {
// if (pat->next != NULL) {
// return (tree->next != NULL && match(pat->children, tree->children) &&
// match(pat->next, children->next);
// }
// }
This diff is collapsed.
Click to expand it.
util.h
+
9
−
1
View file @
cae50ade
...
...
@@ -11,6 +11,12 @@ struct node {
};
struct
linked_node
{
int
type
;
struct
linked_node
*
next
;
// siblings
struct
linked_node
*
children
;
};
enum
type
{
SQLPProgram
,
...
...
@@ -28,10 +34,12 @@ enum type {
};
struct
node
*
new_node
(
int
count
,
int
type
);
void
translate
(
struct
linked_node
*
linked_tree
,
struct
node
*
tree
);
void
print_tree
(
struct
node
*
root
,
int
indent
);
void
print_linked_tree
(
struct
linked_node
*
root
,
int
indent
);
void
destroy_tree
(
struct
node
*
root
);
bool
match
(
struct
linked_node
*
pat
,
struct
linked_node
*
tree
);
#endif
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment