Anatomy of programming languages

Page 12

2.1. SIMPLE LANGUAGE OF ARITHMETIC

Figure 2.1: Graphical illustration of abstract structure The last picture represents both the last expressions in the previous example. This is because the pictures do not need parentheses, since the grouping structure is explicit. The conceptual structure (illustrated by the pictures) is called the abstract syntax of the language. The particular details and rules for writing expressions as strings of characters is called the concrete syntax. The abstract syntax for arithmetic expressions is very simple, while the concrete syntax is quite complex. As a result, for now we will focus on the abstract syntax, and deal with concrete syntax later.

2.1.1

Abstract Syntax in Haskell

This section describes how to represent abstract syntax using Haskell. The code for this section is found in the Simple file. Arithmetic expressions can be represented in Haskell with the following data type: data Exp = Number | Add | Subtract | Multiply | Divide

Int Exp Exp Exp Exp

Exp Exp Exp Exp

This data type defines five representational variants, one for numbers, and four for the the binary operators of addition, subtraction, multiplication, and division. A number that appears in a program is called a literal. The five examples given above can be written as values of type Exp to create five test cases: −−4 t1 = Number 4 − − −5 + 6 t2 = Add (Number (−5)) (Number 6) − − 3 − (−2 ) − (−7 ) t3 = Subtract (Subtract (Number 3) (Number (−2))) (Number (−7)) 12

2013/04/24 14:15:36


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.