Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Lexical Analyzer

Posted on 2013-11-04
1
Medium Priority
?
745 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 28

Accepted Solution

by:
dpearson earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article discusses the Chain of Responsibility pattern, explaining What it is;Why it is; andHow it is At the end of this article, I hope you will be able to describe the use and benefits of Chain of Responsibility.  Backgrou…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses

971 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