SlideShare a Scribd company logo
1 of 5
Download to read offline
import java.io.*;
import java.util.Scanner;
// to run on Athena (linux) -
// save as: Recognizer.java
// compile: javac Recognizer.java
// execute: java Recognizer
//
// EBNF Grammar is -
// ::= |
// ::= |
// ::= ( )
// ::= { }
// ::= 0 | 1
// ::= a | b | c
//--------------------------------------------
public class Recognizer
{
static String inputString;
static int index = 0;
static int errorflag = 0;
private char token()
{ return(inputString.charAt(index)); }
private void advancePtr()
{ if (index < (inputString.length()-1)) index++; }
private void match(char T)
{ if (T == token()) advancePtr(); else error(); }
private void error()
{
System.out.println("error at position: " + index);
errorflag = 1;
advancePtr();
}
//----------------------
private void exp()
{ if (token() == '(') list(); else atom(); }
private void atom()
{ if ((token() == '0')
|| (token() == '1')) digit(); else str(); }
private void list()
{
match('(');
exprlist();
match(')');
}
private void exprlist()
{ while ((token() == '0')
|| (token() == '1')
|| (token() == 'a')
|| (token() == 'b')
|| (token() == 'c')
|| (token() == '(')) exp(); }
private void digit()
{ if ((token() == '0') || (token() == '1')) match(token()); else error(); }
private void str()
{ if ((token() == 'a') || (token() == 'b') || (token() == 'c')) match(token()); else error(); }
//----------------------
private void start()
{
exp();
match('$');
if (errorflag == 0)
System.out.println("legal." + " ");
else
System.out.println("errors found." + " ");
}
//----------------------
public static void main (String[] args) throws IOException
{
Recognizer rec = new Recognizer();
Scanner input = new Scanner(System.in);
System.out.print(" " + "enter an expression: ");
inputString = input.nextLine();
rec.start();
}
}
Solution
import java.io.*;
import java.util.Scanner;
// to run on Athena (linux) -
// save as: Recognizer.java
// compile: javac Recognizer.java
// execute: java Recognizer
//
// EBNF Grammar is -
// ::= |
// ::= |
// ::= ( )
// ::= { }
// ::= 0 | 1
// ::= a | b | c
//--------------------------------------------
public class Recognizer
{
static String inputString;
static int index = 0;
static int errorflag = 0;
private char token()
{ return(inputString.charAt(index)); }
private void advancePtr()
{ if (index < (inputString.length()-1)) index++; }
private void match(char T)
{ if (T == token()) advancePtr(); else error(); }
private void error()
{
System.out.println("error at position: " + index);
errorflag = 1;
advancePtr();
}
//----------------------
private void exp()
{ if (token() == '(') list(); else atom(); }
private void atom()
{ if ((token() == '0')
|| (token() == '1')) digit(); else str(); }
private void list()
{
match('(');
exprlist();
match(')');
}
private void exprlist()
{ while ((token() == '0')
|| (token() == '1')
|| (token() == 'a')
|| (token() == 'b')
|| (token() == 'c')
|| (token() == '(')) exp(); }
private void digit()
{ if ((token() == '0') || (token() == '1')) match(token()); else error(); }
private void str()
{ if ((token() == 'a') || (token() == 'b') || (token() == 'c')) match(token()); else error(); }
//----------------------
private void start()
{
exp();
match('$');
if (errorflag == 0)
System.out.println("legal." + " ");
else
System.out.println("errors found." + " ");
}
//----------------------
public static void main (String[] args) throws IOException
{
Recognizer rec = new Recognizer();
Scanner input = new Scanner(System.in);
System.out.print(" " + "enter an expression: ");
inputString = input.nextLine();
rec.start();
}
}

More Related Content

Similar to import java.io.; import java.util.Scanner; to run on Athena .pdf

Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
David Xie
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 

Similar to import java.io.; import java.util.Scanner; to run on Athena .pdf (20)

What`s new in Java 7
What`s new in Java 7What`s new in Java 7
What`s new in Java 7
 
