# Introduction to Programming Languages/Syntax Directed Interpretation

### Syntax Directed Interpretation[edit | edit source]

An interpreter is a program that simulates the execution of programs written in a particular programming language. There are many ways to implement interpreters, but a typical implementation relies on the parsing tree as the core data structure. In this case, the interpreter is a visitor that traverses the derivation tree of the program, simulating the semantics of each node of this tree. As an example, we shall build an interpreter for the language of arithmetic expressions whose logic grammar is given below:

expr --> mulexp, [+], expr. expr --> mulexp. mulexp --> rootexp, [*], mulexp. mulexp --> rootexp. rootexp --> ['('], expr, [')']. rootexp --> number. number --> digit. number --> digit, number. digit --> [0] ; [1] ; [2] ; [3] ; [4] ; [5] ; [6] ; [7] ; [8] ; [9].

Any program in this simple programming language is ultimately a number. That is, we can ascribe to a program written in this language the meaning of the number that the program represents. Consequently, interpreting a program is equivalent to finding the number that this program denotes. The attribute grammar below implements such interpreter:

expr(N) --> mulexp(N1), [+], expr(N2), {N is N1 + N2}. expr(N) --> mulexp(N). mulexp(N) --> rootexp(N1), [*], mulexp(N2), {N is N1 * N2}. mulexp(N) --> rootexp(N). rootexp(N) --> ['('], expr(N), [')']. rootexp(N) --> number(N, _). number(N, 1) --> digit(N). number(N, C) --> digit(ND), number(NN, C1), { C is C1 * 10, N is ND * C + NN }. digit(N) --> [0], {N is 0} ; [1], {N is 1} ; [2], {N is 2} ; [3], {N is 3} ; [4], {N is 4} ; [5], {N is 5} ; [6], {N is 6} ; [7], {N is 7} ; [8], {N is 8} ; [9], {N is 9}.

Notice that we can use more than one attribute per node of our derivation tree. The node `number`

, for instance, has two attributes, which we use to compute the value of a sequence of digits. Below we have a few examples of queries that we can issue in this language, assuming that we have saved the grammar in a file called `interpreter.pl`

:

?- consult(interpreter). % num2 compiled 0.00 sec, 4,340 bytes true. ?- expr(N, [2, *, 1, 2, 3, +, 3, 2, 1], []). N = 567 ; false. ?- expr(N, [2, *, '(', 1, 2, 3, +, 3, 2, 1, ')'], []). N = 888 ; false.