Optional and required terminators in a context-sensitive grammar definition

The chapter on compilers contains the following grammar definition and sample code.

...
statement: whileStatement
           | ifStatement
           | ... // Other statement possibilities
           | '{' statementSequence '}'
whileStatement: 'while' '(' expression ')' statement
ifStatement: ... // Definition of "if"
statementSequence: '' // empty sequence (null)
                   | statement ';' statementSequence
expression: ... // Definition of "expression"
...             // More definitions follow

 

while (expression) {
 statement;
 statement;
 while (expression) {
  while(expression)
     statement;
  statement;
 }
}

How does the loop work with the inner most code whilewithout { }? It seems to me that defining the wording requires them. Is this a mistake in the book or do I not understand the syntax?


[Change] I apologize for any ambiguity. Everything printed above is verbatim from the book. The passes were not my business.

+3
source share
2 answers

Consider your code example again:

1 while (expression) {
2  statement;
3  statement;
4  while (expression) {
5   while(expression)
6      statement;
7   statement;
8  }
9 }

, 6 , , 2, 3 7 ? , while statement, a statementSequence, , statement. 5 6 , ';', .

+2

while , ) . statement, . .

+2

Source: https://habr.com/ru/post/1717182/


All Articles