Showing posts with label For Loop. Show all posts
Showing posts with label For Loop. Show all posts

PHP Examples - Loops - While, Do While, For, Foreach

PHP Examples - Loops 
 While, Do While,  For, Foreach
Program:

<html >
<head>

<title>PHP Example 3 : Loops</title>
</head>

<body>
<?php
 
 echo "PHP Example 3";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"LOOPS";
 echo"</h1>";
 echo"<br/><li>";
 echo"WHILE LOOP: PRIME NUMBERS: ";
 echo"<br/>";
 $i=2;
 while($i<50)
 {
  $flag=true;
  $j=2;
  while($j<($i/2))
   if($i%$j==0)
   {
    $flag=false;
    break;
   }
   else
    $j++;
  if($flag)
  echo $i.' ';
  $i++;
 }
 echo"<br/></li>";

 echo"<br/><li>";

 echo"FOR LOOP: FIBONACCI NUMBERS";
 echo"<br/>";
 $t1=1;
 $t2=1;
 echo $t1.' '.$t2;
 for($i=1;$i<10;$i++)
 {
  $t3=$t1+$t2;
  $t1=$t2;
  $t2=$t3;
  echo ' '.$t3;
 }
 
 echo"<br/></li>";
  
  
 echo"<br/><li>";
 echo"DO WHILE LOOP: NATURAL NUMBERS ";
 echo"<br/>";
 $i=1;
 do{
  echo " $i";
  $i++;
 }while($i<=10);
 echo"<br/></li>";
 
 echo"<br/><li>";
 echo"FOR EACH LOOP: STUDENTS & MARKS "; 
 echo"<br/>";
 
   $mark["Mithun"]= "40";
   $mark["Shibin"]= "38";
   $mark["Manzoor"]= "35";
   $mark["Suhail"]= "45";
   $mark["Jishnu"]="36";
 
   foreach( $mark as $key => $value)
   {
    echo "Name: $key, Marks: $value <br/>"; 
   }
 echo"<br/></li>"; 

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

Intermediate Code Generator for For Loop - Compiler Design - Yacc Program

Program:

// Lex file: im4.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%

// Yacc file: im4.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%

S       : FOR '(' E ';'{lab1();} E {lab2();}';' E {lab3();}')' E';'{lab4(); exit(0);}
         ;
E       : V '='{push();} E{codegen_assign();}
         | E '+'{push();} E{codegen();}
         | E '-'{push();} E{codegen();}
         | E '*'{push();} E{codegen();}
         | E '/'{push();} E{codegen();}
         | '(' E ')'
         | '-'{push();} E{codegen_umin();} %prec UMINUS
         | V
         | NUM{push();}
         ;
V       : ID {push();}
         ;

%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int label[20];
int top=0;
char i_[2]="0";
char temp[2]="t";

int lno=0,ltop=0;
int start=1;

main()
{
    printf("Enter the expression:\n");
    yyparse();
}

push()
{
   strcpy(st[++top],yytext);
}

codegen()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
    top-=2;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_umin()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = -%s\n",temp,st[top]);
    top--;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_assign()
{
    printf("%s = %s\n",st[top-2],st[top]);
    top-=2;
}
lab1()
{
    printf("L%d: \n",lno++);
}
lab2()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = not %s\n",temp,st[top]);
    printf("if %s goto L%d\n",temp,lno);
    i_[0]++;
    label[++ltop]=lno;
    lno++;
    printf("goto L%d\n",lno);
    label[++ltop]=lno;
    printf("L%d: \n",++lno);
 }
lab3()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",start);
    printf("L%d: \n",x);
   
}

lab4()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",lno);   
    printf("L%d: \n",x);
}

Output:
nn@linuxmint ~ $ lex im4.l
nn@linuxmint ~ $ yacc im4.y
conflicts: 4 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i=b;i=i+1) a=a+b;
i = 0
L0:
i = b
t0 = not i
if t0 goto L1
goto L2
L3:
t1 = i + 1
i = t1
goto L0
L2:
t2 = a + b
a = t2
goto L3
L1:
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...