-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.php
71 lines (65 loc) · 2.07 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* Test file.
*
* PHP version 5.3
*
* @category SLR
* @package Core
* @author Krzysztof "Bushee" Nowaczyk <[email protected]>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link http://bushee.ovh.org
*/
require_once 'slr.php';
// TODO pretty classes for parser config, ruleset and rules
$parserConfig = array(
'start' => 'E',
'rules' => array(
'E' => array(
array(
array('E', '+', 'E'),
create_function('$v', 'return $v[0] + $v[2];')
),
array(
array('E', 'x', 'E'),
create_function('$v', 'return $v[0] * $v[2];')
),
array(
array('(', 'E', ')'),
create_function('$v', 'return $v[1];')
),
array(
array('NUM'),
create_function('$v', 'return $v[0];')
)
),
'NUM' => array(
array(
array('digit', 'NUM'),
create_function('$v', 'return $v[0] . $v[1];')
),
array(
array(), // TODO proper handling of empty rules
)
)
),
'success' => create_function('$v', 'echo "$v\n"; return $v;')
);
// TODO pretty classes for lexer config and rules
// TODO operator precedence routines
$lexerConfig = array(
'initial' => array(
array('string', '+', create_function('&$value', 'return \'+\';')),
array('string', '*', create_function('&$value', 'return \'x\';')),
array('string', '(', create_function('&$value', 'return \'(\';')),
array('string', ')', create_function('&$value', 'return \')\';')),
array('regex', '/[0-9]/', create_function('&$value', 'return \'digit\';'))
)
);
$string = '3*(5+2)+13*2+1';
$lexer = new SLR\Lexer\Lexer($lexerConfig);
$slr = new SLR\Parser\SLRTable($parserConfig);
$parser = new SLR\Parser\Parser($slr);
$tokens = $lexer->lex($string);
$lexerCaretPosition = $lexer->getCaretPosition();
$parser->parse($tokens, $lexerCaretPosition['row']);