It would be far better to test for the statements that start with keywords first, and then fall into the assignment case if no keywords match. That way you don't need X,Y,Z as keywords:
private void statemt(){
if(token() == 'I'){
ifstmt();
}else if(token() == 'D'){
doloop();
}else if(token() == 'R'){
read();
}else if(token() == 'O'){
output();
} else {
assnmt();
}
}
I don't see any good reason for these abbrevations either in this day and age, and language.
To answer your question about the if
statement:
private void ifstatement() {
match('I');
comprsn();
match('@');
statement();
if (token() == '%') {
statement();
}
if (token() != '&') {
syntax_error("'&' expected");
}
}
To answer the second extension about {
statement ... }
, this is represented in the grammar by
statement ::= '{' statement ... '}'
and this is implemented in statement()
thus:
private void statement(){
if (token == '{') {
do {
statement();
} while (token() != '}');
} else if(token() == 'I'){
ifstmt();
}else if(token() == 'D'){
doloop();
}else if(token() == 'R'){
read();
}else if(token() == 'O'){
output();
} else {
assnmt();
}
}
Note that I'm completely ignoring the issue of when tokens get consumed, as you haven't told us how your token()
and match()
methods work.