Can someone give me a brief description of how to determine the precedence of one thing with respect to another in a grammar? I can't seem to find a good answer in my books or online even though i'm pretty sure its very simple. Example
S-> B
B-> B and T | T
T-> T or F | F
F-> not F| (B)|true|false
The question was "what is the precedence of "and" with respect to "or" in the above grammar?".
"and" has lower precedence than "or", but I'm not sure why.
The symbol B represents the and statement, and its definition contains T which is the or. You can get from and to or, but not the other way around. So if you think of the language statement as a stack, you'll keep doing replacements and adding to the top of the stack until you wind up evaluating the whole statement, and as you then pop items off you'll reverse direction and the or statement will always be above (and therefore evaluate before) the and. Hope that makes sense.
Related
I'm struggling to get a SQL statement to run.
I need to have an and / or statement which gives me:
Where Condition 1 is true
OR
Where both Condition 2 AND Condition 3 are true. (not only one of them)
Appreciate some ideas :)
You can split them with parenthesis, you also only need to define 'WHERE' once. Example:
WHERE
{condition_1} or ({condition_2} and {condition_3})
Edit: You don't technically require parenthesis due to AND having a higher precedence than OR, but it makes it much easier to read and see at a glance exactly what you're trying to do.
I'm tasked with writing a regular grammar based on a regular expression.
Given the regular expression a*b can be written as S -> b | aS
Is it incorrect that ba* as a regular grammar is S -> b | Sa?
I'm told the correct answer is in fact S -> bA, A -> ^| aA but I don't see the difference myself.
An explanation would be greatly appreciated!
IIRC, both your answer and the one being called "correct" are correct. See this. What you have constructed is a "left regular grammar", while the proponent(s) of the "correct" answer obviously prefer a "right regular grammar". There are other arbitrary rules that may be held more or less pedantically, like the "no empty productions" rule, but they don't really affect the class of regular languages, just the compactness of the grammar you use for a particular language, as your example highlights - a single production with two alternatives vs. two productions, one with a single clause, and one with two alternatives, one of which is empty.
I see this:
Project.update_all("cost = cost * 3",
"lower(technology) LIKE '%microsoft%'")
as an example of update_all method in Active Record when I'm following The Rails 3 Way, very simple phrase, huh? But I just can't figure out what do parentheses mean in lower(technology) here.
So, Could you tell me some possible answers? Because I don't know if there are some different situations we can use parentheses like this.
thanks.
They call the SQL LOWER function to lowercase the string.
LOWER technology
would be a syntax error, because LOWER is a function, not a keyword.
I was given a regular expression, and I am suppose to covert it to NFA and then DFA. Here's the regular expression:
a ( b | c )* a | a a c* b
Then I coverted this to NFA using Thomson's algorithm:
and here's the DFA:
Can someone please take a quick look at let me know if I am wrong or right?
Since this is very likely homework, I'm hesitant to just give you the complete correct solution.
Your NFA appears correct, but has a lot of superfluous states that aren't necessary but do not adversely affect its correctness. (At first glance it looks like you could remove 11 states.)
Your DFA is incorrect, though. This is because when you branch off to begin handling one condition of the string or the other, you later rejoin them together. This allows it to take the path from an accepted string matching a(b|c)*a and take in another b or c by travelling to nodes 15,17 or 11. It then accepts this string even though it doesn't match your expression.
What you need to do is basically stop this from happening. If you have additional questions feel free to ask.
I highly recommend making a list of test strings that you know should be and shouldn't be accepted, and then trace them through, making sure your automata ends in the correct (accept or reject) state.
I'm looking at some SQL code which has a WHERE clause like this:
WHERE 'USD' = CCY
I asked the writer why he's putting the value on the left hand side, and he said it's best practice to do so, stemming from C++ where people could mistakenly assign the value instead of comparing equality by forgetting the second equals sign.
I've never seen this before.
What are your thoughts?
Er, C++ is not SQL. There's no == in SQL and no assignments in a WHERE clause.
I'm not sure it qualifies as "best practice" but there is a convention which places the known value on the right-hand side. So, with a literal as in your example that would be
WHERE CCY = 'USD'
Best practise in c++ does not make it best practise in SQL. The query optimizer will not care, so it is just a matter of preference, but I have to say it would not be my preference or how I would naturally write it.
Never seen it in SQL, where of course the C++ reasoning does not apply, as '=' is not an assignment operator in this context. also, a lot of C++ programmers (including me) don't like this style.
If you look at it:
'USD' = CCY
is essentially the same:
CCY = 'USD'
As for:
it's best practice to do so, stemming
from C++ where people could mistakenly
assign the value instead of comparing
equality by forgetting the second
equals sign.
Well, i have never seen this happen, and if this was that important, we would definitely have seen this somewhere and this would have been practiced by most if not by all.
I personally would not do it that way, but put the column name on the left hand side as this to me is more readable / easier to follow within an SQL query.
I've very rarely seen it done the opposite way, and don't think the reason given is really applicable to SQL (as has been pointed out, it's "=" in SQL, not "==")
If he says it's a best practice, I'd ask him to prove that with SQL not C++ sources. Since 99.9% of the SQL code I've ever read (including our code, other organization's code, Microsoft help files, SQL Blogs, etc) does the opposite of what your dev does, I'd say that violating the normal expectation of the developer who will maintain the code is a bad idea. In SQL we expect to see the form
WHERE CCY = 'USD'
not
WHERE 'USD' = CCY
Therefore the professsional would also write code in that manner to ensure it is clear to the maintainer.