(* * This is a version of ../inputs//term-factor-does-work.rsl with the latest * and greates grammar-like syntax. *) module TermFactorAttributeGrammar; (* * Attribute definitions. *) define obj attribute type:Type; define obj attribute valu:Value; define obj attribute text:string; (* * 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(Symtab, 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} | 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;} ; end TermFactorAttributeGrammar;