This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxAnalyzer.php
74 lines (60 loc) · 1.69 KB
/
SyntaxAnalyzer.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
72
73
74
<?php
namespace Wandu\Compiler;
use Closure;
use Wandu\Compiler\Exception\UnknownTokenException;
use Wandu\Compiler\Exception\UnknownTokenFromLexException;
/**
* Use LALR Parsing Table.
*/
class SyntaxAnalyzer
{
/** @var \Wandu\Compiler\LexicalAnalyzer */
protected $lexer;
protected $tokens = [];
protected $syntaxes;
/**
* @param \Wandu\Compiler\LexicalAnalyzer $lexer
*/
public function __construct(LexicalAnalyzer $lexer)
{
$this->lexer = $lexer;
}
public function setTokens(array $tokens) // Nonterminal
{
$this->tokens = array_unique_union($this->tokens, $tokens);
}
/**
* @param $nonTermName
* @param array $tokens
* @param \Closure $handler
* @return self
*/
public function addSyntax($nonTermName, array $tokens, Closure $handler)
{
$this->syntaxes[] = [$nonTermName, $tokens, ];//$handler];
return $this;
}
public function analyze($context)
{
// get lex tokens
$predefinedTokens = array_flip($this->tokens);
$lexTokens = array_map(function ($lexToken) use ($predefinedTokens) {
if ($lexToken instanceof Token) {
return $lexToken;
}
if (isset($predefinedTokens[$lexToken])) {
return new Token($lexToken);
}
throw new UnknownTokenFromLexException($lexToken);
}, $this->lexer->analyze($context));
// get FIRST
// print_r($this->syntaxes);
// get FOLLOW
// get LOOKAHEAD
// with null
//
$parsingTables = []; // [NO_STATES][NO_SYMBOLS + 1]
// print_r($lexTokens);
return '';
}
}