Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created December 22, 2011 18:26
Show Gist options
  • Save kakkun61/1511308 to your computer and use it in GitHub Desktop.
Save kakkun61/1511308 to your computer and use it in GitHub Desktop.
Java Advent Calendar 2011 code 8
PARSER_BEGIN (Parser)
public class Parser {
}
PARSER_END (Parser)
SKIP: {
" "
}
TOKEN: {
<INTEGER: (["0"-"9"])+>
| <PLUS: "+">
| <MINUS: "-">
| <MULTIPLY: "*">
| <DIVIDE: "/">
| <NL: "\r\n" | "\n" | "\r">
}
void List(): {
int val;
} {
(val = Expression() <NL> {
System.out.println(val);
})*
}
int Expression(): {
int val1;
int val2;
} {
val1 = MultiplyExpression() (
<PLUS> val2 = MultiplyExpression() {
val1 += val2;
}
| <MINUS> val2 = MultiplyExpression() {
val1 -= val2;
}
)* {
return val1;
}
}
int MultiplyExpression(): {
int val1;
int val2;
} {
val1 = PrimaryExpression() (
<MULTIPLY> val2 = PrimaryExpression() {
val1 *= val2;
}
| <DIVIDE> val2 = PrimaryExpression() {
val1 /= val2;
}
)* {
return val1;
}
}
int PrimaryExpression(): {
Token token;
} {
token = <INTEGER> {
return Integer.parseInt(token.image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment