Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,839
There are 45 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Help with Lexical Analyser

Thread title: Help with Lexical Analyser
Reply    
    Thread tools Search this thread Display Modes  
05-16-2011, 11:12 AM
#1
unikorndesigns is offline unikorndesigns
unikorndesigns's Avatar
Status: Junior Member
Join date: May 2011
Location:
Expertise: Web Designing and Development
Software: Photoshop, Dreamweaver and etc
 
Posts: 41
iTrader: 0 / 0%
 

unikorndesigns is on a distinguished road

  Old  Help with Lexical Analyser

guys,
i have got a major issue with lexical analyser in PHP. am scanning the C++ program using the PHP coding. When i read from the file, i am not able to get the spaces as in....the following is my code

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int a, b, c, sum=0;
sum=a+b;
cout<<sum;
}

a ver basic program to scan and display what all stuffs are present in the c++ program. i am supposed to identify the variables, datatypes, keywords etc.

i could identify all except the identifiers i.e the keywords

i thought of scanning each character and when i encounter a space i'll go for chcking what it is. but in PHP i read the file and outputs the c++ code my program is like

#include#include#includecvoidmain(){inta,b,c,sum=0 ;sum=a+b;cout<;}

ANY HELP?? PLEASE thanks in advance

Reply With Quote
05-16-2011, 05:56 PM
#2
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

It would be a good idea to show us what you have so far. Once we can see where you are, it will be much easier to guide you forward (or sideways!).

Reply With Quote
05-17-2011, 07:31 PM
#3
unikorndesigns is offline unikorndesigns
unikorndesigns's Avatar
Status: Junior Member
Join date: May 2011
Location:
Expertise: Web Designing and Development
Software: Photoshop, Dreamweaver and etc
 
Posts: 41
iTrader: 0 / 0%
 

unikorndesigns is on a distinguished road

  Old

the following is my PHP code......may look a little messy....pls dont mind that

<?php
if(isset($_POST['submit']))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error Encountered: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$filename=$_FILES["file"]["name"];
@list($file, $extn) = split('[.]', $filename);

echo "<strong><em><u>".$extn. "</u></em></strong> file";
echo "<br>Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]);
// echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
session_start();
switch($extn)
{
case 'cpp':
include('cppfunction.php');
echo "<br>";
$counter=0;
$i=0;
$j=-1;
/* $file1 = fopen("uploads/".$filename, "r") or exit("Unable to open file!");
while(!feof($file1))
{
$program[$counter]=fgetc($file1)."\n";
$inputprogram=implode($program);
$counter++;
}
// echo "<br>".$program['30']."is in 3rd loc";
fclose($file1);*/
$file = fopen("uploads/".$filename, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
$a=fgetc($file)."\n";
$program[$counter]=$a;
$inputprogram=implode($program);
$counter++;
$_SESSION['scan']=trim($a);
$scanvar=$_SESSION['scan'];
$keywords[$i]=$_SESSION['scan'];
$keywordcheck=implode($keywords);
if(substr_count($scanvar,"#")==1||substr_count($sc anvar,"]")==1||substr_count($scanvar,"[")==1||substr_count($scanvar,"(")==1||substr_count ($scanvar,")")==1||substr_count($scanvar,"{")==1|| substr_count($scanvar,"}")==1||substr_count($scanv ar,",")==1||substr_count($scanvar,"||")==1||substr _count($scanvar,":")==1||substr_count($scanvar,"." )==1||substr_count($scanvar,"*")==1)
{
$punctuator[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
//echo "<br>$scanvar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ".substr_count($scanvar,"#");
$punctuators=implode($punctuator);
}

if(substr_count($scanvar,"==")==1||substr_count($s canvar,"!=")==1||substr_count($scanvar,">")==1||su bstr_count($scanvar,"<")==1||substr_count($scanvar ,">=")==1||substr_count($scanvar,"<=")==1)
{
$relop[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
$relops=implode($relop);
}

if(substr_count($scanvar,"+")==1||substr_count($sc anvar,"-")==1||substr_count($scanvar,"*")==1||substr_count ($scanvar,"/")==1||substr_count($scanvar,"%")==1||substr_count ($scanvar,"++")==1||substr_count($scanvar,"--")==1)
{
$arithop[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
$arithops=implode($arithop);
}

if(substr_count($scanvar,"=")==1||substr_count($sc anvar,"+=")==1||substr_count($scanvar,"-=")==1||substr_count($scanvar,"/=")==1||substr_count($scanvar,"%=")==1||substr_cou nt($scanvar,"*=")==1||substr_count($scanvar,".=")= =1)
{
$assgnmntop[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
$assgnmntops=implode($assgnmntop);
}

if(substr_count($scanvar,"&&")==1||substr_count($s canvar,"||")==1||substr_count($scanvar,"!")==1)
{
$logicop[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
$logicops=implode($logicop);
}

if(substr_count($scanvar,"\a")==1||substr_count($s canvar,"\b")==1||substr_count($scanvar,"\f")==1||s ubstr_count($scanvar,"\n")==1||substr_count($scanv ar,"\r")==1||substr_count($scanvar,"\t")==1||subst r_count($scanvar,"\v")==1||substr_count($scanvar," \\")==1||substr_count($scanvar,"\'")==1||substr_co unt($scanvar,"\"")==1||substr_count($scanvar,"\?") ==1||substr_count($scanvar,"\On")==1||substr_count ($scanvar,"\xHn")==1||substr_count($scanvar,"\0")= =1)
{
$charop[$i]=$_SESSION['scan']."&nbsp;&nbsp;|&nbsp;&nbsp;";
$charops=implode($charop);
}

if(substr_count($scanvar,"a")==1||substr_count($sc anvar,"b")==1||substr_count($scanvar,"c")==1||subs tr_count($scanvar,"d")==1||substr_count($scanvar," e")==1||substr_count($scanvar,"f")==1||substr_coun t($scanvar,"g")==1||substr_count($scanvar,"h")==1| |substr_count($scanvar,"i")==1||substr_count($scan var,"j")==1||substr_count($scanvar,"k")==1||substr _count($scanvar,"l")==1||substr_count($scanvar,"m" )==1||substr_count($scanvar,"n")==1||substr_count( $scanvar,"o")==1||substr_count($scanvar,"p")==1||s ubstr_count($scanvar,"q")==1||substr_count($scanva r,"r")==1||substr_count($scanvar,"s")==1||substr_c ount($scanvar,"t")==1||substr_count($scanvar,"u")= =1||substr_count($scanvar,"v")==1||substr_count($s canvar,"w")==1||substr_count($scanvar,"x")==1||sub str_count($scanvar,"y")==1||substr_count($scanvar, "z")==1||substr_count($scanvar,"A")==1||substr_cou nt($scanvar,"B")==1||substr_count($scanvar,"C")==1 ||substr_count($scanvar,"D")==1||substr_count($sca nvar,"E")==1||substr_count($scanvar,"F")==1||subst r_count($scanvar,"G")==1||substr_count($scanvar,"H ")==1||substr_count($scanvar,"I")==1||substr_count ($scanvar,"J")==1||substr_count($scanvar,"K")==1|| substr_count($scanvar,"L")==1||substr_count($scanv ar,"M")==1||substr_count($scanvar,"N")==1||substr_ count($scanvar,"O")==1||substr_count($scanvar,"P") ==1||substr_count($scanvar,"Q")==1||substr_count($ scanvar,"R")==1||substr_count($scanvar,"S")==1||su bstr_count($scanvar,"T")==1||substr_count($scanvar ,"U")==1||substr_count($scanvar,"V")==1||substr_co unt($scanvar,"W")==1||substr_count($scanvar,"X")== 1||substr_count($scanvar,"Y")==1||substr_count($sc anvar,"Z")==1)
{
$letter[$i]=$_SESSION['scan'];
$letters=implode($letter);
}

$i=$i+1;
// unset($_SESSION['scan']);
}
// echo $keywordcheck;
$keyword["1"]="asm";
$keyword["2"]="auto";
$keyword["3"]="break";
$keyword["4"]="case";
$keyword["5"]="catch";
$keyword["6"]="char";
$keyword["7"]="class";
$keyword["8"]="const";
$keyword["9"]="continue";
$keyword["10"]="default";
$keyword["11"]="delete";
$keyword["12"]="do";
$keyword["13"]="double";
$keyword["14"]="else";
$keyword["15"]="enum";
$keyword["16"]="extern";
$keyword["17"]="float";
$keyword["18"]="for";
$keyword["19"]="friend";
$keyword["20"]="goto";
$keyword["21"]="if";
$keyword["22"]="inline";
$keyword["23"]="integer";
$keyword["24"]="int";
$keyword["25"]="long";
$keyword["26"]="new";
$keyword["27"]="operator";
$keyword["28"]="private";
$keyword["29"]="protected";
$keyword["30"]="public";
$keyword["31"]="register";
$keyword["32"]="return";
$keyword["33"]="short";
$keyword["34"]="signed";
$keyword["35"]="sizeof";
$keyword["36"]="static";
$keyword["37"]="struct";
$keyword["38"]="switch";
$keyword["39"]="template";
$keyword["40"]="this";
$keyword["41"]="throw";
$keyword["42"]="try";
$keyword["43"]="typedof";
$keyword["44"]="union";
$keyword["45"]="unsigned";
$keyword["46"]="virtual";
$keyword["47"]="void";
$keyword["48"]="volatile";
$keyword["49"]="while";
$keyword["50"]="include";
$keyword["51"]="cin";
$keyword["52"]="cout";
$keyword["53"]="main";
for($i=1; $i<=53; $i++)
{
if(substr_count($keywordcheck,$keyword[$i])>0)
{
$keywordsused[$i]=$keyword[$i]."&nbsp;&nbsp;|&nbsp;&nbsp;";
$keywords_detected=implode($keywordsused);
}

if(substr_count($letters,$keyword[$i])>0)
{
$letters=str_replace($keyword[$i],"",$letters);
}
}
$headerfile["1"]="conioh";
$headerfile["2"]="iostreamh";
$headerfile["3"]="ostreamh";
$headerfile["4"]="istreamh";
$headerfile["5"]="fstreamh";
$headerfile["6"]="ifstreamh";
$headerfile["7"]="ofstreamh";
$headerfile["8"]="stdioh";
$headerfile["9"]="stdlibh";
$headerfile["10"]="processh";
$headerfile["11"]="stringh";
$headerfile["12"]="mathh";
for($i=1; $i<=10; $i++)
{
if(substr_count($letters,$headerfile[$i])>0)
{
$letters=str_replace($headerfile[$i],"",$letters);
}
}

// $counter-1; //the actual length of the program
$backup=$keywordcheck;
for($t=0,$d=0;$t<strlen($keywordcheck);$t++)
{
$scanvar=$backup[$t];

if(substr_count($scanvar,"a")==1||substr_count($sc anvar,"b")==1||substr_count($scanvar,"c")==1||subs tr_count($scanvar,"d")==1||substr_count($scanvar," e")==1||substr_count($scanvar,"f")==1||substr_coun t($scanvar,"g")==1||substr_count($scanvar,"h")==1| |substr_count($scanvar,"i")==1||substr_count($scan var,"j")==1||substr_count($scanvar,"k")==1||substr _count($scanvar,"l")==1||substr_count($scanvar,"m" )==1||substr_count($scanvar,"n")==1||substr_count( $scanvar,"o")==1||substr_count($scanvar,"p")==1||s ubstr_count($scanvar,"q")==1||substr_count($scanva r,"r")==1||substr_count($scanvar,"s")==1||substr_c ount($scanvar,"t")==1||substr_count($scanvar,"u")= =1||substr_count($scanvar,"v")==1||substr_count($s canvar,"w")==1||substr_count($scanvar,"x")==1||sub str_count($scanvar,"y")==1||substr_count($scanvar, "z")==1||substr_count($scanvar,"A")==1||substr_cou nt($scanvar,"B")==1||substr_count($scanvar,"C")==1 ||substr_count($scanvar,"D")==1||substr_count($sca nvar,"E")==1||substr_count($scanvar,"F")==1||subst r_count($scanvar,"G")==1||substr_count($scanvar,"H ")==1||substr_count($scanvar,"I")==1||substr_count ($scanvar,"J")==1||substr_count($scanvar,"K")==1|| substr_count($scanvar,"L")==1||substr_count($scanv ar,"M")==1||substr_count($scanvar,"N")==1||substr_ count($scanvar,"O")==1||substr_count($scanvar,"P") ==1||substr_count($scanvar,"Q")==1||substr_count($ scanvar,"R")==1||substr_count($scanvar,"S")==1||su bstr_count($scanvar,"T")==1||substr_count($scanvar ,"U")==1||substr_count($scanvar,"V")==1||substr_co unt($scanvar,"W")==1||substr_count($scanvar,"X")== 1||substr_count($scanvar,"Y")==1||substr_count($sc anvar,"Z")==1)
{
$nextloc=$t+1;
}

}

echo "<br>Program Scanned: ".@$inputprogram.
"<br><br>Punctuators Detected: ".@$punctuators.
"<br>Keywords Detected: ".@$keywords_detected.
"<br>Logical Operators Detected: ".@$logicops.
"<br>Arithmetic Operators Detected: ".@$arithops.
"<br>Assignment Operators Detected: ".@$assgnmntops.
"<br>Identifiers Detected: ".@$identifiers;
for($i=0;$i<$d;$i++)
echo $identifier[$i];
fclose($file);
echo "<br>";
break;

case 'c':
break;

case 'java':
break;

default:
echo "<span style='color: red;'><em><strong>Invalid File</strong></em></span>";
break;
};
}
}
?><br />

<strong><em>Upload the file:</em></strong>
<form action="index.php" method='post' enctype="multipart/form-data">
<input type="file" name="file" id="file" size="40"/>
<input type='submit' name="submit" value="Upload"/>
</form>

Reply With Quote
Reply    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed