3 class LalrHTMLVisualizationGenerator
{
11 public function __construct( $parser_name, $fsm, $grammar ) {
12 $this->name
= $parser_name;
13 $this->filename
= $parser_name . "Visualization";
14 $this->grammar
= $grammar;
18 $this->goto_map
= array();
19 foreach( $this->fsm
->get_statetable() as $state_id => $map ) {
20 foreach( $map as $symbol => $action_arr ) {
21 list( $action, $target ) = explode(':',$action_arr,2);
22 if( $action == 'goto' ) {
23 if( !isset( $this->goto_map
[$symbol] ) )
24 $this->goto_map
[$symbol] = array();
25 $this->goto_map
[$symbol][$state_id] = $target;
31 public function generate() {
32 if( !is_dir( $this->dir
) && !mkdir( $this->dir
, 0755, true ) )
33 throw new GeneratorException( "Could not create dir $class_dir" );
35 $this->fp
= fopen( $this->dir
. DIRECTORY_SEPARATOR
. $this->filename
.'.html', 'w' );
37 ob_start( array( $this, 'write_block'), 1024 );
44 public function write_block( $block ) {
45 fwrite( $this->fp
, $block );
48 private function build_html() {
53 <title
>Visualization of parser
<?php
echo htmlentities($this->name
);?
></title
>
54 <style type
="text/css">
60 border
-right
: 1px solid
#bbbbbb;
68 border
-top
: 3px solid black
;
81 .inner_table td
, .inner_table th
{
86 background
-color
: #dddddd;
90 text
-decoration
: underline
;
94 background
-color
: #dddddd;
100 <h1
>Visualization of parser
<?php
echo htmlentities($this->name
);?
></h1
>
102 <tr
><td style
="width: 50%;">
103 <h2
>Lexical analysis
</h2
>
105 <?php
foreach( $this->grammar
->get_tokens() as $token => $match ): ?
>
107 <th
><?php
echo htmlentities( $token ); ?
></th
>
108 <td
><?php
echo htmlentities($match); ?
></td
>
112 </td
><td style
="width: 50%;">
115 <?php
foreach( $this->grammar
->get_rules() as $item ):?
>
117 <th
><?php
echo htmlentities($item->get_name());?
></th
>
118 <td
class="target"><?php
echo htmlentities($item->generates());?
></td
>
120 <?php
foreach( $item->get_symbols() as $i=>$sym ): ?
>
121 <td
><?php
echo $sym; ?
></td
>
129 <h2
>LR Parser table
</h2
>
130 <table
class="visible" cellspacing
="0">
132 <th colspan
="2">State
</th
>
133 <th
class="bar"></th
>
134 <th
>Error handler
</th
>
135 <th
class="bar"></th
>
136 <?php
foreach( $this->grammar
->terminals() as $sym ): if($sym[0]=='_') continue;?
>
137 <th
><?php
echo htmlentities($sym); ?
></th
>
139 <th
class="bar"></th
>
140 <?php
foreach( $this->grammar
->non_terminals() as $sym ): ?
>
141 <th
><?php
echo htmlentities($sym); ?
></th
>
145 <?php
foreach( $this->fsm
->get_statetable() as $state_id => $map ):?
>
147 <th
class="hard_top bordered"><?php
echo htmlentities($state_id); ?
></th
>
148 <td
class="inner_table hard_top bordered">
150 <?php
foreach( $this->fsm
->get_state($state_id)->closure() as $item ):?
>
152 <th
><?php
echo htmlentities($item->get_name());?
></th
>
153 <td
class="target"><?php
echo htmlentities($item->generates());?
></td
>
155 <?php
foreach( $item->get_symbols() as $i=>$sym ): ?
>
156 <td
<?php
if( $item->get_ptr() == $i ) echo ' class="mark"';?
>><?php
echo $sym; ?
></td
>
158 <?php
if( $item->complete() ):?
><td
class="mark"> 
;</td
><?php
endif;?
>
163 <td
class="bar hard_top bordered"></td
>
164 <td
class="hard_top bordered"><?php
echo $this->fsm
->get_default_error_handler($state_id); ?
></td
>
165 <td
class="bar hard_top bordered"></td
>
166 <?php
foreach( $this->grammar
->terminals() as $sym ): if($sym[0]=='_') continue; ?
>
167 <td
class="hard_top bordered"><?php
168 if( isset( $map[$sym] ) ) {
169 list($action, $target) = explode(':',$map[$sym],2);
170 print $action.'<br/>'.$target;
174 <td
class="bar hard_top bordered"></td
>
175 <?php
foreach( $this->grammar
->non_terminals() as $sym ): ?
>
176 <td
class="hard_top bordered"><?php
177 if( isset( $map[$sym] ) ) {
178 list($action, $target) = explode(':',$map[$sym],2);
179 print $action.'<br/>'.$target;