Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created December 22, 2011 16:23
Show Gist options
  • Save kakkun61/1510874 to your computer and use it in GitHub Desktop.
Save kakkun61/1510874 to your computer and use it in GitHub Desktop.
Java Advent Calendar 2011 code 7
PARSER_BEGIN (Parser)
public class Parser {
}
PARSER_END (Parser)
SKIP: {
" "
}
TOKEN: {
<INTEGER: (["0"-"9"])+>
| <PLUS: "+">
| <MINUS: "-">
| <NL: "\r\n" | "\n" | "\r">
}
void List(): {
int val;
} {
val = Expression() <NL> {
System.out.println(val);
}
}
int Expression(): {
int val1;
int val2;
} {
val1 = PrimaryExpression() (
<PLUS> val2 = PrimaryExpression() {
val1 += val2;
}
| <MINUS> 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