Search code examples
javaparsingintegerlexical

parser with integer literals


I am looking for an easy and efficient way to implement a set of numbers in a lexical parser in java. For example my input code is as follows :

"6+9" ,      

the output would have to be a little like this :

   Number : 6   
   Sign : +   
   Number: 9  

The issue I have is i have no way to recognize the number other than to implement it as follows :

static char INTVALUE = ('0') ;    

which means I would have to manually enter each number from 0 to 9 and I don't know If such a method would even allows to have a number such as 85 in my input .

This is for a homework assignment by the way Thanks .


Solution

  • Why dont use regular expressions for this. It sounds a best fit for what you are attempting to do.

    Its fairly simple to learn. Look at Character classes (\d) and Quatifiers(+ ?) in this cheatsheet

    To check for integers and doubles us the following.

    aStr.matches("-?\\d+(\\.\\d+)?");
    

    For just integers:

    aStr.matches("-?\\d+");