I am interested in detecting redundant parentheses in OCaml code. Some ideas I have tried with no results include using regular expressions, comparing reverse code generated from AST. I am lost on how to proceed with this task.
There is a simple solution. Parse the code (using compilerlibs) and then print it back (again using compiler libs) and compare the results. The compilerlibs pretty printer will not put any redundant parentheses. To make the comparison easier, you can remove all spaces, or just count the number of parentheses.
There are less heave and more adhocy approaches, e.g., to catch the common misuse of parentheses:
f(x) instead of f x
(f x) * (f y), instead of f x * f y where * is an arbitrary infix operator.
Finally, the general approach, in case if you need for the student project. Would be to compare operators precedence and mark operators that have higher precedence (bind tighter) but still have parenthesis, e.g., (x * y) + z, here * has higher precedence than + but is still delimited with parentheses.
Related
When is it is necessary to put the whole (right) expression of a define macro in parenthesis?
If I do something like
#define SUM(x, y) ((x)+(y))
I have to put the right expression into parenthesis, because "+" has a low precedence in C (and it wouldn't work if I would use it in the following context SUM(x, y) * 5U)
Are these parenthesis still required if I use an operator of the highest precedence e.g.
#define F foo()
or even
#define ACCESS(x, y) (x)->(y)
Does an expression exist that that would break the actual meaning as it would do for the SUM() example?)
For the precedence rules I used http://en.cppreference.com/w/c/language/operator_precedence
It's a good practice to use parenthesis to avoid some big blunders. Currently you might think that your operator is having high precedence but suppose you're making the header file of your code and give it to your friend who uses macro from that file with the function having even higher precedence so at that time it will be a great pain to debug the code. You can save this time as a programmer by putting parenthesis.
Some programmers using simple #defines will write
#define ZERO (0)
which is of no use other than complicating things.
Actually the macro is wysiwyg so with that in mind you can decide what parentheses are needed:
SUM(3,4) => ((3)+(4)) // parentheses around 3 and 4 are not necessary
RATIO(3+4,4+5) => ((3+4)/(4+5)) // probably necessary here due to operator precedence
Its not rocket science to figure this out, it's actually quite straightforward.
I'm trying to write an expression parser. One part I'm stuck on is breaking down an expression into blocks via its appropriate order of precedence.
I found the order of precedence for C++ operators here. But where exactly do I split the expression based on this?
I have to assume the worst of the user. Here's a really messy over-exaggerated test example:
if (test(s[4]) < 4 && b + 3 < r && a!=b && ((c | e) == (g | e)) ||
r % 7 < 4 * givemeanobj(a & c & e, b, hello(c)).method())
Perhaps it doesn't even evaluate, and if it doesn't I still need to break it down to determine that.
It should break down into blocks of singles and pairs connected by operators. Essentially it breaks down into a tree-structure where the branches are the groupings, and each node has two branches.
Following the order of precedence the first thing to do would be to evaluate the givemeanobj(), however that's an easy one to see. The next would be the multiplication sign. Does that split everything before the * into a separate , or just the 4? 4 * givemeanobj comes before the <, right? So that's the first grouping?
Is there a straightforward rule to follow for this?
Is there a straightforward rule to follow for this?
Yes, use a parser generator such as ANTLR. You write your language specification formally, and it will generate code which parses all valid expressions (and no invalid ones). ANTLR is nice in that it can give you an abstract syntax tree which you can easily traverse and evaluate.
Or, if the language you are parsing is actually C++, use Clang, which is a proper compiler and happens to be usable as a library as well.
I am trying to calculate the expression i and I can't figure out how to print the value as I'm new to f#.
let mutable sqSum = 0.0
let mutable sqrRoot = 0.0
for i in [startNum..endNum] do
for j in [i..(i+k-1)] do
let x = j |> double
sqSum <- x*x
sqrRoot <- sqrt sqSum
if <>sqrRoot % 1.0 > 0.0 then
printfn "%i" i
The specific error message you are seeing should point to the exact location of the problem (at least for the compilers I use). So, specifically, it should be saying the infix operator located where "<>" is, is incorrect, which is true.
In many other languages, we use "!" to say the opposite of a boolean value. In F#, we may often use "<>" as a replacement for "not equal" or "!=". However, in your specific instance, we need to just simply use the keyword "not" and to surround the expression with parantheses (otherwise it will attempt to "not" a float value).
Specifically, you're code should look like this:
if not (sqrRoot % 1.0 > 0.0) then
As an additional note, you might want to fix your indentation as, like Python, indentation is very important in F# as it's not used for readability, but for specifying blocks of code. However, I cannot see all of your code, so it might be fine depending on how it is setup.
I'm trying to define some operators that add the operators with the arguments and the lone arguments to a list.
So far I've defined the two operators I'm gonna use, which are OR and NEGATION, but i don't know how to specify the actions they have to do next, which are creating the lists and add the operators and arguments to it.
:- op(400,fx,neg).
:- op(500,xfx,or).
After that I'm not certain of how to add the operators and arguments to a list and make a union of all the lists.
According to the union instruction in the manual it would be something like this:
neg(X,[]) :- union([X],[neg(X)],[]).
or(X,Y,[]) :- union([X],[or(X,Y)],[]).
or(X,Y,[]) :- union([Y],[or(X,Y)],[]).
Which doesn't send any errors, but how do I make the union of all the lists and how do I specify that any lower case letter can be used for input.
An example of an imput would be:
neg(a or b).
And the expected output:
[neg(a or b), a or b, a, b]
You're running into an interesting global thing about Prolog that operators really bring to the surface, which is that operators are just another way of constructing terms.
These operator definitions of yours enable you to create terms, such as:
?- X = neg a or b.
X = neg a or b.
Endowing these terms with meaning, on the other hand, will require you to create another predicate. This is because terms in Prolog are not expressions that reduce on their own—this is partly why you need to use is/2 to reduce an arithmetic expression to a value. Even things that are purely arithmetic are just terms in Prolog:
?- X = 16*3 + 4.
X = 16*3+4.
This is not some special behavior of =/2 in Prolog. This is how terms are made. Reducing the value requires deploying another predicate:
?- X is 16*3+4.
X = 52.
So what you appear to have done is assumed that your neg operator has induced a two-argument predicate for reducing it and that your or operator has induced a three argument predicate for reducing it. Actually neither of these things has happened, all that your operator declarations do is allow you to create terms like the one above, neg a or b. So you still need to create a separate predicate for evaluating them, which is where your semantics enter the picture. So let's implement an eval/2 predicate that converts your term into the result value you want:
eval(neg X, [neg X|Result]) :-
eval(X, Result).
eval(X or Y, [X or Y|Result]) :-
eval(X, R1),
eval(Y, R2),
append(R1, R2, Result).
eval(X, [X]) :- atomic(X).
The key idea here is to match what your operators give you and peel one layer off at a time, recursively calling the rest. Our base case is "atomic values" which is to say atoms like a.
This gives us what you're looking for:
?- eval(neg (a or b), R).
R = [neg (a or b), a or b, a, b]
Note that is/2 is an operator. You could also define an operator which "does work" by declaring the operator and then supplying rules for their application. That won't help you in this case, because your examples for neg and or require you to retain structure rather than discard it. is/2, on the other hand, assumes the structure exists on the right argument and reduces it to a value for the left. You could do something similar, by say making eval/2 an operator, in which case the operator is used on the left side of the :-, like so:
[Neg X|R] eval (neg X) :- R eval X.
However, I find this kind of hard to handle and would definitely avoid it unless it ushered in more clarity.
By the way, you will most likely want to replace xfx in your op/3 call with either xfy or yfx, since constructions like a or b or c will not work with xfx due to an operator priority clash. With xfy, it will be parsed as a or (b or c) and with yfx it will be parsed as (a or b) or c which is probably more helpful. And also, if you intend always to parenthesize what you use neg with, then you do not need to declare an operator for it—the purpose of a unary operator is just to allow you to skip the parens (and control how much of what follows is consumed by it.)
Hope this helps!
I have been presented the problem of reading an input file that contains logic statements, and am required to construct a Truth Table to determine whether an ASK matches any/all models that are determined. An example of some data I may expect to read in is:
(p & z => x) => ((p | d) & z)
Please don't get too caught up in the example and whether it really makes sense, I just made it up to show the different compositions I may be presented with. Multiple such statements can be separated with semicolons.
I have sorted out the semicolon splitting without any dramas, and now have a vector of strings containing each separate statement, where each string is as presented above. Now without parenthesis being involved, I believe determining the statements would be rather straight forward, but with their involvement I am now required to compute different sections before others. EG:
(p | d) = result and then (result & x)
I have seen people discussing the concept of using a stack to determine if open brackets are closed properly, but I do not believe that this would be appropriate for my situation as this would not allow me to determine what statements were inside what set of parenthesis.
The current idea I have is to use the stack idea, and try to determine the "depth" of a statement (essentially how far it is nested) and then mark this number with each statement, but I believe this sounds like an inelegant solution. Does anyone have any tips as to how I should construct an algorithm to properly address the problem?
You need to build a tree of the expressions, where your variables are leaves.
Your expression will then become:
=>
/ \
/ \
/ \
=> &
/ \ / \
& X | Z
/ \ / \
p z P D
Once you have built this kind of representation, the evaluation is straightforward.
Another approach, with the same result, is reducing your expression to something like RPN (where you can use your stack idea):
P, Z, &, X, =>, P, D, |, Z, &, =>
As suggested in the comments, you can do it with the shunting yard algorithm.