Professional Documents
Culture Documents
Compiler Design
Compiler Design
--- group2
K.MohananjaliAP19110010182
J.ManognaAP19110010153
N.RajyalakshmiAP19110010301
V.AnirudhAP19110010175
T.DharmeshAP19110010183
Report
DATATYPES IN PHP:
PHP supports the following data types:
String:
Integer:
An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base
8), or binary (base 2) notation
Example:
<?php
$x = 5985;
var_dump($x);
?>
Output:
int(5985)
Float:
A float (floating-point number) is a number with a decimal point or a number in exponential form.
In the following example $x is a float. The PHP var_dump() function returns the data type and value:
Example:
<?php
$x = 10.365;
var_dump($x);
?>
output:float(10.365)
Boolean:
A Boolean represents two possible states: TRUE or FALSE.
Example:
$x = true;
$y = false;
Array:
An array stores multiple values in one single variable. In the following example
$cars is an array.
Example:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
Object:
Classes and objects are the two main aspects of object-oriented programming. A
class is a template for objects, and an object is an instance of a class.
Example:
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
NULL:
Null is a special data type that can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Example:
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?
Resource:
The special resource type is not an actual data type. It is the storing of a reference
to functions and resources external to PHP. A common example of using the
resource data type is a database call.
Decision-Making Statements:
PHP conditional statements
allow you to make a decision, based upon the result of a condition. These
statements are called Decision Making Statements.
Conditional Statements in PHP are:
● If statements
● If else statements
● If elseif else statement
● Switch statement
SYNTAX:
if (condition){
// if TRUE then execute this code
}
Example:
<?php
$x = 12;
if ($x > 0) {
?>
Output:
SYNTAX:
if (condition) {
// if TRUE then execute this code
}
elseif {
// if TRUE then execute this code
}
elseif {
// if TRUE then execute this code
}
else {
// if FALSE then execute this code
}
Example:
<?php
$x = "August";
if ($x == "January") {
Else{
}
?>
OUTPUT:
SWITCH STATEMENT: The “switch” performs in various cases i.e., it has various
cases to which it matches the condition and appropriately executes a particular case
block. It first evaluates an expression and then compares it with the values of each case.
If a case matches then the same case is executed. To use the switch, we need to get
familiar with two different keywords namely, break and default.
● The break statement is used to stop the automatic control flow into the next cases
and exit from the switch case.
● The default statement contains the code that would execute if none of the cases
match.
SYNTAX:
switch(n) {
case statement1:
code to be executed if n==statement1;
break;
case statement2:
code to be executed if n==statement2;
break;
case statement3:
code to be executed if n==statement3;
break;
case statement4:
code to be executed if n==statement4;
break;
......
default:
code to be executed if n != any case;
EXAMPLE:
<?php
$n = "February";
switch($n) {
case "January":
echo "Its January";
break;
case "February":
echo "Its February";
break;
case "March":
echo "Its March";
break;
case "April":
echo "Its April";
break;
case "May":
echo "Its May";
break;
case "June":
echo "Its June";
break;
case "July":
echo "Its July";
break;
case "August":
echo "Its August";
break;
case "September":
echo "Its September";
break;
case "October":
echo "Its October";
break;
case "November":
echo "Its November";
break;
case "December":
echo "Its December";
break;
default:
echo "Doesn't exist";
}
?>
OUTPUT:
Its February
if…else Statement: We understood that if a condition will hold i.e., TRUE, then the
block of code within if will be executed. But what if the condition is not TRUE and we
want to perform an action? This is where else comes into play. If a condition is TRUE
then if block gets executed, otherwise else block gets executed.
SYNTAX:
if (condition) {
// if TRUE then execute this code
}
else{
// if FALSE then execute this code
}
EXAMPLE:
else{<?php
$x = -12;
if ($x > 0) {
echo "The number is positive";
}
?>
echo "The number is negative";
}
OUTPUT:
The number is negative
For Loop:
The for loop is used when you know in advance how many times the script
should run.
for statement has the following properties:
● The two semicolons are required and create three sections: an initialization
statement, a boolean expression, and an update statement.
● The initialization step occurs once at the beginning of the loop.
● The boolean_expression must evaluate to true or false.
● The initialization and update_statement sections can contain multiple
statements, separated by commas.
● Any variables declared in the initialization step are local variables in for
loop and go out of scope when the loop finishes.
Syntax:
for (initialization; boolean_expression; update_statement)
{
//body of the loop;
}
Example:
<?php
for($i=0;$i<=2;$i++)
{
echo "The Number is ".$i."<br/>";
}
?>
Output:
The Number is 0
The Number is 1
The Number is 2
Parse Tree:
CFG for Variable declaration:
<var-decl>→ <type><list>
<type>→ int|float
<list>→ id|id,<list>
id={a,b,...A,B,...}
Parse tree:
Semantic Actions: Variable declaration tells the compiler two things: The name
of the variable and the type of data the variable will hold
CFG for Arithmetic expression:
<exp>→ <term><operator><term>
<term>→ <factor><operator><factor>
<operator>→ +|-|*|=
<factor>→ a-z|A-Z
Parse tree:
CFG for IF-ELSE:
<statement>→ <if><exp><statement><else><statement>|<statement><statement>
<exp>→ < < >, < > >, <= = >,...
Parse tree:
Semantic analysis:
The if-else statement is used to carry out two operations in response to a single
condition. The if-else statement is an extension to the if statement that allows us to
perform two different operations, one for the correctness of the condition and the
other for its incorrectness. It is important to note that the if and else blocks cannot
be executed concurrently. It is always preferable to use an if-else statement because
it always invokes an otherwise case with every if condition..
For loop:
Structure of For Loop:
for(initialization; condition; increment/decrement)
for($n=1;$n<=10;$n++)
Keywords : for
Terminals : (, ), =, <, >, ++, --, variable i.e. i
CFG:
S → XY
X → for
Y → ( A; B; C ) (Rest of loop will be Y)
A → V = N (Initialization)” (Increment / Decrement)
N → 0-9N / [0-9]
V→ _W/ a-zW / A-ZW
W → _W/ a-zW/ 0-9W/ lambda
Parse tree:
Semantic actions:
The PHP for loop is similar to the java/C/C++ for a loop.
initialization - Initialize the loop counter value. The initial value of the for loop is
done only once. This parameter is optional.
condition - Evaluate each iteration value. The loop continuously executes until the
condition is false. If TRUE, the loop execution continues, otherwise, the execution
of the loop ends.
Increment/decrement - It increments or decrements the value of the variable.
flow chart of for loop:
Target language: Python
Syntax Of Target Language:
Python statement ends with the token NEWLINE character (carriage return). It means
each line in a Python script is a statement. The following Python script contains three
statements in three separate lines.
EXAMPLE:
print('id: ', 1)
Use backslash character / to join a statement span over multiple lines, as shown below.
EXAMPLE:
EXAMPLE:
>>> print('Hello \
World!') # a multi-line statement
Hello World!
>>> print('Hello') \
print(' World!') # two statement in one logical line
SyntaxError: invalid syntax
Expressions in parentheses (), square brackets [ ], or curly braces { } can be spread over
multiple lines without using backslashes.
EXAMPLE:
list = [1, 2, 3, 4
5, 6, 7, 8,
9, 10, 11, 12]
Indentation in Python:
The leading space or tab at the beginning of the line is considered as the indentation level
of the line, which is used to determine the group of statements. Statements with the same
level of indentation are considered as a group or block.
Indentation Rules:
● Use the colon: to start a block and press Enter.
● All the lines in a block must use the same indentation, either space or a tab.
● Python recommends four spaces as indentation to make the code more readable. Do not
mix space and tab in the same block.
● A block can have inner blocks with next-level indentation.
def SayHello(name):
print("Hello ", name)
print("Welcome to Python Tutorials")