I'm trying to create a parser for a simple language with the lemon port to PHP, and it works, almost.
The following grammar:
%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); }
should parse inputs like:
- 'if $cond1,!$cond2,$cond3() $var = $this($a(),$inst1 = $b())'
- '$var'
- '%rulename $var'
While inputs like 1 and 2 work as expected, somehow, not understandable for me how, input 3 returns string(8) "rulename". I have no idea which reductions should take place for that to happen. Is it something wrong with the grammar, or shall I start debugging PHP_ParserGenerator itself?
Thanks