0% found this document useful (0 votes)
2 views5 pages

Lab 5 (Latest-ByAman)(For Students)

The document outlines the steps to create a Java project named 'Lab5Activity' and implement a JavaCC parser in a file called 'Lab5.jj'. It includes detailed instructions for setting up the package, writing the parser code, and running the application to read input from the standard input. Additionally, it provides exercises for creating methods that recognize specific patterns in input strings.

Uploaded by

oshayabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Lab 5 (Latest-ByAman)(For Students)

The document outlines the steps to create a Java project named 'Lab5Activity' and implement a JavaCC parser in a file called 'Lab5.jj'. It includes detailed instructions for setting up the package, writing the parser code, and running the application to read input from the standard input. Additionally, it provides exercises for creating methods that recognize specific patterns in input strings.

Uploaded by

oshayabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 5

1. Select File → New→ Java Project


2. Give Project Name as ‘Lab5Activity’ and click on Finish.
3. Right click on Lab5Activity → src and select New → Package
4. Write package name as ‘Lab5Pack’ and click on Finish
5. Right click on ‘Lab5Pack’ and select New → other . . .
6. In ‘Select a wizard’, select JavaCC → JavaccSampleFile and click on Next.
7. In the next window, enter File Name as Lab5.jj and click on Finish as follows:
Folder: Lab5Activity/src
Package: Lab5Pack
Type: JJFile
Static/Non Static: Static
File Name: Lab5.jj
8. Expand ‘Lab5Pack’ and open the file ‘Lab5.jj’ by double clicking on it.
9. Write following code in it
/**
* JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for
JavaCC 1.5.0+
*/
options
{
static = true;
}

PARSER_BEGIN(Lab5)

package Lab5Pack;

public class Lab5


{
public static void main(String args []) throws ParseException
{
Lab5 parser = new Lab5(System.in);
while (true)
{
System.out.println("Reading from standard input...");
System.out.print("Enter an a followed by semicolon" :");
try
{
switch (Lab5.A())
{
case 0 :
System.out.println("OK.");
break;
case 1 :
System.out.println("Goodbye.");
break;
default :
break;
}
}
catch (Exception e)
{
System.out.println("NOK.");
System.out.println(e.getMessage());
Lab5.ReInit(System.in);
}
catch (Error e)
{
System.out.println("Oops.");
System.out.println(e.getMessage());
break;
}
}
}
}

PARSER_END(Lab5)

SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}

TOKEN :
{

< A : "a" >

int A() :
{ }
{
<A> ";"
{
System.out.println("Found an a");
return 0;
}
| ";"
{
return 1;
}
}

10. Now right click on ‘Lab5Activity’ or ‘Lab5.java’


11. Select Run As → Java Application to execute this file.

You will see the following input


Reading from standard input...
Enter an a followed by semicolon: a;
Found an a
OK.
Reading from standard input...
Enter an a followed by semicolon: ;
Goodbye.
Reading from standard input...
Enter an a followed by semicolon:

Note:
If you want to enter four a’s, you should write your statements as
/**
* JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for JavaCC
1.5.0+
*/
options
{
static = true;
}

PARSER_BEGIN(Lab5)

package Lab5Pack;

public class Lab5


{
public static void main(String args []) throws ParseException
{
Lab5 parser = new Lab5(System.in);
while (true)
{
System.out.println("Reading from standard input...");
System.out.print("Enter four a's followed by semicolon: ");
try
{
switch (Lab5.A())
{
case 0 :
System.out.println("OK.");
break;
case 1 :
System.out.println("Goodbye.");
break;
default :
break;
}
}
catch (Exception e)
{
System.out.println("NOK.");
System.out.println(e.getMessage());
Lab5.ReInit(System.in);
}
catch (Error e)
{
System.out.println("Oops.");
System.out.println(e.getMessage());
break;
}
}
}
}

PARSER_END(Lab5)

SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}

TOKEN :
{

< A : ("a") { 4 } >

int A() :
{ }
{
<A> ";"
{
System.out.println("Found four a's");
return 0;
}
| ";"
{
return 1;
}
}

The output will be as follows:


Reading from standard input...
Enter four a's followed by semicolon: aaaa;
Found four a's
OK.
Reading from standard input...
Enter four a's followed by semicolon: aaa;
Oops.
Lexical error at line 2, column 4. Encountered: '59' (59), after prefix
"aaa"
Exercises:
1) Write method named hi() which recognizes all words starting with ‘h’, followed by one
or more i’s. For example, hi, hii, hiii and so on.
Solution:

2) Write method named world() which recognizes all words starting with worl, followed by
2 up to 5 d’s. For example, worldd, worlddd, worldddd, worlddddd.
Solution:

You might also like