Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Token> lex ( String input ) {
- ArrayList<Token> tl = new ArrayList<>();
- Pattern pattern = null;
- pattern = Pattern.compile("if[^a-zA-Z]|[a-zA-Z]+|[0-9]+|[+]|[-]");
- for (String s: input.split("\\s")) {
- System.out.println(s);
- Matcher matcher = pattern.matcher(s);
- while (matcher.find()) {
- if (matcher.group().matches("if") && s.length() == 2) {
- tl.add(new T_If());
- }
- else if (matcher.group().matches("then") && s.length() == 4) {
- tl.add(new T_Then());
- }
- else if (matcher.group().matches("[+]")) {
- tl.add(new T_Plus());
- }
- else if (matcher.group().matches("[-]")) {
- tl.add(new T_Minus());
- }
- else if (matcher.group().matches("[a-zA-Z]+(\\w)*")) {
- tl.add(new T_Identifier(matcher.group()));
- }
- else if (matcher.group().matches("[0-9]+")) {
- tl.add(new T_Integer(Integer.parseInt(matcher.group())));
- //System.out.println(matcher.group().toString());
- }
- }
- }
- for (Token t: tl) {
- System.out.println(t.toString());
- }
- return tl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement