Translation Rules: It Contains Regular Expressions and Code Segments
Translation Rules: It Contains Regular Expressions and Code Segments
Translation Rules: It Contains Regular Expressions and Code Segments
Flex (fast lexical analyzer generator) is a free and open-source software alternative to lex. It is a
computer program that generates lexical analyzers (also known as "scanners" or "lexers").
{ declarations }
%%
{ transition rules }
%%
{ Auxiliary functions } (optional)
Declaration: This section includes declaration of variables, constants and regular definitions.
Translation rules: It contains regular expressions and code segments.
Form : Pattern {Action}
Pattern is a regular expression or regular definition.
Action refers to segments of code.
Auxiliary functions: LEX generates C code for the rules specified in the Rules section and places
this code into a single function called yylex(). In addition to this LEX generated code, the
programmer may wish to add his own code to the lex.yy.c file. The auxiliary functions section
allows the programmer to achieve this.
The following variables are offered by LEX to aid the programmer in designing sophisticated
lexical analyzers. These variables are accessible in the LEX program and are automatically
declared by LEX in lex.yy.c.
yyin-
yytext-
yyleng-
The yyfunctions
yylex()
yywrap()
Example of lex program
/*** Definition section ***/
%{
/* C code to be copied verbatim */
#include <stdio.h>
%}
/* This tells flex to read only one input file */
%option noyywrap
%%
Working of JVM
Execution Engine
Execution engine execute the .class (bytecode). It reads the byte-code line by line, use data and
information present in various memory area and execute instructions. It can be classified in
three parts :-
Interpreter : It interprets the bytecode line by line and then executes.
Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter. It compiles the
entire bytecode and changes it to native code so whenever interpreter see repeated
method calls ,JIT provide direct native code for that part so re-interpretation is not
required, thus efficiency is improved.
Garbage Collector : It destroy un-referenced objects.
It is a interface which interacts with the Native Method Libraries and provides the native
libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be
called by C/C++ libraries which may be specific to hardware.
2D row-major
As mentioned above, in row-major layout, the first row of the matrix is placed in contiguous
memory, then the second, and so on:
2D column-major
Describing column-major 2D layout is just taking the description of row-major and replacing
every appearance of "row" by "column" and vice versa. The first column of the matrix is placed
in contiguous memory, then the second, and so on:
In 3D
Assuming 3 dimensions: rows, columns and depth. The following diagram shows the memory
layout of a 3D array with , in row-major:
4. Design a web page in PHP.
Step 1
Create a folder on your server and name it "design".
Step 2
Create the following files in the design folder:
'header.html', 'footer.html', 'right_column.html', 'left_column.html'
Step 3
Create another folder and name it "pages"
Step 4
In the "pages" directory, create a page and give it the name: 'main.html'
Step 5
Now in the root directory create a file and give it the 'index.php'
Step 6
Add the following code to your 'index.php' file (don't worry, it will be explained later!):
<?php
if (isset($_REQUEST['page']))
{
if($_REQUEST['page'] !="")
if(file_exists("pages/".$_REQUEST['page'].".html"))
$page_content = file_get_contents("pages/".$_REQUEST['page'].".html");
else
if (file_exists($_REQUEST['page'].".html"))
$page_content = file_get_contents($_REQUEST['page'].".html");
else
echo "<center>Page:".$_REQUEST['page']." does not exist! Please check the url and try
again!</center>";
}
else
$page_content = file_get_contents("pages/main.html");
$page_content = str_replace("!!HEADER!!",
file_get_contents("design/header.html"),$page_content);
$page_content = str_replace("!!LEFT_COLUMN!!",
file_get_contents("design/left_column.html"),$page_content);
$page_content = str_replace("!!RIGHT_COLUMN!!",
file_get_contents("design/right_column.html"),$page_content);
$page_content = str_replace("!!FOOTER!!",
file_get_contents("design/footer.html"),$page_content);
$page_content = str_replace("!!COMMON_TAGS!!",
file_get_contents("design/common_tags.html"),$page_content);
echo $page_content;
?>
Step 7
Go to your 'main.html' and design it as you would like your website layout to look at the
end, only here, instead of adding the complete header design, add !!HEADER!! and then
go to the 'header.html' file that you created in the "design" folder. Now in the
'header.html', design the main header of your website. This design will be the header of
all your pages at the end.
Now do the same thing for the other designs, that is: put !!FOOTER!! and design
'footer.html', !!RIGHT_COLUMN!! and design 'right_column.html', put
!!LEFT_COLUMN!! and design 'left_column.html' respectively.
'main.html'
'header.html'
<div class="footer">
<center>
<a href="http://www.zeronese.net">PHP Simple Templating System is a Copy Right of
Zeronese.net</a>
</center>
</div>
'right_column.html'
and to add a little touch to our design, we create a 'styles.css' file in the design folder and add
the following code:
body{
background-color:#003399;
color:#FFFFFF;
}
a{
color:#FFFFFF;
font-weight:bold;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
.column{
background-color:#3366CC;
vertical-align:top;
}
.header{
background-color:#336699;
}
.footer{
background-color:#336699;
}
#include <iostream>
int main() {
int *pc, c;
c = 5;
cout << "Value of c (c): " << c << endl << endl;
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
c = 11; // The content inside memory address &c is changed from 5 to 11.
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
*pc = 2;
cout << "Value of c (c): " << c << endl << endl;
return 0;
}
6.Write a program in Java to implement exception handling.
class Division {
public static void main(String[] args) {
int a, b, result;
a = input.nextInt();
b = input.nextInt();
// try block
try {
result = a / b;
System.out.println("Result = " + result);
}
// catch block
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}
7. Write a program in C++ to implement different parameter passing Methods.
#include <iostream>
using namespace std;
// function declaration
void swap(int x, int y);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
Passing by Pointer:
// C++ program to swap two numbers using
// pass by pointer.
#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
int z = *x;
*x = *y;
*y = z;
}
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\n";
swap(&a, &b);
cout << "After Swap with pass by pointer\n";
cout << "a = " << a << " b = " << b << "\n";
}
Output:
Before Swap
a = 45 b = 35
After Swap with pass by pointer
a = 35 b = 45
Passing by Reference:
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
}
// Main Class
public class Multithread
{
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i=0; i<8; i++)
{
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}
Output:
Thread 12 is running
Thread 15 is running
Thread 13 is running
Thread 17 is running
Thread 16 is running
Thread 14 is running
Thread 11 is running
Thread 10 is running
9. Implement different types of functions used in Prolog.
Defining four functions (it’s called predicates in prolog), addition, subtraction, multiplication,
and division. Each of them takes three parameters, the result is saved to the third parameter.
Queries
?- addition(2,3,X).
?- subtraction(4,2,X).
?- multiplication(3,4,X).
?- division(9, 3, X).
10. Implement Inheritance, Encapsulation & Polymorphism in C#.
Inheritance
public class ParentClass {
public ParentClass() {
Console.WriteLine("Parent Constructor.");
}
public void print() {
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass: ParentClass {
public ChildClass() {
Console.WriteLine("Child Constructor.");
}
public static void Main() {
ChildClass child = new ChildClass();
child.print();
}
}
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Encapsulation
using system;
public class Department {
private string departnamenane;
public int departed;
// Accessor.
public string GetDepartname() {
return departname;
}
// Mutator.
public void SetDepartname(string a) {
departname = a;
}
}
public static int Main(string[] args) {
Department d = new Department();
d.SetDepartname("CS&IT");
Console.WriteLine("The Department is :" + d.GetDepartname());
return 0;
}
Output:
The Department is: CS&IT
Polymorphism