How to draw a DFA for that given language? - dfa

This the question : L = { vwv : w,v ∈ {a,b}* , |v| = 2 }
I draw it until W cames up idk what to do !
this is my way :

The language defined as L = { vwv : w,v ∈ {a,b}* , |v| = 2 is associated with the regular expression:
{a,b}{a,b}{a,b}*{a,b}{a,b}
The associated DFA is:
a a a a
-->-- -->-- -->-- -->--
start s1 s2 s4 final
-->-- -->-- -->-- -->--
b b | | b b
| |
a v ^ b
| |
| |
s3

Related

Creating grammars for lexical analyzer

I am trying to create a lexical analyzer for a given language. Can't write grammar rules correctly. My task is
The input language contains conditional statements if ... then ... else and if ... then, separated by a symbol ; (semicolon). Condition statements contain identifiers, comparison signs <,>, =, hexadecimal numbers, sign assignments (:=). Consider the sequence as hexadecimal numbers digits and symbols a, b, c, d, e, f starting with a digit (for example, 89, 45ac, 0abc )
I got these rules:
S -> if E then S else S; S | if E then S; S | if E then S else S | if E then S | I := E
E -> H | E > H | E < H | E = H
I -> LLL
H -> DHL | DL | DH |D
L -> a | b | c | d | e | f
D -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Is the rule (H -> DHL | DL | D | DH) for determining hexadecimal numbers set correctly? Are the rest of the rules correct?

Print tic tac toe board fortran90

I am just trying to create a subroutine to print out the current state of the board in tic tac toe. Zeros are blank spaces, 1's are X's, and 2's are O's. I'm not sure what's wrong with my code, but ti would print out the current state, instead it prints out a bunch of different, incorrect boards. Any help at all would be greatly appreciated.
!!!!!!!!!This is my code and subroutine:
program ttt
implicit none
integer, dimension(9) :: board
integer :: playerx
character (len=1), dimension(9) :: cboard
integer :: i, j
!Print board with numbered spots
!print *, "Enter a number 1-9 to play tic-tac-toe"
print *, " "
print "(a11)", " 1 | 2 | 3 "
print "(a11)", "---+---+---"
print "(a11)", " 4 | 5 | 6 "
print "(a11)", "---+---+---"
print "(a11)", " 7 | 8 | 9 "
print *, " "
board = (/ 2, 0, 0, &
0, 1, 0, &
0, 0, 1 /)
playerx = 1
call printboard(board, playerx)
end program ttt
! Subroutine to print out the current state of the board
subroutine printboard(board, playerx)
implicit none
integer, intent(in), dimension(9) :: board
integer, intent(in) :: playerx
character (len=1), dimension(9) :: cboard
integer :: i, j
! board array is series of 1s, 2s, and 0s. set 1 = x, 2 = o, and 0 = " "
if (playerx == 1) then
do i = 1,9
do j = 1, 9
if (board(i) == 0) cboard(j) = " "
if (board(i) == 1) cboard(j) = "x"
if (board(i) == 2) cboard(j) = "o"
if (j < 0 .and. j < 4) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", "---+---+---"
endif
if (j > 3 .and. j < 7) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", "---+---+---"
endif
if (j > 6 .and. j < 10) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", " "
endif
endif
end subroutine printboard
This is what the code produces:
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
o | o | o
---+---+---
o | o | o
---+---+---
o | o | o
---+---+---
o | o | o
o | o | o
o | o | o
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
x | x | x
x | x | x
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
x | x | x
x | x | x
Your code seems to be a little bit complicated. I would have it simplified here and there.
First of all, you can use 2D array to store your data. I think it would be way more natural.
Instead of filling character(1), dimension(3,3) :: cboard based on values from integer, dimension(3,3) :: board you can simply introduce function that will provide required character depending on the value stored inside your board.
When it comes to printing, I think that putting multiple print sections, that behave differently (depending on whether there will be a new line or not), is an overkill.
It is also a good idea to put parts of the code (that are responsible for a certain logic of your application) into modules. This way, you can reuse and replace them. E.g. you can easily add new module that will print your board differently.
When it comes to Fortran, it's a good idea to remember column/row order in arrays. Might be confusing for people coming with the experience based on different languages.
module board_printer
implicit none
contains
function integer2char(x)
integer, intent(in) :: x
character(1) :: integer2char
if(x == 0) then integer2char = ' '
else if(x == 1) then integer2char = 'x'
else integer2char = 'o'
end if
end function integer2char
subroutine printboard(board)
integer, intent(in), dimension(3, 3) :: board
integer :: i
do i = 1, 3
write(*, fmt="(a)") ' '//integer2char(board(i,1))//' | '//integer2char(board(i,2))//' | '//integer2char(board(i,3))//' '
if( i /= 3) then
write(*, fmt="(a)") '---+---+---'
end if
end do
write(*,*) ""
end subroutine printboard
end module board_printer
program ttt
use board_printer
implicit none
integer, dimension(3, 3) :: board
write(*,fmt="(a)") "Enter a number 1-9 to play tic-tac-toe"
write(*,fmt="(a)") " "
write(*,fmt="(a)") ' 1 | 4 | 7 '
write(*,fmt="(a)") '---+---+---'
write(*,fmt="(a)") ' 2 | 5 | 8 '
write(*,fmt="(a)") '---+---+---'
write(*,fmt="(a)") ' 3 | 6 | 9 '
write(*,fmt="(a)") ''
board = reshape((/ 0, 0, 0, 0, 0, 0, 0, 0, 0 /), shape(board) )
call printboard(board)
board = reshape((/ 1, 0, 0, 0, 1, 0, 0, 0, 1 /), shape(board) )
call printboard(board)
board = reshape((/ 0, 0, 1, 0, 1, 0, 1, 0, 0 /), shape(board) )
call printboard(board)
board = reshape((/ 2, 0, 0, 0, 2, 0, 0, 0, 2 /), shape(board) )
call printboard(board)
board = reshape((/ 1, 0, 2, 0, 1, 0, 2, 0, 1 /), shape(board) )
call printboard(board)
end program ttt

Storing user-defined logic in a database

I'm designing a database to store information about events that are dynamic in nature. What I mean by this is that, each type of event will have some variables attached to them that changes on each occurrence based on some rules defined by the user.
Let's say we have Event Type A with variable X and Y. In this event type, the user can define some rules that determines the value of X and Y on each occurrence of the event.
An example of a set of rules a user might define:
On first occurrence, X = 0; Y = 0;
On each occurrence, X = X + 1;
On each occurrence, if X == 100 then { X = 0; Y = Y + 1 }
By defining these rules, the value of X and Y changes dynamically on all occurrences of the event as follow:
1st occurrence: X = 1, Y = 0
2nd occurrence: X = 2, Y = 0
...
100th occurrence: X = 0, Y = 1
Now, I'm not sure how to store the "user-defined rules" in a database and later query them in my code. Can anyone point me in the right direction? Here's a start:
EVENTS
id;
name;
description;
event_type;
EVENT_TYPE_A_OCCURRENCES
id;
event_id;
X;
Y;
EVENT_RULES
id;
event_id;
frequency; // the frequency in which this rule applies
at_occurrence; // apply this rule at a specific occurrence
condition; // stores the code for the condition
statements; // stores the code for the statements
I'm no expert, please help me solve this problem. Thank you.
Assume following user defined rules stored in table:
-----------------------------------------------------------------
|eventid|occurance|keep-old-x|keep-old-y|x-frequency|y-frequency|
-----------------------------------------------------------------
| A | 1 | T | F | 1 | 100 |
-----------------------------------------------------------------
| B | 2 | F | T | -2 | 0 |
-----------------------------------------------------------------
| C | 5 | T | T | 100 | -3 |
-----------------------------------------------------------------
Lets say before event X = 10, Y = 12.
Event = A, ocuuurance = 1, keep-old-x = T, keep-old-y = F, x-frequency = 1, y-frequency = 100
if keep-old-x is T then
X = X + x-frequency
else
X = x-frequency
endif
if keep-old-y is T then
Y = Y + y-frequency
else
Y = y-frequency
endif
Now, X = 11, Y = 100
You may need to add two more columns to change value of X variable on specific value; as:
--------------------------
|if-x-value| x-new-value |
--------------------------
| 100 | 0 |
--------------------------
| 125 | 5 |
--------------------------
| 150 | 10 |
--------------------------
I hope this helps.

How to identify grammar is LR(0) or SLR(1)?

Is this grammar LR(0) or SLR(1)?
S -> E $
E -> T + E | T
T -> x
The following diagram proves that this grammar is NOT in LR(0) (it has a shift reduce conflict):
+--------------+ E +------------+
| |----------> | S -> E . $ |
| S -> . E $ | +------------+
| E -> . T + E |
| E -> . T | T +--------------+ state 2
| T -> . x |----------> | E -> T . + E | This state contains a
| | | E -> T . | Shift-Reduce conflict.
+--------------+ +--------------+
| | + ^
| x V | T
| +--------------+
V | E -> T + . E |
+---------------+ x | E -> . T + E | +--------------+
| T -> x . | <---------| E -> . T |--------> | E -> T + E . |
+---------------+ | T -> . x | +--------------+
+--------------+
However, it IS in SLR(1) because the conflict in state 2 can be resolved by using the fact that the + token is not in FOLLOW(E).
Since SLR(1) parsers can look 1 token ahead, they can decide to shift in state 2 if the next token is + (and solve the conflict by doing so).
If the SLR(1) parser is in state 2, and next token is +,
why not choose reduce?
Well, suppose the parser chooses to reduce E -> T.
Then eventually the + token will be read, and it will follow either E,
or some other variable that E was derived from (only S in this grammar).
But neither E nor S can have the + token (immediately) follow them!

Trouble understanding meaning of functional dependency notation (A - > BC)

I'm having a hard time visualizing exactly what A->BC means, mainly what exactly BC does.
For example, on a table "If A -> B and B -> C, then A -> C" would look like this, and the statement would be true:
A | B | C
1 | 2 | 3
1 | 2 | 3
What would A -> BC look like?
How would you show something like "If AB -> C, then A -> BC" is false?
Thanks!
EDIT:
My guess at it is that AB -> C means that C is dependant on both A and B, so the table would look like this:
A | B | C
1 | 2 | 3
1 | 2 | 3
Or this (which would be a counterexample for my question above):
A | B | C
1 | 2 | 4
1 | 3 | 4
And both would be true. But this would be false:
A | B | C
1 | 2 | 4
1 | 3 | 5
Is that the right idea?
In case you haven't already read this, it's an okay introduction to functional dependencies. It says:
Union: If X → Y and X → Z, then X → YZ
Decomposition: If X → YZ, then X → Y and X → Z
I find it helpful to read A -> B as "A determines B", and read A -> BC as "A determines B and C". In other words, given an A, you can uniquely determine the value of B and C, but it's not necessarily true that given a B and a C, you can uniquely determine the value of A.
Here's a simple example: a table with at least 3 columns, where A is the primary key and B and C are any other columns:
id | x | y
------------
1 | 7 | 4
2 | 9 | 4
3 | 7 | 6
To show that If AB -> C, then A -> BC is false, you just have to come up with a single counter-example. Here's one: a table where AB is the primary key (therefore by definition it satisfies AB -> C):
A | B | C
------------
1 | 1 | 4
1 | 2 | 5
2 | 1 | 6
2 | 2 | 4
However, it does not satisfy A -> B (because for A=1, B=1,2) and therefore, by Union, it does not satisfy A -> BC. (Bonus points: does it satisfy A -> C? Does it matter?)

Resources