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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.