我正在尝试使用lemon port to PHP为简单语言创建解析器,它几乎可以正常工作。
以下语法:
%name SP_
%declare_class { class SpecParser }
%token_prefix SP_
%include_class {
public $retvalue = '<todo: error handling>';
public function singleKey($elem) {
end($elem);
return key($elem);
}
}
%parse_accept {
$this->retvalue = $this->_retvalue;
}
%right ASSIGN.
start(A) ::= spec(B) . { A = B; }
spec(A) ::= top_stmt(B) . { A = B; }
top_stmt(A) ::= . { A=array('empty' => NULL); }
top_stmt(A) ::= conditional(B) . { A = B;}
top_stmt(A) ::= retval(B) . { A = B; }
top_stmt(A) ::= assignment(B) . { A = array('assignment' => B); }
top_stmt(A) ::= MOD STRING(B) top_stmt(C) . {C['rulename'] = B; A=B;}
conditional(A) ::= IF stmt_list(B) . { A = array('condbreak' => array('stmt_list' => B)); }
conditional(A) ::= IF stmt_list(B) stmt(C) . { A = array('condexec' => array('cond' => B,'exec' => C)); }
retval(A) ::= access(B) . { A = array('access' => B); }
retval(A) ::= invoke(B) . { A = array('invoke' => B); }
assignment(A) ::= access(B) ASSIGN expr(C) . {A = array('lval' => B, 'rval' => C);}
expr(A) ::= retval(B) . { A = array('retval' => B); }
expr(A) ::= NOT retval(B) . { A = array('!retval' => B); }
stmt(A) ::= expr(B) . { A = array('expr' => B); }
stmt(A) ::= assignment(B) . { A = array('assignment' => B); }
empty_stmt_list(A) ::= . {A = array();}
empty_stmt_list(A) ::= stmt(B) . {
A = array(B);
}
empty_stmt_list(A) ::= empty_stmt_list(B) COMMA stmt(C) . {
B[] = C;
A = B;
}
stmt_list(A) ::= stmt_list(B) COMMA stmt(C) . {B[] = C; A=B; }
stmt_list(A) ::= stmt(B) . { A = array(B); }
invoke(A) ::= access(B) LPAREN empty_stmt_list(C) RPAREN . { A = array('target' => B,'args' => C); }
access(A) ::= VARIABLE(B) . { A = array('variable' => B); }
应解析输入,如:
虽然像1和2这样的输入按预期工作,不知何故,对我来说不可理解输入3返回字符串(8)“rulename”。我不知道应该采取哪种减少措施。语法有问题,还是我应该开始调试PHP_ParserGenerator本身?
由于
答案 0 :(得分:1)
我一定很累。错误是错误的任务:
top_stmt(A) ::= MOD STRING(B) top_stmt(C) . {C['rulename'] = B; A=B;}
应该是:
top_stmt(A) ::= MOD STRING(B) top_stmt(C) . {C['rulename'] = B; A=C;}