Here is the productions.
A-> Aa| b|c;
Now shall i do
A->bA'
A'-> aA' | e (empty transition)
A->c
Will it be the right answer? That is either 'b' or 'c' can be used?
I would say, there has to be 'b' or 'c' at the beginning, so more like this:
A -> bA' | cA'
A'-> aA' | e
Related
I am asked to write the grammar which generate the following language over the alphabet Z={a,b}
M={w | numbers of b's in w is 3 modulo 4}
My Answer is
S->bP| bJ | aS
P->bQ | bK | aP
Q->bR | bL | aQ
R->bS | e | aS
L->e
will this work?
Can we make it better?
Not sure what J, K and L are. But yes, you can probably do better; a DFA with 4 states can recognize your language, so there's definitely a regular grammar with four nonterminals:
S -> aS | bR
R -> aR | bT
T -> aT | bU | b
U -> aU | bS | a
This works because states S, R, T and U correspond to having seen 0, 1, 2 and 3 instances of b, modulo 4. Seeing instances of a leaves you in whatever state you were in before, while seeing b takes you to the next state. Because state u is accepting we add U -> e, and then remove the empty production in the usual way.
I'm trying to wrap my head around parser theory, and I keep finding the same example in different sources. The grammar goes approximately like this (simplified):
E = T
E = E + T
T = 0..9
So supposedly a string 2 + 2 will be parsed as such ("|" separates the stack from the reminder)
|2 + 2 <-can't reduce, shift
2|+ 2 <-reduce by T = 0..9
T|+ 2 <-reduce by E = T
E|+ 2 <-can't reduce, shift
E +|2 <-can't reduce, shift
E + 2| <-reduce by T = 0..9
E + T| <-reduction by E = E + T here?
E| <-done
The question is, at E + T step parser can apply two different reductions to the rightmost part of the stack: E = T (resulting in E + E) and E = E + T (resulting in E). And I can't find a clear and conscise explanation how it chooses one over the other.
What am I missing?
What are the possible states?
0: Beginning
1: Just shifted 0..9 after State 0, recognize a T
2: Reduce State 1 to an E.
3: Just shifted + after State 2 or 5, looking for T
4: Just shifted 0..9 after State 3, recognize a T giving us E + T.
5: Reduce state 4 to an E
6: Reach the end of the stack after state 2 or 5.
So we start in state 0. Shift a 2. We are now in state 1. Transition to state 2. Shift a +. We are now in state 3. We shift a 2. We are in state 4. We reduce to state 5. We reach the end of the stack and wind up with an expression tree looking like the following:
E
|
E + T
| |
T 2
|
2
According to the grammar, an E can never follow a +. This rules out the E = T production at this state.
To fully understand that, construct the parser tables by hand - the example is small enough to make this feasible.
I want to select distinct/group by column and show for the second column only one value(out of 3 possible) priority by: if A appears in the group show it, if not show B, if not show C.
This is the table:
A B
---- -----
FST A
FST B
FST C
INCS C
INCS B
ASW A
AWR C
WER C
WER C
WER B
RESULT
A B
---- -----
FST A
INCS B
ASW A
AWR C
WER B
For your given data, the easiest way is:
select A, min(B)
from table t
group by A;
For the more general problem where alphabetic ordering is not correct (if you wanted to return 'B' ahead of 'A', say), you can do this with case statements:
select A,
(case min(case B when 'A' then 1 when 'B' then 2 when 'C' then 3 else 4 end)
when 1 then 'A'
when 2 then 'B'
when 3 then 'C'
else '???'
end)
from table t
group by A;
EDIT:
Oracle has an easier way to do this than the nested selects:
select A,
max(B) keep (dense_rank first
order by (case B when 'A' then 1 when 'B' then 2 when 'C' then 3 else 4 end)
) as BestB
from table t
group by A;
Consider the following directed graph:
I'd like to be able to pick two nodes in this graph, and then find all of the possible paths between those two nodes. (Well, not all of the paths, just those paths not containing cycles.)
First, I model the graph in a view (as a transitive closure)
CREATE OR REPLACE VIEW closure AS
WITH
edges AS (
-- for demonstration purposes, I just need to model the edges
SELECT 'A' AS start_node, 'C' AS end_node FROM dual UNION ALL
SELECT 'A', 'D' FROM dual UNION ALL
SELECT 'B', 'C' FROM dual UNION ALL
SELECT 'B', 'D' FROM dual UNION ALL
SELECT 'C', 'E' FROM dual UNION ALL
SELECT 'D', 'F' FROM dual UNION ALL
SELECT 'E', 'A' FROM dual UNION ALL
SELECT 'E', 'F' FROM dual UNION ALL
SELECT 'E', 'G' FROM dual UNION ALL
SELECT 'F', 'B' FROM dual UNION ALL
SELECT 'F', 'G' FROM dual
)
SELECT
CONNECT_BY_ROOT start_node AS start_node,
end_node AS end_node,
CONNECT_BY_ROOT start_node||sys_connect_by_path(end_node, ' -> ') AS path,
level + 1 AS path_length
FROM
edges
CONNECT BY
NOCYCLE PRIOR end_node = start_node
-- this bit just adds the trivial paths to the closure
UNION ALL
SELECT DISTINCT start_node, start_node, start_node, 1 FROM edges
;
view CLOSURE created.
Now, suppose that I wanted to see all of the paths that start at 'E' and end at 'G'.
SELECT path, path_length
FROM closure
WHERE start_node='E'
AND end_node = 'G'
ORDER BY path_length ASC
;
PATH PATH_LENGTH
------------------------------ -----------
E -> G 2
E -> F -> G 3
E -> A -> C -> E -> G 5
E -> A -> D -> F -> G 5
E -> F -> B -> C -> E -> G 6
E -> A -> C -> E -> F -> G 6
E -> A -> D -> F -> B -> C -> E -> G 8
7 rows selected
I am surprised the the third, fifth, sixth, and seventh rows are returned. These four rows all contain a cycle (specifically, from 'E' to 'E').
Looking at the data in my view, it appears that the only cycles that are included are those that include the root node. (e.g., the cycle 'E' -> 'A' -> 'C' -> 'E') is only included in paths that begin with node 'E'... it is never included in paths that begin with any other node.)
I have tried to prevent these cycles from happening by adding a CONNECT_BY_ROOT start_node <> start_node in my "CONNECT BY" clause, but this is not allowed by Oracle.
How can I prevent these cycles from occurring in my data?
==== BEGIN EDIT ====
As #Egor Skriptunoff pointed out below, I am working with a table of edges, so maybe I should expect to see some vertices repeated, as long as they are not visited over the same edge. However, if this were the case, I should see the path 'E -> F -> B -> D -> F -> G' in the results above. This is a path that cycles through node 'F', once via edge 'E -> F' and once via 'D -> F'. However, this path is not there.
I presume this is because the NOCYCLE is preventing node 'F' from occurring twice. Is this really the case?
Oracle version: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
==== END EDIT ====
Your select clause is flawed - you have to apply CONNECT_BY_ROOT to the head of the edge instead of its tail
-- ...
CONNECT_BY_ROOT end_node AS start_node,
end_node AS end_node,
sys_connect_by_path(end_node, ' -> ') AS path,
level AS path_length
-- ...
You are working with a table of edges, so it is repeated edges that are excluded, vertices may repeat.
[BASE]
/ \ \
C1 C2 C3
/\ \
C4 C5 C6
I have a tree like the above. This is a N child tree which is not balanced. The problem is, I need to select one of the node based on some condition. Like
Select C1 when k1 = a
Select C4 when K1 = a and K2=b and K3=C
Select C5 when k1 = a and k'=z
Select C2 when K'' = b
Select C3 when k5 = 9
Select C6 when k5=9 and k6 = 10
The input to the program would be an arbitraty length of key value pairs like if input is -k1=a,k2=b,k3=c,k8=10 - I should select C4 as that is the best match.
Ideally I was thinking of traversing the tree and for each node, there is a selection criteria which I can match against the input set. But soon I figured out, this tree can be very huge and Base node can have tens of thousands of child nodes under it. So it might not be a good idea to go node by node. If there is a way to select the nodes more efficiently, I would love to know that.
Looks like your k's are pointing to directory structure and the leaf of this structure (exactly one leaf for each directory) is the node you are looking for. You can keep this string in node as another value. What is not clear in question is how are the k's related to the tree
for e.g.
a->c1
a/b/c->c4
I have found a workable solution like this one
----------------------------------------
|rowId|param1|param2|param3|param4|node|
----------------------------------------
|10 | a | | | | C1 |
----------------------------------------
|14 | a | b | c | | C4 |
----------------------------------------
|18 | a | b | | | C5 |
----------------------------------------
Lets call it a condition table. Each column represent the input series (k) and for different combinations of the value, there is a node to be selected. This table can be think of an in memory data structure or a real table in RDBMS.