问题
Is there an EBNF rule that describes a Forth infinite loop or if statement?
回答1:
EBNF is used to describe syntax. A loop being infinite or otherwise wouldn't normally fall within what it would describe. As such, you'd be looking at the EBNF for an indefinite loop, which looks something like:
indefinite_loop ::= 'BEGIN' statements cond 'UNTIL'
Normally the cond
will be something that pushes a 0 or 1 on the stack to determine whether to continue the loop (0
means continue the loop, 1
means exit). As such, if you just insert a 0
directly, the loop will execute forever:
: infinite_loop BEGIN do_whatever 0 UNTIL ;
回答2:
You can also use:
infinite_loop ::= 'BEGIN' statements 'AGAIN'
The AGAIN
word is used to do an unconditional branch back to the BEGIN
. For example:
: main-loop BEGIN listen-for-event process-event AGAIN ;
来源:https://stackoverflow.com/questions/6053030/forth-language-ebnf-rule-for-an-infinite-loop-or-if-statement