In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 6 // Exercise 7
Write a grammar for bitwise logical expressions. A bitwise logical expression is much like an arithmetic expression except that the operators are ! (not), ~ (complement), & (and), | (or) and ^ (exclusive or).
Each operator does its operation to each bit of its integer operands. ! and ~ are prefix unary operators.
A ^ binds tighter than a | (just as * binds tighter than +) so that x|y^z means x|(y^z) rather than (x|y)^z. The & operator binds tighter than ^ so that x^y&z means x^(y&z).
Expression:
Third Term
Expression "|" Third Term
Third Term:
Second Term
Third Term "^" Second Term
Second Term:
First Term
Second Term "&" First Term
First Term:
Primary
First Term "!" Primary
First Term "~" Primary
Primary:
Object //int, char, string etc
"(" Expression ")"
Following the example earlier in chapter 6 I think this is how it would be done although I'm not entirely sure. English was always my best subject but grammar in programming is throwing my brain for a loop.
EDIT 07/10/2019 - This was almost correct the first time I did it. However, now I understand his thought process a little better on grammars I've realised that the First Term section is incorrect. "!" and "~" are always pre-fix operators and as such it will be sufficient for First term to look like this:
Primary
"!" Primary
"~" Primary
This way, they will always affect the Primary instead of the left value. Here is an updated version on GitHub.
I don't think that your solution is correct :/
ReplyDeleteIt is important to note that '&' is stronger than '^' and '^' is stronger than '|'
This is how I did this... what do you think?
Platform 1:
Platform 2
Platform 1 “!” Platform 2
Platform 1 “~” Platform 2 Term
Platform 2:
Platform 3
Platform 2 “|” Platform 3
Platform 3:
Platform 4
Platform 3 “^” Platform 4
Platform 4:
Final Platform
Platform 3 “&” Platform 4
Final Platform:
Object
"(" Expression ")"
If you think that I am wrong then please do tell me...
Thanks!
I have done it back to front, the way he does in the book, I think that's why it looks a little confusing.
DeleteI can't actually remember how I did this, I know a question after this got us writing bitwise grammar and the code works.
Looking at what I've written I think I'm trying to say that a primary is an expression within brackets. An expression is a third term. Then a third term is a thrid followed by second and then a second term is a second followed by first. It's very confusing and I don't know how to explain it.
He tells us that & is the strongest followed by ^ and then |. I put ! and ~ with the primary(the object) because these are prefix operators and so can come before the actual object so the PC needs to look for that in case the user inputs those first instead of a number. Then it looks for an &, then ^ and finally |. That is the expression which makes up the primary.
At least that's how it looks to me lol.
OMG! You are right. This is what happens when I don't read the questions properly. I thought that '!' and '~' works like '+' and '-'.
DeleteLOL!