Java设置环境变量
Java设置环境变量Java设置环境变量
Java设置环境变量
 
Java final project of scientific calcultor
Java final project of scientific calcultorJava final project of scientific calcultor
Java final project of scientific calcultor
 
2014 holden - databricks umd scala crash course
2014   holden - databricks umd scala crash course2014   holden - databricks umd scala crash course
2014 holden - databricks umd scala crash course
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
Scientific calcultor-Java
Scientific calcultor-JavaScientific calcultor-Java
Scientific calcultor-Java
 
Ioc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiDIoc container | Hannes Van De Vreken | CODEiD
Ioc container | Hannes Van De Vreken | CODEiD
 
Pragmatic Swift
Pragmatic SwiftPragmatic Swift
Pragmatic Swift
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
Rntb20200805
Rntb20200805Rntb20200805
Rntb20200805
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)
 
Tamarin and ECMAScript 4
Tamarin and ECMAScript 4Tamarin and ECMAScript 4
Tamarin and ECMAScript 4
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
MiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScriptMiamiJS - The Future of JavaScript
MiamiJS - The Future of JavaScript
 
JavaScript Core
JavaScript CoreJavaScript Core
JavaScript Core
 
Android ndk
Android ndkAndroid ndk
Android ndk
 

More from arakalamkah11

1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
arakalamkah11
 
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
arakalamkah11
 
Light is a form of energy that can be released by.pdf
                     Light is a form of energy that can be released by.pdf                     Light is a form of energy that can be released by.pdf
Light is a form of energy that can be released by.pdf
arakalamkah11
 
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
arakalamkah11
 
Youre looking for Ka, the dissociation constant.pdf
                     Youre looking for Ka, the dissociation constant.pdf                     Youre looking for Ka, the dissociation constant.pdf
Youre looking for Ka, the dissociation constant.pdf
arakalamkah11
 
remote operating system installationRemote Installation Services (.pdf
remote operating system installationRemote Installation Services (.pdfremote operating system installationRemote Installation Services (.pdf
remote operating system installationRemote Installation Services (.pdf
arakalamkah11
 
Title of this process is The Project Life Cycle (Phases)The pr.pdf
Title of this process is The Project Life Cycle (Phases)The pr.pdfTitle of this process is The Project Life Cycle (Phases)The pr.pdf
Title of this process is The Project Life Cycle (Phases)The pr.pdf
arakalamkah11
 
solutionA=  1    1    0    4    3    1    2    0 .pdf
solutionA=  1    1    0    4    3    1    2    0 .pdfsolutionA=  1    1    0    4    3    1    2    0 .pdf
solutionA=  1    1    0    4    3    1    2    0 .pdf
arakalamkah11
 
Q1). Gene therapy is an experimental approach to treat the disease b.pdf
Q1). Gene therapy is an experimental approach to treat the disease b.pdfQ1). Gene therapy is an experimental approach to treat the disease b.pdf
Q1). Gene therapy is an experimental approach to treat the disease b.pdf
arakalamkah11
 
package s3; Copy paste this Java Template and save it as Emer.pdf
package s3;  Copy paste this Java Template and save it as Emer.pdfpackage s3;  Copy paste this Java Template and save it as Emer.pdf
package s3; Copy paste this Java Template and save it as Emer.pdf
arakalamkah11
 
Our body is having two line defence system against pathogens.Pathoge.pdf
Our body is having two line defence system against pathogens.Pathoge.pdfOur body is having two line defence system against pathogens.Pathoge.pdf
Our body is having two line defence system against pathogens.Pathoge.pdf
arakalamkah11
 

More from arakalamkah11 (20)

Particulars $Amount Direct materials 64452 Direct Labour (1332.pdf
    Particulars $Amount   Direct materials 64452   Direct Labour (1332.pdf    Particulars $Amount   Direct materials 64452   Direct Labour (1332.pdf
Particulars $Amount Direct materials 64452 Direct Labour (1332.pdf
 
Decrease in inventory Source of cash $   440 Decrease in account.pdf
    Decrease in inventory Source of cash $   440   Decrease in account.pdf    Decrease in inventory Source of cash $   440   Decrease in account.pdf
Decrease in inventory Source of cash $   440 Decrease in account.pdf
 
Diamond is sp3 covalent. It makes for bonds to ne.pdf
                     Diamond is sp3 covalent. It makes for bonds to ne.pdf                     Diamond is sp3 covalent. It makes for bonds to ne.pdf
Diamond is sp3 covalent. It makes for bonds to ne.pdf
 
Too low. If you do not dry the sodium thiosulfate.pdf
                     Too low. If you do not dry the sodium thiosulfate.pdf                     Too low. If you do not dry the sodium thiosulfate.pdf
Too low. If you do not dry the sodium thiosulfate.pdf
 
1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
1. A bus is a bunch of wires used to connect multiple subsystems. Bu.pdf
 
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
1. E) The vapor pressure of a liquid.Increasing attractive intermo.pdf
 
Light is a form of energy that can be released by.pdf
                     Light is a form of energy that can be released by.pdf                     Light is a form of energy that can be released by.pdf
Light is a form of energy that can be released by.pdf
 
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
1) Presence of jaws with paired fins in fish helps to feed them. Fin.pdf
 
Youre looking for Ka, the dissociation constant.pdf
                     Youre looking for Ka, the dissociation constant.pdf                     Youre looking for Ka, the dissociation constant.pdf
Youre looking for Ka, the dissociation constant.pdf
 
Okay so dipole-dipole interactions would occur be.pdf
                     Okay so dipole-dipole interactions would occur be.pdf                     Okay so dipole-dipole interactions would occur be.pdf
Okay so dipole-dipole interactions would occur be.pdf
 
remote operating system installationRemote Installation Services (.pdf
remote operating system installationRemote Installation Services (.pdfremote operating system installationRemote Installation Services (.pdf
remote operating system installationRemote Installation Services (.pdf
 
Title of this process is The Project Life Cycle (Phases)The pr.pdf
Title of this process is The Project Life Cycle (Phases)The pr.pdfTitle of this process is The Project Life Cycle (Phases)The pr.pdf
Title of this process is The Project Life Cycle (Phases)The pr.pdf
 
The answer is A) Pol II, with twelve subunits on its own, is capable.pdf
The answer is A) Pol II, with twelve subunits on its own, is capable.pdfThe answer is A) Pol II, with twelve subunits on its own, is capable.pdf
The answer is A) Pol II, with twelve subunits on its own, is capable.pdf
 
solutionA=  1    1    0    4    3    1    2    0 .pdf
solutionA=  1    1    0    4    3    1    2    0 .pdfsolutionA=  1    1    0    4    3    1    2    0 .pdf
solutionA=  1    1    0    4    3    1    2    0 .pdf
 
Q1). Gene therapy is an experimental approach to treat the disease b.pdf
Q1). Gene therapy is an experimental approach to treat the disease b.pdfQ1). Gene therapy is an experimental approach to treat the disease b.pdf
Q1). Gene therapy is an experimental approach to treat the disease b.pdf
 
Solution Three modes of DNA replication 1) Semi conservative.pdf
Solution Three modes of DNA replication 1) Semi conservative.pdfSolution Three modes of DNA replication 1) Semi conservative.pdf
Solution Three modes of DNA replication 1) Semi conservative.pdf
 
package s3; Copy paste this Java Template and save it as Emer.pdf
package s3;  Copy paste this Java Template and save it as Emer.pdfpackage s3;  Copy paste this Java Template and save it as Emer.pdf
package s3; Copy paste this Java Template and save it as Emer.pdf
 
Our body is having two line defence system against pathogens.Pathoge.pdf
Our body is having two line defence system against pathogens.Pathoge.pdfOur body is having two line defence system against pathogens.Pathoge.pdf
Our body is having two line defence system against pathogens.Pathoge.pdf
 
Once neurons are produced, they migrate and modify to form six layer.pdf
Once neurons are produced, they migrate and modify to form six layer.pdfOnce neurons are produced, they migrate and modify to form six layer.pdf
Once neurons are produced, they migrate and modify to form six layer.pdf
 
PDU is called Protocol Data Unit.It consists of user data and protoc.pdf
PDU is called Protocol Data Unit.It consists of user data and protoc.pdfPDU is called Protocol Data Unit.It consists of user data and protoc.pdf
PDU is called Protocol Data Unit.It consists of user data and protoc.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 

Recently uploaded (20)

Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 

import java.io.; import java.util.Scanner; to run on Athena .pdf

  • 1. import java.io.*; import java.util.Scanner; // to run on Athena (linux) - // save as: Recognizer.java // compile: javac Recognizer.java // execute: java Recognizer // // EBNF Grammar is - // ::= | // ::= | // ::= ( ) // ::= { } // ::= 0 | 1 // ::= a | b | c //-------------------------------------------- public class Recognizer { static String inputString; static int index = 0; static int errorflag = 0; private char token() { return(inputString.charAt(index)); } private void advancePtr() { if (index < (inputString.length()-1)) index++; } private void match(char T) { if (T == token()) advancePtr(); else error(); } private void error() { System.out.println("error at position: " + index); errorflag = 1; advancePtr(); } //---------------------- private void exp() { if (token() == '(') list(); else atom(); }
  • 2. private void atom() { if ((token() == '0') || (token() == '1')) digit(); else str(); } private void list() { match('('); exprlist(); match(')'); } private void exprlist() { while ((token() == '0') || (token() == '1') || (token() == 'a') || (token() == 'b') || (token() == 'c') || (token() == '(')) exp(); } private void digit() { if ((token() == '0') || (token() == '1')) match(token()); else error(); } private void str() { if ((token() == 'a') || (token() == 'b') || (token() == 'c')) match(token()); else error(); } //---------------------- private void start() { exp(); match('$'); if (errorflag == 0) System.out.println("legal." + " "); else System.out.println("errors found." + " "); } //---------------------- public static void main (String[] args) throws IOException { Recognizer rec = new Recognizer(); Scanner input = new Scanner(System.in); System.out.print(" " + "enter an expression: ");
  • 3. inputString = input.nextLine(); rec.start(); } } Solution import java.io.*; import java.util.Scanner; // to run on Athena (linux) - // save as: Recognizer.java // compile: javac Recognizer.java // execute: java Recognizer // // EBNF Grammar is - // ::= | // ::= | // ::= ( ) // ::= { } // ::= 0 | 1 // ::= a | b | c //-------------------------------------------- public class Recognizer { static String inputString; static int index = 0; static int errorflag = 0; private char token() { return(inputString.charAt(index)); } private void advancePtr() { if (index < (inputString.length()-1)) index++; } private void match(char T) { if (T == token()) advancePtr(); else error(); } private void error() { System.out.println("error at position: " + index);
  • 4. errorflag = 1; advancePtr(); } //---------------------- private void exp() { if (token() == '(') list(); else atom(); } private void atom() { if ((token() == '0') || (token() == '1')) digit(); else str(); } private void list() { match('('); exprlist(); match(')'); } private void exprlist() { while ((token() == '0') || (token() == '1') || (token() == 'a') || (token() == 'b') || (token() == 'c') || (token() == '(')) exp(); } private void digit() { if ((token() == '0') || (token() == '1')) match(token()); else error(); } private void str() { if ((token() == 'a') || (token() == 'b') || (token() == 'c')) match(token()); else error(); } //---------------------- private void start() { exp(); match('$'); if (errorflag == 0) System.out.println("legal." + " "); else System.out.println("errors found." + " "); }
  • 5. //---------------------- public static void main (String[] args) throws IOException { Recognizer rec = new Recognizer(); Scanner input = new Scanner(System.in); System.out.print(" " + "enter an expression: "); inputString = input.nextLine(); rec.start(); } }