Skip to content

Instantly share code, notes, and snippets.

@axgle
Created October 21, 2018 11:32
Show Gist options
  • Save axgle/9a382001982ab6133f5f4ebc5382d59f to your computer and use it in GitHub Desktop.
Save axgle/9a382001982ab6133f5f4ebc5382d59f to your computer and use it in GitHub Desktop.
parle lex parse test
<?php
use Parle\{Lexer,Parser,Token,Stack};
$p = new Parser();
/***
a=1;
b=2;
...
sum = 3
*/
$p->token("W N F '='");
$p->push("start","exps");
$f['es']=$p->push("exps","exps exp");
$p->push("exps","exp");
$f['e']=$p->push("exp","W '=' N F");
$p->build();
//exit;
$lex = new Lexer();
$lex->push("[0-9]+",$p->tokenId('N'));
$lex->push("[a-z]+",$p->tokenId('W'));
$lex->push("[;]",$p->tokenId('F'));
$lex->push("[=]",$p->tokenId("'='"));
$lex->push("[ \t\n]",Token::SKIP);
$lex->build();
$p->dump();
$in = "a=1;b=2;c=3;d=4;";
if(!$p->validate($in,$lex)){
print_r
(
$p->errorinfo()
)
;
}
$p->consume($in,$lex);
$s = new Stack();
do{
if($p->action==2){
// print_r($p->sigil());
if($p->reduceId==$f['e']){
$s->push($p->sigil(2));
}
if($p->reduceId==$f['es']){
// $s->push($p->sigil(2));
print_r([
$s,$p->sigil(1)
]);
$top = $s->top;
$s->pop();
$s->top += $top;
}
}
$p->advance();
}while( !in_array($p->action,[0,4]));
print_r($s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment