Attribute grammars

8.3 Attribute grammars

  In Section 8.1 we have written a program that solves the rep min problem. This program computes the minimum of a tree, and it computes the tree in which all the leaf values are replaced by the minimum value. The minimum is computed bottom- up: it is synthesized from its children. The minimum value is then passed on to the functions that build the tree with the minimum value in its leaves. These functions receive the minimum value from their parent tree node: they inherit the minimum value from their parent.

  We can see the rep min computation as a computation on a value of type Tree, on which two attributes are defined: the minimum and result tree attributes. The minimum is computed bottom-up, and is then passed down to the result tree, and is therefore a synthesized and inherited attribute. The result tree is computed bottom- up, and is hence a synthesized attribute.

  The formalism in which it is possible to specify such attributes and computations on datatypes or grammars is called attribute grammars, and was originally proposed

  Programming with higher-order folds

  compile = foldExprAS compileAlgebra compileAlgebra :: ExprASAlgebra (Label -> ([InstructionSM],Label))

  compileAlgebra = (\cce cte cee -> \l ->

  let (cc,l’) = cce (l+2)

  (tc,l’’) = cte l’ (ec,l’’’) = cee l’’

  in (

  cc ++ [BrFalse l] ++ tc ++ [BrAlways (l+1)] ++ [SetLabel l] ++ ec ++ [SetLabel (l+1)]

  ,l’’’ )

  ,\cf cx -> \l -> let (xc,l’) = cx l

  (fc,l’’) = cf l’ in (xc ++ fc ++ [Call],l’’)

  ,\i -> \l -> ([LoadInt i],l) ,\b -> \l -> ([LoadBool b],l) )

  Listing 18: CompileExpr.hs

  8.3 Attribute grammars

  by Donald Knuth in [10]. Attribute grammars provide a solution for the system- atic description of the phases of the compiler that come after scanning and parsing. Although they look different from what we have encountered thus far and are prob- ably a little easier to write, they can straightforwardly be mapped onto a functional program. The programs you have seen in this chapter could also have been obtained by means of such a mapping from an attribute grammar specification. Traditionally such attribute grammars are used as the input of a compiler generator. Just as we have seen how by introducing a suitable set of parsing combinators one may avoid the use of a special parser generator and even gain a lot of flexibility in extend- ing the grammatical formalism by introducing more complicated combinators, we have shown how one can do without a special purpose attribute grammar processing system. But, just as the concept of a context free grammar was useful in understand- ing the fundamentals of parser combinators, understanding attribute grammars will help significantly in describing the semantic part of the recognition and compilation process. This chapter does not further introduce attribute grammars, but they will appear again in the course in implementing programming languages.

  Programming with higher-order folds

  Chapter 9

  Pumping Lemmas: the expressive power of languages

  introduction

  In these lecture notes we have presented several ways to show that a language is regular or context-free, but until now we did not give any means to show the nonregularity or noncontext-freeness of a language. In this chapter we fill this gap by introducing the so-called Pumping Lemmas. For example, the pumping lemma for regular languages says

  IF language L is regular, THEN it has the following property P : each sufficiently long word w ∈ L has

  a substring that can be repeated any number of times, every time yielding another word of L

  In applications, pumping lemmas are used in the contrapositive way. In the regular case this means that one may conclude that L is not regular, if P does not hold. Although the ideas behind pumping lemmas are very simple, a precise formulation is not. As a consequence, it takes some effort to get familiar with applying pumping lemmas. Regular grammars and context-free grammars are part of the Chomsky hierarchy, which consists of four different kinds of grammars and their corresponding languages. Pumping lemmas are used to show that the expressive power of the different elements of the Chomsky hierarchy is different.

  goals

  After you have studied this chapter you will be able to

  • prove that a language is not regular; • prove that a language is not context-free; • identify languages and grammars as regular, context-free or none of these; • give examples of languages that are not regular, andor not context-free; • explain the Chomsky hierarchy.

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