Solved

Java Lexical Analyzer

Posted on 2013-11-04
1
694 Views
Last Modified: 2013-11-05
I need assistance in wrapping my head around the development of a lexical analyzer in Java.  It has been a little bit since I have taken my last java course, however am fairly familiar with the language.  In the class I am taking we are working with BNF grammars and using recursive descent parsing to traverse these grammars and verify that a user input string is valid according to the rules of the grammar.  I have a decent pseudo code design for the grammar and have pieced most of it together, however the design of the lexical analyzer is eluding me to read the user input string.  
There are only 3 letters I am concerned with a, b and c.  I am trying to stay with the book here and declare the tokens separate as an enumerated type (i.e. {NONE, LETTER, ERROR, END_OF_FILE}).
My initial question:
Is this basic idea correct for the lexer?
•      Start with character NONE
•      Look at next character
    o          if character = a, b, or c
¿              Add character to lexeme
¿              Look at Next character
              •       Continue until end of user input string
    o          If there is no character
¿              Return lexeme
    o          If character != a, b, or c
¿              Return ERROR token
0
Comment
Question by:Autkast
1 Comment
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39623422
Yes that approach basically sounds right, although you don't actually say what the valid lexemes are.  It sounds like you're accepting strings of the form "[abc]+"?

The only part that looks a little odd is here:

>> If there is no character
>>             Return lexeme

Usually this would be "if the next character is white space, then return lexeme".  But there appears to be no white space in your character set, so maybe this is equivalent to what you have?

E.g. If you were parsing java, and the lexical analyzer was reading an identifier, it would normally terminate at the first character not in the valid set for the identifier:

int abc = 10 ;

while parsing 'abc' you want the lexical analyzer to stop at the ' ' (space) after 'abc' - because it's the first character not allowed within an identifier - and return the lexeme at that point.

Anyway, hope that helps,

Doug
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question