-
Notifications
You must be signed in to change notification settings - Fork 0
/
inToPost.php
99 lines (85 loc) · 1.78 KB
/
inToPost.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
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
<?php
$infix="";
if(isset($_GET["infix"]))
$infix=$_GET["infix"];
$stack=new SplStack();
$infix_arr=str_split($infix);
$postfix=array();
$operators=array(
"("=>2,
"."=>0,
"+"=>1,
")"=>3,
"*"=>1,
"|"=>0
);
//print_r($infix_arr);
//print_r($operators);
$temp;
//$infix_temp=array();
foreach($infix_arr as $token){
if(isOperator($token)){
$prec=checkPrecedence($token);
if(count($stack)==0&&$prec==3)
echo "Invalid input string";
else{
if($prec==3){
echo "here";
while(count($stack)>0){
$temp=$stack->pop();
//echo "<br>Temp:".$temp;
if($temp!="(")
array_push($postfix,$temp);
else
break;
}
}
else
$stack->push($token);
}
}
else{
array_push($postfix,$token);
}
}
while(count($stack)>0){
array_push($postfix,$stack->pop());
}
$stack->rewind();
//echo "Stack contents"."<br>";
while($stack->valid()){
echo $stack->current(),PHP_EOL;
$stack->next();
}
echo "<br>Postfix String<br>";
print_r($postfix);
function isOperator($char){
return ($char=="("||$char=="*"||$char=="."||$char=="+"||$char==")"||$char=="|");
}
function checkPrecedence($char){
global $operators;
return $operators[$char];
}
function postNFA($postfix){
$graph=new Structures_Graph();
$arr=array();
for($i=0;$i<strlen($postfix);$i++){
switch ($postfix[$i]) {
case '':
break;
default:
array_push($arr,$postfix[$i]);
}
}
}
?>
<html>
<head>
<title>Thompson NFA</title>
</head>
<body>
<form action="inToPost.php" method="GET">
<input type="text" placeholder="Enter string to create NFA" name="infix">
</form>
</body>
</html>