Created
October 21, 2018 11:32
-
-
Save axgle/9a382001982ab6133f5f4ebc5382d59f to your computer and use it in GitHub Desktop.
parle lex parse test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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