D Template based paser generator in 137 loc
    BCS 
    BCS at pathlink.com
       
    Thu Jan  4 10:54:00 PST 2007
    
    
  
This is a totaly template based system. No external utilities are 
needed. (* the 137 loc is after removal of all unneeded line)
Here is a slightly compressed use case:
struct set
{
/******* action code ********/
static PObject Action(char[] string : "MULTIPLY")(PObject[3] set)
    {...}
static PObject Action(char[] string : "SUM"     )(PObject[3] set)
    {...}
static PObject Action(char[] string : "PASS"    )(PObject[1] set)
    {...}
/******** Terminal code *********/
static PObject Terminal(char[] name : "NUM")(IParser p)  {...}
static PObject Terminal(char[] name : "PLUS")(IParser p) {...}
static PObject Terminal(char[] name : "TIMES")(IParser p){...}
// "ADD" indecates the root rule
mixin Parser!("ADD",
     "PRIMARY:PASS/NUM;
      MUL:MULTIPLY/PRIMARY TIMES MUL|PASS/PRIMARY;
      ADD:SUM/MUL PLUS ADD|PASS/MUL;");
}
void main()
{
    auto a = new ExpGrammer;
    a.data = "1 + 3 * 4 + 5 * 5 ";
    Value v = cast(Value)set.Parser(a);
    assert(v !is null);
    writef("%d\n", v.value);
}
full example here:
http://www.webpages.uidaho.edu/~shro8822/exp_grammer.d
full code here:
http://www.webpages.uidaho.edu/~shro8822/dparse.d
    
    
More information about the Digitalmars-d-announce
mailing list