Question: Antlr lexical analyzer Build a lexical analyzer for the Java programming language in ANTLR. The lexical analyzer, also known as the lexer , will be
Antlr lexical analyzer
Build a lexical analyzer for the Java programming language in ANTLR. The lexical analyzer, also known as the lexer, will be converting general Java source code to tokens. We aim to be able to handle at least the most commonly found tokens.
lexer grammar JavaLexer;
WHITESPACE : (' ' | '\t' | ' ' | ' ')+ -> skip ;
/* Complete the rest */
Main.Java
/* The solution implementation */
import java.util.regex.*; import java.awt.geom.*; import java.util.Vector;
public class Main { public String name; public Main(String name) { System.out.printf("Constructor for %s ", name); this.name = name; }
public String f(String a, String b) throws Exception { if(a == null || a.length() == 0) throw new Exception(this.name + ":" + " a is null"); if(b == null || b.length() == 0) throw new Exception(this.name + ":" + " b is null");
return a + " " + b; }
public String g(String a, int b) { StringBuilder s = new StringBuilder(a); s.append(" "); s.append(b); s.reverse(); return s.toString(); }
public void delay(int ms) throws Exception { System.out.printf("[%s] sleep for %d ms ", this.name, ms); Thread.sleep(ms); System.out.printf("[%s] done ", this.name); }
public Pattern compile(String re) throws Exception { return Pattern.compile(re); }
public Point2D point() { return new Point2D.Double(1, 2); } }
Test.java
age = 123;
int adder(int a, int b) { }
class Empty { }
/* A hello class */ class Hello { public Hello(int name) {
} }
class Hello { String say(String message) { return "Hello there, " + message; }
int adder(int a, int b) { return a + b; } }
public class Hello { String name; public Hello(name) { this.name = name; } @Override String toString() { return this.name; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
