<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>4.4. Example Attribute Grammars</title><link rel="stylesheet" href="fptools.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="Happy User Guide"><link rel="up" href="sec-AttributeGrammar.html" title="Chapter 4. Attribute Grammars"><link rel="prev" href="sec-AttrGrammarLimits.html" title="4.3. Limits of Happy Attribute Grammars"><link rel="next" href="sec-invoking.html" title="Chapter 5. Invoking Happy"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4.4. Example Attribute Grammars</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="sec-AttrGrammarLimits.html">Prev</a> </td><th width="60%" align="center">Chapter 4. Attribute Grammars</th><td width="20%" align="right"> <a accesskey="n" href="sec-invoking.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-AttributeGrammarExample"></a>4.4. Example Attribute Grammars</h2></div></div></div><p> The following two toy attribute grammars may prove instructive. The first is an attribute grammar for the classic context-sensitive grammar { a^n b^n c^n | n >= 0 }. It demonstrates the use of conditionals, inherited and synthesized attributes. </p><pre class="programlisting"> { module ABCParser (parse) where } %tokentype { Char } %token a { 'a' } %token b { 'b' } %token c { 'c' } %token newline { '\n' } %attributetype { Attrs a } %attribute value { a } %attribute len { Int } %name parse abcstring %% abcstring : alist blist clist newline { $$ = $1 ++ $2 ++ $3 ; $2.len = $1.len ; $3.len = $1.len } alist : a alist { $$ = $1 : $2 ; $$.len = $2.len + 1 } | { $$ = []; $$.len = 0 } blist : b blist { $$ = $1 : $2 ; $2.len = $$.len - 1 } | { $$ = [] ; where failUnless ($$.len == 0) "blist wrong length" } clist : c clist { $$ = $1 : $2 ; $2.len = $$.len - 1 } | { $$ = [] ; where failUnless ($$.len == 0) "clist wrong length" } { happyError = error "parse error" failUnless b msg = if b then () else error msg } </pre><p> This grammar parses binary numbers and calculates their value. It demonstrates the use of inherited and synthesized attributes. </p><pre class="programlisting"> { module BitsParser (parse) where } %tokentype { Char } %token minus { '-' } %token plus { '+' } %token one { '1' } %token zero { '0' } %token newline { '\n' } %attributetype { Attrs } %attribute value { Integer } %attribute pos { Int } %name parse start %% start : num newline { $$ = $1 } num : bits { $$ = $1 ; $1.pos = 0 } | plus bits { $$ = $2 ; $2.pos = 0 } | minus bits { $$ = negate $2; $2.pos = 0 } bits : bit { $$ = $1 ; $1.pos = $$.pos } | bits bit { $$ = $1 + $2 ; $1.pos = $$.pos + 1 ; $2.pos = $$.pos } bit : zero { $$ = 0 } | one { $$ = 2^($$.pos) } { happyError = error "parse error" } </pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sec-AttrGrammarLimits.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sec-AttributeGrammar.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sec-invoking.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4.3. Limits of Happy Attribute Grammars </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 5. Invoking <span class="application">Happy</span></td></tr></table></div></body></html>