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.

calc :- readln (L,___, lowercase), preprocess (L, PreL), print (PreL), nl, expr


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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!