patternjavaMinor
Ad-hoc lexical scanner for a calculator language
Viewed 0 times
lexicallanguagescannerforcalculatorhoc
Problem
Challenge:
Build an ad-hoc scanner for a calculator language.
Specifications:
The tokens for the language are as follows:
assign → :=
plus → +
minus → -
times → *
div → /
lparen → (
rparen → )
id → letter (letter|digit)*
number → digit digit | digit (.digit | digit.) digit *
-------excluding read and write
comment → / (non- | non-/) / | // (non-newline newline
For simplicity, halt in case of a lexical error.
Sample Input:
number := a + b
total := (a - b) * c
final := total + num
LexTest := )) a - b
My Solution:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LexScan {
final static String[] ILLEGAL_IDS = {"read", "write"};
static enum Token {
ASSIGN(':', ":="),
PLUS('+'),
MINUS('-'),
TIMES('*'),
DIVIDES('/'),
LEFT_PARENTHESES('('),
RIGHT_PARENTHESES(')');
private final String lexeme;
private final char matcher;
Token(char matcher) {
this.matcher = matcher;
lexeme = String.valueOf(matcher);
}
Token(char matcher, String lexeme) {
this.matcher = matcher;
this.lexeme = lexeme;
}
public char matcher() {
return matcher;
}
public String lexeme() {
return lexeme;
}
@Override
public String toString() {
return name().replaceAll("_", " ");
}
public String toLexEntry() {
return toString() + ": " + lexeme;
}
}
public static void main(String[] args) {
if (args.length > 0) {
iterativePrintln(lexify(toTestString(args)));
System.exit(0);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter test case: ");
iterativePrintln(lexify(input.nextLine()));
}
public static List lexify(String testCase) {
String sanitized = testCase.re
Build an ad-hoc scanner for a calculator language.
Specifications:
The tokens for the language are as follows:
assign → :=
plus → +
minus → -
times → *
div → /
lparen → (
rparen → )
id → letter (letter|digit)*
number → digit digit | digit (.digit | digit.) digit *
-------excluding read and write
comment → / (non- | non-/) / | // (non-newline newline
For simplicity, halt in case of a lexical error.
Sample Input:
number := a + b
total := (a - b) * c
final := total + num
LexTest := )) a - b
My Solution:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LexScan {
final static String[] ILLEGAL_IDS = {"read", "write"};
static enum Token {
ASSIGN(':', ":="),
PLUS('+'),
MINUS('-'),
TIMES('*'),
DIVIDES('/'),
LEFT_PARENTHESES('('),
RIGHT_PARENTHESES(')');
private final String lexeme;
private final char matcher;
Token(char matcher) {
this.matcher = matcher;
lexeme = String.valueOf(matcher);
}
Token(char matcher, String lexeme) {
this.matcher = matcher;
this.lexeme = lexeme;
}
public char matcher() {
return matcher;
}
public String lexeme() {
return lexeme;
}
@Override
public String toString() {
return name().replaceAll("_", " ");
}
public String toLexEntry() {
return toString() + ": " + lexeme;
}
}
public static void main(String[] args) {
if (args.length > 0) {
iterativePrintln(lexify(toTestString(args)));
System.exit(0);
}
Scanner input = new Scanner(System.in);
System.out.print("Enter test case: ");
iterativePrintln(lexify(input.nextLine()));
}
public static List lexify(String testCase) {
String sanitized = testCase.re
Solution
Parentheses checking not complete
Your program currently checks that open parentheses are balanced numberswise with close parentheses. However, it does not check that the parentheses occur in the correct sequence or even in syntactically correct situations. For example, all of the following test cases are considered correct:
Expand your grammar
I think that in order to correctly handle parentheses and other syntactical errors, you need to define a more complete grammar. Something like this:
Then you need to attempt to parse a "statement" and print an error if the input doesn't match the grammar for a statement.
Your program currently checks that open parentheses are balanced numberswise with close parentheses. However, it does not check that the parentheses occur in the correct sequence or even in syntactically correct situations. For example, all of the following test cases are considered correct:
a := ) b + c (
a := b + c ()
a := (b + ) cExpand your grammar
I think that in order to correctly handle parentheses and other syntactical errors, you need to define a more complete grammar. Something like this:
-> :=
-> | "+" | "-"
-> | "*" | "/"
-> | "(" ")"
-> (your current syntax for id)Then you need to attempt to parse a "statement" and print an error if the input doesn't match the grammar for a statement.
Code Snippets
a := ) b + c (
a := b + c ()
a := (b + ) c<statement> -> <id> := <expression>
<expression> -> <term> | <term> "+" <expression> | <term> "-" <expression>
<term> -> <factor> | <term> "*" <factor> | <term> "/" <factor>
<factor> -> <id> | "(" <expression> ")"
<id> -> (your current syntax for id)Context
StackExchange Code Review Q#106575, answer score: 3
Revisions (0)
No revisions yet.