-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.class.php
More file actions
178 lines (154 loc) · 5.1 KB
/
Parser.class.php
File metadata and controls
178 lines (154 loc) · 5.1 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* @author Alexander Bresk
* @link http://www.cip-labs.net/projects/ipsum/
* @copyright cip-labs.net 2010-2012
* @package ipsum
* @version 1.0.0 (november 2011)
* @name Parser.class.php
*/
require_once 'Morphem.class.php';
require_once 'Lexer.class.php';
class Parser {
/**
*
* @var Lexer
*/
var $_lexer;
/**
*
* @var Morphem
*/
var $_morphem;
/**
* @method Parser
* @desc constructor
* @param $value - expression
*/
function Parser($value){
$this->_lexer = new Lexer($value);
$this->_lexer->init();
}
/**
* @method run
* @desc processing method
* @return float - result
*/
function run(){
if(!$this->_lexer->startupCheck())
$this->error("run()", 1, "number of brackets are not correct");
return $this->expression();
}
/**
* @method addFunction
* @param string $name
* @param string $function
* @desc adds the function $function to the symbol table with the
* synonym $name
*/
function addFunction($name, $function){
Lexer::$_userFunctions[$name] = $function;
}
/**
* @method expression
* @return float
* @desc realizes the E rule in the grammar
*/
function expression(){
$tmp = 0;
$this->_lexer->getNext();
$this->_morphem = $this->_lexer->getMorphem();
$tmp = $this->term();
//because of -1-1 rule
$this->_lexer->getNext(CVAL);
$this->_morphem = $this->_lexer->getMorphem();
if($this->_morphem->getCode() == CVAL && $this->_morphem->getValue() == '+'){
$this->_morphem->setEaten(true);
$tmp += $this->expression();
}else if($this->_morphem->getCode() == CVAL && $this->_morphem->getValue() == '-'){
$this->_morphem->setEaten(true);
$tmp -= $this->expression();
}
return $tmp;
}
/**
* @method term
* @return float
* @desc realizes the T rule in the grammar
*/
function term(){
$tmp = 1;
$this->_lexer->getNext();
$this->_morphem = $this->_lexer->getMorphem();
$tmp = $this->factor();
$this->_lexer->getNext(CVAL);
$this->_morphem = $this->_lexer->getMorphem();
if($this->_morphem->getCode() == CVAL && $this->_morphem->getValue() == '*'){
$this->_morphem->setEaten(true);
$tmp *= $this->term();
}elseif($this->_morphem->getCode() == CVAL && $this->_morphem->getValue() == '/'){
$this->_morphem->setEaten(true);
if(($zero = $this->term()) != 0) $tmp /= $zero;
else $this->error('term()', 1, 'division by zero: ' . $tmp . '/' . $zero . '!');
}
return $tmp;
}
/**
* @method factor
* @return float
* @desc realizes the F rule in the grammar
*/
function factor(){
$tmp = 0;
//for all FVALS
if($this->_morphem->getCode() == FVAL){
if(array_key_exists($this->_morphem->getValue(), Lexer::$_userFunctions)){
$funcMorphem = clone $this->_morphem;
$this->_morphem->setEaten(true);
$this->_lexer->getNext();
$this->_morphem = $this->_lexer->getMorphem();
//just look ahead -> dont eat!
if($this->_morphem->getCode() == CVAL && $this->_morphem->getValue() == '('){
$funcParser = new Parser(($expression = $this->_lexer->getExpressionForFunction()));
$func = Lexer::$_userFunctions[$funcMorphem->getValue()];
$tmp = $func($funcParser->run());
$this->_lexer->getNext();
$this->_morphem = $this->_lexer->getMorphem();
$this->_morphem->setEaten(true);
return $tmp;
}
//function name isnt defined
}else
$this->error('factor()', 1, 'function' . $this->_morphem->getValue() . 'is not defined!');
}
//for all CVALS
if($this->_morphem->getCode() == CVAL){
if($this->_morphem->getValue() == '('){
$this->_morphem->setEaten(true);
//find term
$tmp = $this->expression();
$this->_lexer->getNext();
$this->_morphem = $this->_lexer->getMorphem();
$this->_morphem->setEaten(true);
}
//for all DVALS
}else{
if($this->_morphem->getCode() == DVAL){
$this->_morphem->setEaten(true);
$tmp = $this->_morphem->getValue();
}else $this->error("factor()", 1, "is not DVAL!");
}
return $tmp;
}
/**
* @method error
* @param string $func
* @param int $error_code
* @param string $error_text
*/
function error($func, $error_code, $error_text){
echo PHP_EOL , "[" , $func , "]: " , "(code: " , $error_code , "), " , $error_text , PHP_EOL;
if($error_code != 0) exit($error_code);
}
}
?>