Earlier I created this question asking how to create if/else statements using ANTLR 4. I got an excellent answer which also showed how to do while loops. I have implemented this in my language and I am now trying to make a do-while loop using nearly the same principles.
My syntax is as follows for while loops:
count is 0
while count is less than 10
count+
if count not equals 10
write " " + count + ": Getting there..."
else if count equals 10
write count + ": The end!"
end if
end while
And this is what I would like for do-while loops:
count is 0
do
count+
write "count is " + count
if count equals 10
write "The end!"
end if
while count is less than 10
I have tested it and they both work, however, not at the same time. Below is my grammar (sorry for posting it all but I think it's necessary).
If my WHILE
and END_WHILE
tokens are above my DO_WHILE
and DO_WHILE_CONDITION
tokens, then the while loop works. However, if I switch them around my do-while loop works. If I change the DO_WHILE_CONDITION
token to be anything else than while then both works.
Is there anyway that I can have them both work with the current syntax? I understand that it may be a problem because I use the same keyword for multiple things but I hope there is a way to do this.
//////////////////////////////////
// PARSER
//////////////////////////////////
program
: block EOF
;
block
: (statement (NEW_LINE+ | EOF))*
;
statement
: assignment
| if_statement
| while_statement
| until_statement
| do_while_statement
| write
;
assignment
: ID ASSIGN expression # expressionAssignment
| ID PLUS # incrementAssignment
| ID MINUS # decrementAssignment
;
if_statement
: IF condition_block (ELSE_IF condition_block)* (ELSE NEW_LINE statement_block)? END_IF
;
condition_block
: expression NEW_LINE statement_block
;
statement_block
: block
;
while_statement
: WHILE expression NEW_LINE statement_block END_WHILE
;
until_statement
: UNTIL expression NEW_LINE statement_block END_UNTIL
;
do_while_statement
: DO_WHILE NEW_LINE statement_block DO_WHILE_CONDITION expression
;
expression
: atom # atomExpression
| expression PLUS expression # plusExpression
| expression MINUS expression # minusExpression
| expression MULTIPLY expression # multiplicationExpression
| expression DIVIDE expression # divisionExpression
| expression PLUS # incrementExpression
| expression MINUS # decrementExpression
| expression AND expression # andExpression
| expression OR expression # orExpression
| expression EQUALS expression # equalityExpression
| expression NOT_EQUALS expression # notEqualityExpression
| expression LESS_THAN expression # lessThanExpression
| expression NOT_LESS_THAN expression # notLessThanExpression
| expression GREATER_THAN expression # greaterThanExpression
| expression NOT_GREATER_THAN expression # notGreaterThanExpression
| expression GREATER_THAN_OR_EQUAL expression # greaterThanOrEqualExpression
| expression LESS_THAN_OR_EQUAL expression # lessThanOrEqualExpression
;
atom
: INT # integerAtom
| FLOAT # floatAtom
| BOOLEAN # boolAtom
| ID # idAtom
| STRING # stringAtom
| OPEN_PAR expression CLOSE_PAR # expressionAtom
;
write
: WRITE expression
;
//////////////////////////////////
// LEXER
//////////////////////////////////
PLUS : '+';
MINUS : '-';
MULTIPLY : '*';
DIVIDE : '/';
ASSIGN : 'is';
OPEN_CURLY : '{';
CLOSE_CURLY : '}';
OPEN_PAR : '(';
CLOSE_PAR : ')';
COLON : ':';
NEW_LINE : '\r'? '\n';
IF : 'if';
ELSE_IF : 'else if';
ELSE : 'else';
END_IF : 'end if';
WHILE : 'while';
END_WHILE : 'end while';
UNTIL : 'until';
END_UNTIL : 'end until';
DO_WHILE : 'do';
DO_WHILE_CONDITION : 'while';
EQUALS : 'equals';
NOT_EQUALS : 'not equals';
LESS_THAN : 'is less than';
NOT_LESS_THAN : 'is not less than';
GREATER_THAN : 'is greater than';
NOT_GREATER_THAN : 'is not greater than';
GREATER_THAN_OR_EQUAL : 'is greater than or equals';
LESS_THAN_OR_EQUAL : 'is less than or equals';
WRITE : 'write';
AND : 'and';
OR : 'or';
NOT : 'not';
BOOLEAN
: 'TRUE' | 'true' | 'YES' | 'yes'
| 'FALSE' | 'false' | 'NO' | 'no'
;
INT
: (PLUS | MINUS)? NUMBER+
;
FLOAT
: (PLUS | MINUS)? NUMBER+ ('.' | ',') (NUMBER+)?
| (PLUS | MINUS)? (NUMBER+)? ('.' | ',') NUMBER+
;
NUMBER
: '0'..'9'
;
STRING
: '"' ( '\\"' | ~["] )* '"'
;
ID
: ('a'..'z' | 'A'..'Z' | '0'..'9')+
;
WHITESPACE
: [ \t]+ -> skip
;
COMMENT
: ( ';;' .*? ';;' | ';' ~[\r\n]* ) -> skip
;