You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have this exercise that we have to do in class and the professor asked us to figure out good reasons to choose the listener method over visitor method in order to implement a solution for a semantic problem. Although visitor seems more reasonable, he asked us to find possible reasons to choose the listener method over visitor.
Here´s the title of the exercise:
Let us assume a language of logical expressions called EXPL with the following expressive resources: variables, integers, functions, predicates, negation, conjunction, disjunction, implication and universal and existential quantifiers. The predicates and functions are prefix symbols with the exceptions of the relational predicates equal to, greater than, less than, less than or equal to, greater than or equal to and distinct and the functions addition, subtraction, product and division which are infixes.
An example of the EXPL expressions is:
1. FORALL x,y:(p(f(x)+2-3*5) OR r(x,y) => EXISTS z:(z+2 == 15))
2. FORALL x,y:(p(x) AND NOT r(x,y) => z == x)
3. (result == i AND i<=numberOfElements(v))
The semantic problem that we have to treat is that: whenever we find an entry with universal 'FORALL' or existencial 'EXISTS' openning, we have to make sure that the variables declared right next to these operators are used at least once in the logical expression that follows them, for example in this entry:
FORALL x,y:(p(f(x)+2-3*5) OR r(x,y) => EXISTS z:(z+2 == 15))
We have to make sure that the variables declared: x,y show up at least once in the logical expression next to it. (p(f(x)+2-3*5) OR r(x,y) => EXISTS z:(z+2 == 15))
In this case is correct because they appear here f(x) and here r(x,y) at least once.
Also we would have to check EXISTS z:(z+2 == 15)`
checking if the variable z show up in the (z+2 == 15) expression, which is also correct.
Now, regarding the visitor or listener situation, it seems a good reason to choose the visitor method becase we would go through the tree generated only looking for the expressions that are quantifiers returning for example a bool variable that checks whether the variables appear on the right expression or not. If we used a listener method, we would have to create global variables in order to store the variables since we cannot return anything, and then we would have to clear the lists or sets that we would use so that when it enters in the rule again the global variables are clean.
I have seen that a good other reason to choose a listeners is because they use an explicit stack allocated on the heap, whereas visitor uses call stack to manage tree traversals. This might lead to StackOverFlow exceptions while using visitor on deeply nested ASTs.
I would like to know more pros regarding the use of listeners for this exercise.
If the problem is not clear enough I can specify more, let me know!
Thanks!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
We have this exercise that we have to do in class and the professor asked us to figure out good reasons to choose the listener method over visitor method in order to implement a solution for a semantic problem. Although visitor seems more reasonable, he asked us to find possible reasons to choose the listener method over visitor.
Here´s the title of the exercise:
Let us assume a language of logical expressions called EXPL with the following expressive resources: variables, integers, functions, predicates, negation, conjunction, disjunction, implication and universal and existential quantifiers. The predicates and functions are prefix symbols with the exceptions of the relational predicates equal to, greater than, less than, less than or equal to, greater than or equal to and distinct and the functions addition, subtraction, product and division which are infixes.
An example of the EXPL expressions is:
The semantic problem that we have to treat is that: whenever we find an entry with universal 'FORALL' or existencial 'EXISTS' openning, we have to make sure that the variables declared right next to these operators are used at least once in the logical expression that follows them, for example in this entry:
We have to make sure that the variables declared: x,y show up at least once in the logical expression next to it. (p(f(x)+2-3*5) OR r(x,y) => EXISTS z:(z+2 == 15))
In this case is correct because they appear here f(x) and here r(x,y) at least once.
Also we would have to check EXISTS z:(z+2 == 15)`
checking if the variable z show up in the (z+2 == 15) expression, which is also correct.
Now, regarding the visitor or listener situation, it seems a good reason to choose the visitor method becase we would go through the tree generated only looking for the expressions that are quantifiers returning for example a bool variable that checks whether the variables appear on the right expression or not. If we used a listener method, we would have to create global variables in order to store the variables since we cannot return anything, and then we would have to clear the lists or sets that we would use so that when it enters in the rule again the global variables are clean.
I have seen that a good other reason to choose a listeners is because they use an explicit stack allocated on the heap, whereas visitor uses call stack to manage tree traversals. This might lead to StackOverFlow exceptions while using visitor on deeply nested ASTs.
I would like to know more pros regarding the use of listeners for this exercise.
If the problem is not clear enough I can specify more, let me know!
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions