Creating grammars for lexical analyzer - lexical-analysis

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?

Related

How to draw a DFA for that given language?

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

Having trouble with Postgres unnest array syntax

I am looking for guidance on the best way to do this insert. I am trying to create 11 entries for role_id 58385 while looping through the values of each of these arrays. I am new to PostgreSQL and need some guidance as to what I am doing wrong in this instance.
INSERT INTO public.acls (role_id, acl_id, update, can_grant, retrieve, create, archive) VALUES (
'58385',
unnest(array[1,14,20,21,22,24,25,26,36,300,302]),
unnest(array[f,f,t,t,f,f,f,t,f,t,t]),
unnest(array[f,f,f,f,f,f,f,f,f,f,f]),
unnest(array[t,t,t,t,t,t,t,t,t,t,t]),
unnest(array[f,f,t,t,f,f,f,t,f,t,t]),
unnest(array[f,f,f,f,f,f,f,f,f,f,f])
)
Do I need a SELECT subquery for each of the arrays? Or could I make one array from the six and Insert them.
A single select will do it for you, but t and f will need to be true and false:
select '58385',
unnest(array[1,14,20,21,22,24,25,26,36,300,302]),
unnest(array[false,false,true,true,false,false,false,true,false,true,true]),
unnest(array[false,false,false,false,false,false,false,false,false,false,false]),
unnest(array[true,true,true,true,true,true,true,true,true,true,true]),
unnest(array[false,false,true,true,false,false,false,true,false,true,true]),
unnest(array[false,false,false,false,false,false,false,false,false,false,false])
;
?column? | unnest | unnest | unnest | unnest | unnest | unnest
----------+--------+--------+--------+--------+--------+--------
58385 | 1 | f | f | t | f | f
58385 | 14 | f | f | t | f | f
58385 | 20 | t | f | t | t | f
58385 | 21 | t | f | t | t | f
58385 | 22 | f | f | t | f | f
58385 | 24 | f | f | t | f | f
58385 | 25 | f | f | t | f | f
58385 | 26 | t | f | t | t | f
58385 | 36 | f | f | t | f | f
58385 | 300 | t | f | t | t | f
58385 | 302 | t | f | t | t | f
(11 rows)

Find all possible relationships between two nodes in multi-parent hierarchy data model [SQL Server]

I have a data model to define multi-parent hierarical data. Each record will represent a relationship of two nodes in which one will be a parent node and another will be a child node. In my case, a node can have multiple parents. I need to find all possible relationsips between two nodes.
For example take the below table.
---------------------------------
| id | parent_node | child_node |
---------------------------------
| 1 | NULL | A |
| 2 | NULL | B |
| 3 | A | C |
| 4 | A | D |
| 5 | B | D |
| 6 | B | E |
| 7 | C | G |
| 8 | C | H |
| 10 | D | I |
| 11 | E | I |
| 12 | E | J |
---------------------------------
This will form a graph like below
A B
/ \ / \
C D E
/ \ \ / \
G H I J
In the above model, A and B will be the top level node and each has two children. Node D is assigned as the child of node A and B. And also node I is assigned as the child of node D and node E. All other nodes has exactly one parent.
I need to write a query to show all possible relationship of a node with another node.
For example,
A and C has a relationship, because C is child of Node A.
A and D has a relationship, because D is child of Node A.
A and G has a relatiohship, because G is the grandchild of Node A.
This will go for any number of levels.
Two nodes doesn't have any relationship, if any one node is not a child or nth-level grandchild of another.
If two nodes doesn't have any relationship, it will not show up.
The final outcome for the above graph will be as below,
----------------------------
| parent_node | child_node |
----------------------------
| A | C |
| A | D |
| C | G |
| C | H |
| D | I |
| A | G |
| A | H |
| A | I |
| B | D |
| B | E |
| B | I |
| E | I |
| E | J |
| B | J |
----------------------------
I am new to SQL Server. Please help me to solve this query.
By doing some research, I was able to write the query myself. As #SeanLange pointed out in the comments, this type of query is called a recursive CTE.
If the table name is nodes, the following query will create the new table relationship and store all possible relationships in it as mentioned in my question.
;with cte as (
select child_node
, parent_node
, child_node as root
from nodes
union all
select child.child_node
, child.parent_node
, parent.root
from cte parent
join nodes child
on parent.parent_node = child.child_node
)
select parent_node,
root as child_node
into relationship
from cte
where parent_node is not null;
select * from relationship;

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