Programming-Language-Design / src / parser / LexerHelper.java
LexerHelper.java
Raw
package parser;

public class LexerHelper {
	
	public static int lexemeToInt(String str) {
		try {
			return Integer.parseInt(str);
		}
		catch(NumberFormatException e) {
			System.out.println(e);
		}
		return -1;
	}

	public static char lexemeToChar(String str) {
		//Remove quotation marks
		str = str.substring(1, str.length()-1);

		if(str.length()==1) //char constant -> 'a'
			return str.charAt(0);
		else if(str.equals("\\n"))
			return '\n';
		else if(str.equals("\\t"))
			return '\t';
		else { // ASCII CODE
			try {
				return (char) Integer.parseInt(str.substring(1));
			}
			catch(NumberFormatException e) {
				System.out.println(e);
			}
			return '0';
		}
	}

	public static double lexemeToReal(String str) {
		try{
			return Double.parseDouble(str);
		}
		catch(NumberFormatException e) {
			System.out.println(e);
		}
		return -1;
	}





}