Skip to content

Commit 73ce864

Browse files
committed
Use rule-based precedence instead of operator precedence
1 parent 8378774 commit 73ce864

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

regex-graphviz/grammar.y

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
%scanner-token-function scanner.lex()
22
%stype std::pair<std::list<Node>::iterator,std::list<Node>::iterator>
33
%token-path Tokens.h
4-
%token CHAR LPAREN RPAREN ERROR
5-
6-
%left BAR
7-
%left CONCAT
8-
%left PLUS STAR QUESTION
4+
%token CHAR LPAREN RPAREN ERROR BAR PLUS STAR QUESTION
95

106
%%
117

@@ -20,26 +16,10 @@ start : {
2016
}
2117
;
2218

23-
regex : regex STAR {
24-
$$ = $1;
25-
$$.second->addEdgeTo($$.first);
26-
(--$$.first)->addEdgeTo(nextNode);
27-
++$$.first;
28-
}
29-
| regex QUESTION {
30-
$$ = $1;
31-
(--$$.first)->addEdgeTo(nextNode);
32-
++$$.first;
33-
}
34-
| regex PLUS {
35-
$$ = $1;
36-
$$.second->addEdgeTo($$.first);
37-
}
38-
| regex regex
39-
%prec CONCAT {
40-
$$ = { $1.first, $2.second };
41-
}
42-
| regex BAR regex {
19+
regex : disjunct
20+
;
21+
22+
disjunct : disjunct BAR concatenate {
4323
$$ = { nodes.emplace($1.first, "", nodeId++), nextNode };
4424
for (auto& node : nodes) {
4525
for (auto edge : node.getEdges()) {
@@ -59,6 +39,30 @@ regex : regex STAR {
5939
nextNode = --nodes.end();
6040
$$.second->addEdgeTo(nextNode);
6141
}
42+
| concatenate
43+
;
44+
45+
concatenate : concatenate unary {
46+
$$ = { $1.first, $2.second };
47+
}
48+
| unary
49+
;
50+
51+
unary : unary STAR {
52+
$$ = $1;
53+
$$.second->addEdgeTo($$.first);
54+
(--$$.first)->addEdgeTo(nextNode);
55+
++$$.first;
56+
}
57+
| unary QUESTION {
58+
$$ = $1;
59+
(--$$.first)->addEdgeTo(nextNode);
60+
++$$.first;
61+
}
62+
| unary PLUS {
63+
$$ = $1;
64+
$$.second->addEdgeTo($$.first);
65+
}
6266
| LPAREN regex RPAREN {
6367
$$ = $2;
6468
}

0 commit comments

Comments
 (0)