I'm learning about reducing DFA's using the Pair Table Method (Systematic Reduction Method).Here is the DFA we are looking to reduce.
The first step is to lay out the DFA in a table:
0 1
q0 {q0, q3} {q1}
q1 {q2} {q2}
q2 EmptySet {q2}
{q0, q1} {q0, q1, q3} {q1, q2}
{q1, q2} {q2} {q2}
{q0, q1, q2} {q0, q1, q2} {q1, q2}
We don't need to include the empty set state, I think.
Now here is where I'm confused, I need to go through the list of states and mark them based on something. I'm not sure how to proceed.
First of all, the table you have is not a match with the DFA.
The pair table method uses the classes of equivalency. First we partition the states in two: final and non-final states. Initially every final state is distinguishable from any non-final state. So you should create a table like below and mark (q0,q1) and (q1,q2) as q1 is final but q0 and q2 are non-final states.
+----+
| q0 |
+----+----+
| q1 | x |
+----+----+----+
| q2 | | x |
+----+----+----+----+
| q0 | q1 | q2 |
+----+----+----+
Then iteratively, you continue marking using the following rule and stop when in an iteration nothing is changed:
Mark (q_i,q_j) if for some alphabet symbol a, is marked. Int above table, and . As (q1,q2) is marked, we should mark (q0,q2) as well. Note that we only have half of the table. Hence, (q0,q2)=(q2,q0).
+----+
| q0 |
+----+----+
| q1 | x |
+----+----+----+
| q2 | x | x |
+----+----+----+----+
| q0 | q1 | q2 |
+----+----+----+
Related
I have this NFA in the book given:
And their solved DFA result was this:
But According to my solved solution (by finding e-closure of each state), it looks something like this:
| a | b | c
----+----- +-----+---
ABD | CEBD | Φ | C
BD | EBD | Φ | C
C | Φ | EBD | Φ
D | EBD | Φ | Φ
EBD | EBD | Φ | C
CEBD| EBD | EBD | C
What am I missing?
Your solution is identical to the DFA diagram, with two differences:
Your table contains unreachable states (BD, D). Those are culled from the diagram.
There is a misprint in the diagram. The two arrows between {C} and {BDE} have their labels switched. (The arrow going from {C} to {BDE} should be labeled b, and the arrow from {BDE} to {C} should be labeled c.)
I want to design a DFA for the following language after fixing ambiguity.
I thought and tried a lot but couldn't get a proper answer.
S->aA|aB|lambda
A->aA|aS
B->bB|aB|b
I recommend first getting an NFA by considering this to be a regular grammar; then, determinize the NFA, and then we can write down a new grammar that's equivalent to this one but unambiguous (for the same reason the determinized automaton is deterministic). Writing down the NFA for this grammar is easy: productions of the form X -> sY translate into transitions from state X to state Y on input s. Similarly, transitions of the form X -> lambda mean X is an accepting state, and transitions of the form X -> b imply a new accepting state that transitions to a dead state.
We need states for each nonterminal symbol S, A and B; and we will have transitions for every production. Our NFA looks like this:
/---a----\
| |
V |
----->(S)--a-->(A)<--\
| | |
a \--a-/ /--a,b--\
| | |
V V |
/--->(B)--b-->(X)-a,b->(Y)<-----/
| |
\-a,b-/
Here, states (S) and (X) are accepting, state (Y) is a dead state (we didn't really need to depict this explicitly, but bear with me) and this automaton is totally equivalent to the grammar. Now, we need to determinize this. States of the determinized automaton will correspond to subsets of states from the nondeterministic version. Our first deterministic state will correspond to the set containing just (S), and we will figure out the other required subsets (of which we can have at most 32, since we have 5 states and 2 to the power of 5 is 32) using the transitions:
Q s Q'
{(S)} a {(A),(B)}
{(S)} b empty
{(A),(B)} a {(A),(B),(S)}
{(A),(B)} b {(B),(X)}
{(A),(B),(S)} a {(A),(B),(S)}
{(A),(B),(S)} b {(B),(X)}
{(B),(X)} a {(B),(Y)}
{(B),(X)} b {(B),(X),(Y)}
{(B),(Y)} a {(B),(Y)}
{(B),(Y)} b {(B),(X),(Y)}
{(B),(X),(Y)} a {(B),(Y)}
{(B),(X),(Y)} b {(B),(X),(Y)}
We encountered six states, plus a dead state (empty) which we can name q1 through q6, plus qD. All of the states corresponding to subsets with either (S) or (X) in them are accepting, and (S) is the initial state. Our DFA looks like this:
/-a,b-\
| |
V |
----->(q1)--b-->(qD)----/
|
a /--a--\
| | |
V V |
(q2)--a-->(q3)----/
| |
b |
| b
V |
/--(q4)<------/ /--b--\
| | | |
| \------b------(q6)<---+
a /--a----\ | |
| | | | |
\-->(q5)<-----+--a-/ |
| |
\---------b---------/
Finally, we can read off the unambiguous regular grammar from our DFA:
(q1) -> a(q2) | b(qD) | lambda
(qD) -> a(qD) | b(qD)
(q2) -> a(q3) | b(q4)
(q3) -> a(q3) | b(q4) | lambda
(q4) -> a(q5) | b(q6) | lambda
(q5) -> a(q5) | b(q6)
(q6) -> a(q5) | b(q6) | lambda
Consider the following grammar:
S → A | B
A → xy
B → xyz
This is what I think an LR(0) parser would do given the input xyz:
| xyz → shift
x | yz → shift
xy | z → reduce
A | z → shift
Az | → fail
If my assumption is correct and we changed rule B to read:
B → Az
now the grammar suddenly becomes acceptable by an LR(0) parser. I presume this new grammar describes the exact same set of strings than the first grammar in this question.
What are the differences between the first and second grammars?
How do we decouple structural differences in our grammars from the language they describe?
Through normalization?
What kind of normalization?
To further clarify:
I want to describe a language to a parser, without the structure of the grammar playing a role. I'd like to obtain the most minimal/fundamental description of a set of strings. For LR(k) grammars, I'd like to minimize the k.
I think your LR(0) parser is not a standard parser:
Source
An LR(0) parser is a shift/reduce parser that uses zero tokens of lookahead to determine what action to take (hence the 0). This means that in any configuration of the parser, the parser must have an unambiguous action to choose - either it shifts a specific symbol or applies a specific reduction. If there are ever two or more choices to make, the parser fails and we say that the grammar is not LR(0).
So, When you have:
S->A|B
A->xy
B->Az
or
S->A|B
A->xy
B->xyz
LR(0) will never check B rule, And for both of them it will fail.
State0 - Clousure(S->°A):
S->°A
A->°xy
Arcs:
0 --> x --> 2
0 --> A --> 1
-------------------------
State1 - Goto(State0,A):
S->A°
Arcs:
1 --> $ --> Accept
-------------------------
State2 - Goto(State0,x):
A->x°y
Arcs:
2 --> y --> 3
-------------------------
State3 - Goto(State2,y):
A->xy°
Arcs:
-------------------------
But if you have
I->S
S->A|B
A->xy
B->xyz or B->Az
Both of them will accept the xyz, but in difference states:
State0 - Clousure(I->°S):
I->°S
S->°A
S->°B
A->°xy A->°xy, $z
B->°xyz B->°Az, $
Arcs:
0 --> x --> 4
0 --> S --> 1
0 --> A --> 2
0 --> B --> 3
-------------------------
State1 - Goto(State0,S):
I->S°
Arcs:
1 --> $ --> Accept
-------------------------
State2 - Goto(State0,A):
S->A° S->A°, $
B->A°z, $
Arcs: 2 --> z --> 5
-------------------------
State3 - Goto(State0,B):
S->B°
Arcs:
-------------------------
State4 - Goto(State0,x):
A->x°y A->x°y, $z
B->x°yz
Arcs:
4 --> y --> 5 4 --> y --> 6
-------------------------
State5 - Goto(State4,y): - Goto(State2,z):
A->xy° B->Az°, $
Arcs:
5 --> z --> 6 -<None>-
-------------------------
State6 - Goto(State5,z): - Goto(State4,y)
B->xyz° A->xy°, $z
Arcs:
-------------------------
You can see the Goto Table and Action Table is different.
[B->xyz] [B->Az]
| Stack | Input | Action | Stack | Input | Action
--+---------+--------+---------- --+---------+--------+----------
1 | 0 | xyz$ | Shift 1 | 0 | xyz$ | Shift
2 | 0 4 | yz$ | Shift 2 | 0 4 | xy$ | Shift
3 | 0 4 5 | z$ | Shift 3 | 0 4 6 | z$ | Reduce A->xy
4 | 0 4 5 6 | $ | Reduce B->xyz 4 | 0 2 | z$ | Shift
5 | 0 3 | $ | Reduce S->B 5 | 0 2 5 | $ | Reduce B->Az
6 | 0 1 | $ | Accept 6 | 0 3 | $ | Reduce S->B
7 | 0 1 | $ | Accept
Simply when you change B->xyz to B->Az you add an Action to your LR Table to find the differences you can check Action Table and Goto Table (Constructing LR(0) parsing tables)
When you have A->xy and B->xyz then you have two bottom handles [xy or xyz] but when you have B->Az you have only one bottom handle [xy] that can accept an additional z.
I think related to local optimization -c=a+b; d=a+b -> c=a+b; d=c- when you use B->Az you make B->xyz optimized.
Say I have a table:
A, 1
B, 1
C, 2
D, 1
E, 2
How do I view the table grouping by the 2nd column and aggregating by the first with a comma separated concat function ie:
1, "A,B,D"
2, "C,E"
In both defining a pivot table and using the QUERY syntax, it seems that the only aggregation functions available are numerical aggregations like MIN, MAX, SUM, etc. Can I define my own aggregation function?
You have to add a "Calculated Field" to the pivot table, and then select "Summarise by > Custom". This will make the column names in your formula refer to an array of values (instead of a single value). Then you can type a formula like:
= JOIN(", ", MyStringColumn)
More specifically, if you have the following table:
Create a pivot table by going to "Data > Pivot table", with the following configuration. Ensure "Summarize by" is set to "Custom"!
Another option: if the data is in A2:B, then, say, in D2:
=UNIQUE(B2:B)
and then in E2:
=JOIN(",",FILTER(A$2:A,B$2:B=D2))
which is filled down as required.
There are one-formula, auto-expanding solutions, although they get quite convoluted.
You're right, there's no easy way with pivot tables. This though, will do the trick. Inspired by this brilliant answer here.
First, have a header row and run a sort on column A to group by category.
So far, in your example, we have
| A | B
---+-----------+-----------
1 | CATEGORY | ATTRIBUTE
2 | 1 | A
3 | 1 | B
4 | 1 | D
5 | 2 | C
6 | 2 | E
In column C, let's prep the concatenated strings. Start in cell C2 with the following formula, and fill out vertically.
=IF(A2<>A1, B2, C1 & "," & B2)
...looking good...
| A | B | C
---+-----------+-----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS
2 | 1 | A | A
3 | 1 | B | A,B
4 | 1 | D | A,B,D
5 | 2 | C | C
6 | 2 | E | C,E
In column D, let's validate the rows we want to select in a later step, with the following formula, starting in cell D2 and filling out. Basically we are marking the final category rows that carry the full concatenated strings.
=A2<>A3
...almost there now
| A | B | C | D
---+-----------+-----------+----------+-----------
1 | CATEGORY | ATTRIBUTE | STRINGS | VALIDATOR
2 | 1 | A | A | FALSE
3 | 1 | B | A,B | FALSE
4 | 1 | D | A,B,D | TRUE
5 | 2 | C | C | FALSE
6 | 2 | E | C,E | TRUE
Now, lets copy column C and D and paste special as values in the same place. Then add a filter on the whole table and filter out column D for the rows labeled TRUE. Now, remove the filter, delete columns B and D and row 1.
| A | B
---+-----------+-----------
1 | 1 | A,B,D
2 | 2 | C,E
Done. Get ice cream. Watch Road House.
I am designing a state-machine and have one specific state that I can enter from two different states... I am not sure how to go back to the previous state... or am I modeling it wrong ?
to illustrate :
| state | trigger | nextstate
---------------------------------
1. | initial | evtX | A
2. | initial | evtY | B
3. | B | evtX | A
4. | A | evtZ | ????
The last row is where I am having trouble. I need to transition to initial state, if A was arrived at from the transiton in row number 1 and I need to transition to state B, if A was arrived at from transition in row number 3.
How can i model this better ?
In fact, you have two different A states:
| state | trigger | nextstate
---------------------------------
1. | initial | evtX | A1
2. | initial | evtY | B
3. | B | evtX | A2
4. | A1 | evtZ | initial
4. | A2 | evtZ | B
If you want something more powerful, try with Harel/UML statecharts (which have 'superstates, orthogonal regions, and activities as part of a state" [1]). You might have a look at SCXML as weel [2]. I don't know any of them though.
[1] http://en.wikipedia.org/wiki/Harel_statechart#Harel_statechart
[2] http://en.wikipedia.org/wiki/SCXML