|
<<Grammar name="calc0_direct">>
top{ // semantic top level block using C# as host language
internal double result;
internal bool print_(){Console.WriteLine("{0}",result);return true;}
}
Expr: S Sum (!. print_ / FATAL<"following code not recognized">) ;
Sum
{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
internal _Sum(calc0_direct parent){parent_=parent;}
calc0_direct parent_;
double v;
internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
internal bool add_() {v+= parent_.top.result;parent_.top.result=0;return true;}
internal bool sub_() {v-= parent_.top.result;parent_.top.result=0;return true;}
internal bool store_(){parent_.top.result= v; return true;}
} :
Product save_
('+' S Product add_
/'-' S Product sub_)* store_ ;
Product
{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
internal _Product(calc0_direct parent){parent_=parent;}
calc0_direct parent_;
double v;
internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
internal bool mul_(){v*= parent_.top.result;parent_.top.result=0; return true;}
internal bool div_(){v/= parent_.top.result;parent_.top.result=0;return true;}
internal bool store_(){parent_.top.result= v;return true;}
} :
Value save_
('*' S Value mul_
/'/' S Value div_)* store_ ;
Value: Number S / '(' S Sum ')' S ;
Number
{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
internal _Number(calc0_direct parent){parent_=parent;}
calc0_direct parent_;
internal string sNumber;
internal bool store_(){double.TryParse(sNumber,out parent_.top.result);return true;}
}
: ([0-9]+ ('.' [0-9]+)?):sNumber store_ ;
S: [ \n\r\t\v]* ;
<</Grammar>>
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.