Jump to content

[SOLVED] Help with code to parse a scripting language


fert

Recommended Posts

I have this code

<?php
header("Content-type: text/plain");

$vars;
$text=$_GET['text'];

$lines=explode(";",$text);
$num=count($lines);

for($count=0;$count<$num;$count++)
{
	if(preg_match("/int (.*?)\=[0-9]*/",$lines[$count],$matches))
	{
		//print_r($matches);
		$temp=$matches[0];
		$temp=explode("=",$temp);

		$vars[$matches[1]]=$temp[1];
	}
	else
	if(preg_match("/(.*?)\=(.*?)\+(.*?)/",$lines[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/(.*?)\=(.*?)\+(.*?)/","\$vars[$1]=\$vars[$2]+\$vars[$3];",$lines[$count]);
		echo $lines[$count];
		eval($lines[$count]);
	}
	else
	if(preg_match("/display_var (.*?)/",$lines[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/display_var (.*?)/","echo \$vars[$1];",$lines[$count]);

		echo $lines[$count];
		eval($lines[$count]);
	}
	else
	if(preg_match("/display (.*?)/",$line[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/display (.*?)/","echo $1;",$lines[$count]);
		echo $lines[$count];
		eval($lines[$count]);
	}
	$matches=array();
}
print_r($vars);
?>

only this block of code works

if(preg_match("/int (.*?)\=[0-9]*/",$lines[$count],$matches))
	{
		//print_r($matches);
		$temp=$matches[0];
		$temp=explode("=",$temp);

		$vars[$matches[1]]=$temp[1];
	}

this code does nothing

if(preg_match("/(.*?)\=(.*?)\+(.*?)/",$lines[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/(.*?)\=(.*?)\+(.*?)/","\$vars[$1]=\$vars[$2]+\$vars[$3];",$lines[$count]);
		echo $lines[$count];
		eval($lines[$count]);
	}
	else

this code

if(preg_match("/display_var (.*?)/",$lines[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/display_var (.*?)/","echo \$vars[$1];",$lines[$count]);

		echo $lines[$count];
		eval($lines[$count]);
	}

gives this

echo $vars[];a

Fatal error: Cannot use [] for reading in /home/content/z/z/i/zzieba/html/function.php(35) : eval()'d code on line 1

and this code gives nothing

if(preg_match("/display (.*?)/",$line[$count],$matches))
	{
		print_r($matches);
		$lines[$count]=preg_replace("/display (.*?)/","echo $1;",$lines[$count]);
		echo $lines[$count];
		eval($lines[$count]);
	}

I can't see any reasons as to why this code won't work. Any ideas?

the code that works parses code that looks like this

int a=6546;

int b=3525;

int c=325432;

 

the second code is suppose to parse code that looks like this

a=b+c;

 

the third code is suppose to parse code that looks like this

display_var a;

 

the last code is suppose to parse code that looks like this

display Text;

I cleaned up the regexes and made them a little more flexible/restrictive to whitespace. The evals had the most reworking.

 

<?php
$data = <<<DATA
int a=6546;
int b=3525;
int c=325432;
a=b+c;
display_var a;
display Text;
DATA;

header("Content-type: text/plain");

$vars;
//$text=$_GET['text'];
$text = $data;

$lines=explode(";",$text);
$num=count($lines);

for($count=0;$count<$num;$count++)
{
	// Remove leading space.
	$lines[$count] = preg_replace('/^\s+/', '', $lines[$count]);

	if(preg_match("/int\s+(\S+)=(\d+)/", $lines[$count], $matches))
	{
		$vars[$matches[1]] = $matches[2];
	}
	else
	if(preg_match("/(\S+)\s*=(\S+)\s*(\+)\s*(\S+)/", $lines[$count], $matches))
	{
		eval("\$vars[$matches[1]] = \$vars[$matches[2]] $matches[3] \$vars[$matches[4]];");
	}
	else
	if(preg_match("/display_var\s+(\S+)/", $lines[$count], $matches))
	{
		eval("echo \$vars[$matches[1]];");
	}
	else
	if(preg_match("/display\s+(\S+)/",$line[$count],$matches))
	{
		echo $matches[1];
	}
	$matches=array();
}
print_r($vars);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.