C code Validator

An online C code validator

Page 1 of 1

1 Replies - 1246 Views - Last Post: 20 January 2010 - 03:28 PM

#1 casingh  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 19-January 09

C code Validator

Posted 20 January 2010 - 01:52 PM

I have an assignment to create an online C code validator, I have done the following so far.
So far i have attempted to use regular expressions to do the validation. Does anyone have any idea how to go about this? Is this the right path?
<html>
	<head></head>
	<body>
		<form id="codeEntry" method="post">

			<input type="submit" value="validate"></input>
			<br />
			<textarea rows="40" cols="100" name="txtCode"></textarea>
			<br />
			<input type="submit" value="validate"></input>
			<br />
			
			<?php
				$keywords = array("int","float", "char", "long", "void");
				
				//array of preprocessor and their regular expressions
				$prepro = array("define", "include", "ifdef", "ifndef");
				$preprof = array("/\A(?: |#define){1,2}[\t ]{1,2}[_A-Z]{3,10}(?: (?:	  )?)[0-9]+\s*$/",
				'/\A#include(?: (?:  )?)<[\".a-z]+>\s*$/');
			
				//get input code
				$input = $_POST["txtCode"];

				//attempting to ignore comments
				$regex = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/";
				$input = preg_replace($regex , "", $input);
				
				
				//check for extra brackets
				checkBrackets($input);
				
				//get rid of extra backslash
				$input = str_replace(chr(92) . chr(34), chr(34), $input);
				
				//get each line separate in a string array
				$lines = explode(chr(13) . chr(10), $input);
				
				
				//init line count index
				$i = 1;
				
				/*-----------debug------------*/
				//echo "<textarea rows=\"80\" cols=\"150\" name=\"txt\" >" . $input . "</textarea>";
				//printf("line, length, last, string");
				/*---------------------------*/
				echo sizeof($lines);
				echo "<table border=\"1\">";
				foreach ($lines as $s){
					$length = strlen($s);
					
					if (strlen($s) == 0){
						
					}
					
					//look for pre - processor directive
					else if (ord(substr($s, 0, 1)) == 35){
						echo "pre processor on line " . $i . "<br />";
						
						$j = 0;
						foreach ($prepro as $curpp){
							if (strpos($s, $curpp) <> false){
								//echo $curpp . " " . $j;
							//	echo $preprof[$j] . "<br />";
								if (preg_match($preprof[$j], $s)){
									echo "hello worked" . "<br />";
								
								}
								break;
							}
							//echo $j . "  " . $i . "<br />";
							
							$j++;
						}
								
						//	echo "<span style=\"background-color:#FF0000\"> $s </span>";
						
					}
					
					//error --- unknown code
					else if (preg_match("/\A[a-z]{1,6} [Sa-z]{1,7}\* [S_a-z]{1,12}\(\Z/", $s)){
						echo "function begin on line " . $i . "<br />";	
						
						
						//echo "<div style=\"color:#FF0000;\">ERROR FOUND ON LINE" . $i . "</div>";
					}
					
					/*/if line is commented ignore the line
					if (strpos($s, "//") <> false){
						echo "single line comment on line " . $i . "<br />";
					}
					
					if (strpos($s, "/*")){
						echo "multi line comment on line " . $i . "<br />";
					}*/
					
					//debug output
					printTblRow($i++, $length, $s);
				}
				echo "</table>";
				//echo ord(substr($input, 0, 5));
				//echo ord(substr($input, 1, 5));
				
				function printTblRow($i, $length, $s){
					echo "<tr>";
						//printf("<td>%d </td><td>%d </td><td> %s</td>", $i++, $length, $s);
						$print = makeHTMLPrintable($s);
						echo "<td>" . $i . "</td><td>" . $length . "</td><td>" . $print ." </td>";
					//	echo $i++ . "  " . $s . " |  length " . $length . "<br />";
					echo "</tr>";
				}
				
				function removeComments($code){
					return $code;
				}
				
				function makeHTMLPrintable($line){
					$print = str_replace("<", "&lt;", $line);
					//$print = str_replace(">", ">", $line);
					$print = str_replace(">", ">", $print);
					return $print;
				}
				
				function checkBrackets($input){
					$noOpeningBrace = substr_count($input, '(');
					$noClosingBrace = substr_count($input, ')');
					$open = array();
					
					//echo $noOpeningBrace;
					if ($noClosingBrace != $noOpeningBrace){
						echo "ERROR  BRACE MISMATCH" . "<br />";
					}
					$line = 1;
					for($i=0; $i<strlen($input); $i++){
						if ($input[$i] == chr(13)){
							$line++;
						}
						
						if (($input[$i] === "(") || ($input[$i] === "{")){
							//echo $input[$i];
							array_push($open, $input[$i]);
							//echo "Test <br />";
						}
						
						
						
						if ($input[$i] === ")"){
							//echo $input[$i] . "<br />";
//							echo "array " . count($open) . "<br />";
							$pops = array_pop($open);
							if ($pops != "("){
								if ($pops != null){
									echo "Extra $pops at line $line<br />";
								}
								else{
									echo "Mismatch ) at line $line<br />";
								}//end if else
							}//end if(pops
//							echo array_pop($open) . "<br />";
//							echo "array " . count($open) . "<br />";
						}
						
						if ($input[$i] === "}"){
							$pops = array_pop($open);
							if ($pops != "{"){
								if ($pops != null){
									echo "Extra $pops at line $line<br />";
								}
								else{
									echo "Mismatch } at line $line<br />";
								}//end if else
							}//end if(pops
						}//end if (input
					}//end for
				}//end function
			?>
		</form>
	</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: C code Validator

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: C code Validator

Posted 20 January 2010 - 03:28 PM

Umm well that's a massive assignment first off. Having the ability to check all C code methods is pretty insane. You will definitely have to use regular expressions on this one. A good utility to help you out will be

http://linux.softped...load-38508.html

That should get you started.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1