(* * This is a version of ../inputs//term-factor-does-work.rsl with the even * latester and greatester grammar-like syntax. *) module TermFactorAttributeGrammar; (* * Attribute definitions. *) define obj attribute type:Type; define obj attribute valu:Value; define obj attribute text:string; define obj attribute type_:Type; (* * Attribute type definitions. *) obj Type = string; obj Value = number; obj Symtab = SymtabEntry*; obj SymtabEntry = n:Name and t:Type and v:Value; obj Name = string; (* * Auxiliary functions. *) function Lookup(s:Symtab, n:Name)->SymtabEntry; (* * Global attributes. *) var symtab:Symtab; (* * 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;} -- ^ note extra ';', which is OK | t:T {this.:type = t.:type; this.:valu = t.:valu} ; rule T = t:T "*" f:F {this.:type = (if e.:type = t.:type then e.:type else "ERROR"); this.:valu = e.:valu * t.:valu} | f:F {this.:type = f.:type; this.:valu = f.:valu} ; rule F = x:"x" {this.:type = Lookup(symtab, x.:text).t; this.:valu = Lookup(symtab, x.:text).v} ; rule literal = sl:STRING_LITERAL { this.:type_ = Lookup(symtab, x.:text); this.:type = Lookup(symtab, x.:text).t; this.:valu = Lookup(symtab, x.:text).v} ; obj STRING_LITERAL; end TermFactorAttributeGrammar;