演算子順位構文解析

ほい、できた。

using gcc;

var opparser_table = [ 
		      [[`TOK_MULT:MULT_EXPR],[`TOK_DIV:CEIL_DIV_EXPR]],
		      [[`TOK_PLUS:PLUS_EXPR],[`TOK_MINUS:MINUS_EXPR]]
];
var parser = Parser.create_oppre_parser( opparser_table );

var lextable = [
		["[0-9]+":`TOK_DIGIT],
	        ["\\+":`TOK_PLUS],
		["-":`TOK_MINUS],
		["\\*":`TOK_MULT],
		["/":`TOK_DIV],
		["[ \t]":null],
		[ null:`EOF]
];
var lexer = Lex.generate_analyzer( lextable );
var queue = Lex.new_lex_queue( lexer, file.open_rstream("expr") );

var counter = 0;

function term_func( lqueue ) {
  var tok = lqueue.get();

  counter += 1;

  if ( tok == `TOK_DIGIT ) 
    return counter;		// ほんとはちゃんと数値を返さないと
  else
    return 2;			// 超適当
}


var ctxt = parser.new_context();

var tree = parser.parse( ctxt, queue, term_func );
debug_tree( tree );

これで、

1 * 2 + 3 * 4 - 5 * 6

これを食わせると

 <minus_expr 0x404f9840
    arg 0 <plus_expr 0x404f94f8
        arg 0 <mult_expr 0x404f9138
            arg 0 <integer_cst 0x404f803c constant 1>
            arg 1 <integer_cst 0x404f8258 constant 2>>
        arg 1 <mult_expr 0x404f94e0
            arg 0 <integer_cst 0x404f862c constant 3>
            arg 1 <integer_cst 0x404f8848 constant 4>>>
    arg 1 <mult_expr 0x404f9828
        arg 0 <integer_cst 0x404f8cbc constant 5>
        arg 1 <integer_cst 0x404f8ed8 constant 6>>>

こうなる。
演算子順位(operater precedence)をoppreと略すのは大丈夫なのか?


そろそろ、気持ち悪いバグとか、関数間でつじつまが合わないところとか出てき始めたので、これまで適当に作ってきた分のツケがまわってくるのかもしれないなぁ…