Question
Implement a calculator prefix expression interpreter in Prolog as described in the section on attribute grammars in this chapter. The interpreter will read an expression
Implement a calculator prefix expression interpreter in Prolog as described in the section on attribute grammars in this chapter. The interpreter will read an expression from the keyboard and print its result. The interpreter should start with a calk predicate. Here is the calk predicate to get you started.
The program reads a list of tokens from the keyboard. The preprocess predicate should take the list of values and add numb tags to any number it finds in the list. This makes writing the grammar a lot easier. Any number like 6 in L should be replaced by numb ((6) in the list Pregl. The expr predicate represents the start symbol of your grammar. Finally, the interpret predicate is the attribute grammar evaluation of the AST represented by Tree. To make this project easy, write it incrementally. Print the results as you go so you can see what works and what doesn’t. The print predicate will print its argument while the nl predicate will print a newline. Don’t write the entire calk predicate right away. Write one piece, test it, and then move on to the next piece.
calc :- readln (L,___, lowercase), preprocess (L, PreL), print (PreL), nl, expr (Tree, PreL, []), print (Tree), nl, interpret (Tree , 0,_, Val), print (Val), nl.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started