Showing posts with label WHILE. Show all posts
Showing posts with label WHILE. 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>

Parser for Switch Statements with If-then and While Statements inside - Compiler Design - Yacc Program

Program:

// Lex file: pars.l


alpha [a-zA-Z]
digit [0-9]

%%

[ \n\t]
if                  return IF;
then             return THEN;
while           return WHILE;
switch         return SWITCH;
case             return CASE;
default         return DEFAULT;
break           return BREAK;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="            return LE;
">="            return GE;
"=="            return EQ;
"!="             return NE;
"&&"           return AND;
"||"               return OR;
.                   return yytext[0];

%%

// Yacc file: pars.y

%{
#include<stdio.h>
#include<stdlib.h>
%}

%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE AND OR IF THEN WHILE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'

%%

S    :    ST{printf("\nInput accepted.\n");exit(0);};
      ;
ST  :    SWITCH'('ID')''{'B'}'
      ;
B    :    C
      |    C D
      ;
C  :    C C
    |    CASE NUM':'ST1 BREAK';'
    ;
D  :    DEFAULT':'ST1 BREAK';'
    |    DEFAULT':'ST1
    ;
ST1    :    WHILE'('E2')' E';'
    |    IF'('E2')'THEN E';'
    |    ST1 ST1
    |    E';'
    ;
E2    :    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    ;
E    :    ID'='E
    |    E'+'E
    |    E'-'E
    |    E'*'E
    |    E'/'E
    |    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    |    ID
    |    NUM
    ;

%%

#include"lex.yy.c"

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

Output:

nn@linuxmint ~ $ lex pars.l
nn@linuxmint ~ $ yacc pars.y
conflicts: 5 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out

Enter the expression: switch(s)
{
case 1:a=b+c;break;
case 2:    if(a<10)
    then a=b*c;
    break;
case 3:    while(a<5)
    b=b+a;
    break;
}

Input accepted.
nn@linuxmint ~ $

Intermediate Code Generator for While - Yacc Program - Compiler Lab

Program:

(Lex Program: intwh.l)


ALPHA [A-Za-z]
DIGIT [0-9]
%%
while                return WHILE;
{ALPHA}({ALPHA}|{DIGIT})*    return ID;
{DIGIT}+             {yylval=atoi(yytext); return NUM;}
[ \t]                 ;
\n                yyterminate();
.                 return yytext[0];
%%


(Yacc Program: intwh.y) 



%token ID NUM WHILE
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S : WHILE{lab1();} '(' E ')'{lab2();} E ';'{lab3();}
  ;
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 top=0;
char i_[2]="0";
char temp[2]="t";

int lnum=1;
int start=1;
main()
 {
 printf("Enter the expression : ");
 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",lnum++);
}


lab2()
{
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = not %s\n",temp,st[top]);
 printf("if %s goto L%d\n",temp,lnum);
 i_[0]++;
 }

lab3()
{
printf("goto L%d \n",start);
printf("L%d: \n",lnum);
}



Output :
nn@linuxmint ~ $ lex intwh.l
nn@linuxmint ~ $ yacc intwh.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxm./a.out
Enter the expression : while(k=c/s)k=k*c+d;
L1:
t0 = c / s
k = t0
t1 = not k
if t1 goto L0
t2 = k * c
t3 = t2 + d
k = t3
goto L1
L0:
nn@linuxmint ~ $

Parser for WHILE Statements - YACC Program - Compiler Design



Program:

(wh.l)

alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
while    return WHILE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(wh.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM WHILE LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S        : ST1 {printf("Input accepted.\n");exit(0);};
ST1    :    WHILE'(' E2 ')' '{' ST '}'
ST      :     ST ST
          | E';'
          ;
E       : ID'='E
          | E'+'E
          | E'-'E
          | E'*'E
          | E'/'E
          | E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;
E2     : E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;

%%

#include "lex.yy.c"

main()
{
   printf("Enter the exp: ");
   yyparse();
}


Output:

students@ccflab-desktop:~$ lex wh.l
students@ccflab-desktop:~$ yacc wh.y
conflicts: 2 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp: while(a>1){ b=1;}
Input accepted.
Related Posts Plugin for WordPress, Blogger...