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.