(* * This file illustrates how to create an attribute-grammar-style definintion * in RSL. Syntactically, the notation is slightly different than most * standard CFG notations, however these syntactic differences are quite minor. * The differences are: * * (1) Elements of the RHS of a rule must be separated with one of the two * normal RSL operators 'and' or ',', rather than the more typical * blanks. * (2) The normal RSL attribute selection operator '.:' is used to select * the value of symbol attributes rather than the more typical '.'. * (3) The RSL keyword "is" is used in place of the more standard '::=' * rule separator (actually, we allow "::=" now). * * What does not work here is the use of '{' and '}' to enclose semantic * actions. I tried to fit this syntactic use of curly braces into the RSL * grammar, but it was a pain given the normal use of curlies as the tuple * constructor. * * Note that this grammar is patterned directly after the examples in CSC 451 * lectures notes week 9. See ~gfisher/classes/451/lectures/9.ps. * * Cf ./term-factor-does-work.rsl and ./term-factor-doesnt-work.rsl. *) module TermFactorAttributeGrammar; (* * Attribute definitions. *) define obj attribute type:Type; define obj attribute valu:Value; (* * Attribute type definitions. *) obj Type = string; obj Value = number; (* * Grammar and semantic action definitions. *) rule E = e:E "+" t:T { this.:type = (if e.:type = t.:type then e.:type else "ERROR"); this.:valu = e.:valu + t.:valu; } end E; rule E = t:T { this.:type = t.:type; this.:valu = t.:valu; } end; rule T = t:T "*" f:F; rule T' = f:F; rule F = "x"; end TermFactorAttributeGrammar; module TermFactorAttributeGrammarAlt; (* * Attribute definitions. *) define obj attribute type:Type; define obj attribute valu:Value; (* * Attribute type definitions. *) obj Type = string; obj Value = number; (* * Grammar and semantic action definitions. *) rule E = e:E "+" t:T {this.:type = (if e.:type = t.:type then e.:type else "ERROR"); this.:valu = e.:valu + t.:valu;} | t:T {this.:type = t.:type; this.:valu = t.:valu;} end E; rule E = end E; rule T = t:T "*" f:F; rule T' = f:F; rule F = "x"; end TermFactorAttributeGrammarAlt;