Step 6: Adding semantic functions

4.6 Step 6: Adding semantic functions

  Once we have the basic parsing functions, we need to add the semantic glue: the functions that take the results of the elements in the right hand side of a production, and convert them into the result of the left hand side. The basic rule is: Let the types do the work!

  First we add semantic functions to the basic parsing functions station, time, departure, arrival, and spaces. Since function identifier already returns a string, we can take the identity function id for undefined in function station. Since id <> is the identity function, it can be omitted. To obtain a value of type Time from an integer, a character, and an integer, we have to combine the two integers in a tuple. So we take the following function

  \x y z -> (x,z) for undefined in time. Now, since function time returns a value of type Time, we

  can take the identity function for undefined in departure and arrival, and then we replace id <> time by just time. Finally, the result of many is a string, so for undefined in spaces we can take the identity function too.

  The first semantic function for the basic parser tsstring defined in Section 4.5.1 returns an abstract syntax tree of type TS2. So the first undefined in tsstring should return a tuple of a list of things of the correct type (the first component of the type TS2) and a Station. Since many returns a list of things, we can construct such a tuple by means of the function

  \x y z -> (x,z) provided many returns a value of the desired type: [(Station,Time,Time)]. Note

  that this semantic function basically only throws away the value returned by the spaces parser: we are not interested in the spaces between the components of our travelling scheme. The many parser returns a value of the correct type if we replace the second occurrence of undefined in tsstring by the function

  \u v w x y z -> (v,x,z)

  Grammar and Parser design

  Again, the results of spaces are thrown away. This completes a parser for travelling schemes.

  The next semantic functions we define compute the net travel time. To compute the net travel time, we have to compute the travel time of each trip from a station to a station, and to add the travel times of all of these trips. We obtain the travel time of a single trip if we replace the second occurrence of undefined by:

  \u v w (xh,xm) y (zh,zm) -> (zh-xh)60 + zm-xm and Haskell’s prelude function sum sums these times, so for the first occurrence of

  undefined we take:

  \x y z -> sum x The final set of semantic functions we define are used for computing the total waiting

  time. Since the second grammar of Section 4.1 combines arrival times and departure times, we use a parser based on this grammar: the basic parser tstoken2. We have to give definitions of the three undefined semantic functions. If a trip consists of

  a single station, there is now waiting time, so the last occurrence of undefined is the function const 0. The second occurrence of function undefined computes the waiting time for one intermediate station:

  \(uh,um) v (wh,wm) -> (wh-uh)60 + wm-um Finally, the first occurrence of undefined sums the list of waiting time obtained by

  means of the function that replaces the second occurrence of undefined:

  \s t x y z -> sum x

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

ANTARA IDEALISME DAN KENYATAAN: KEBIJAKAN PENDIDIKAN TIONGHOA PERANAKAN DI SURABAYA PADA MASA PENDUDUKAN JEPANG TAHUN 1942-1945 Between Idealism and Reality: Education Policy of Chinese in Surabaya in the Japanese Era at 1942-1945)

1 29 9

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

Improving the VIII-B Students' listening comprehension ability through note taking and partial dictation techniques at SMPN 3 Jember in the 2006/2007 Academic Year -

0 63 87

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37

Transmission of Greek and Arabic Veteri

0 1 22