Ryan Tibbetts
Ryan Tibbetts

Reputation: 412

Shortest REPEAT statement Modula-2

I'm doing a homework assignment in which I have been provided a snippet of text titled "EBNF Description of Modula-2's Syntax" and the question asks:

"Give an example of the shortest REPEAT statement in Modula-2. ('Shortest' means fewest number of lexemes.)"

I apologies for not being able to provide the text, it is on a page that is password protected, however the text is as the title says. Basically this is what I came up with and I want to know if this works.

The EBNF description of REPEAT and it's following statements is as such:

RepeatStatement = `REPEAT` StatementSequence `UNTIL` Expression.

StatementSequence = Statement {“;” Statement}.

Statement = [Assignment | ProcedureCall | IfStatement | CaseStatement | 
             WhileStatement | RepeatStatement | LoopStatement | 
             ForStatement | WithStatement | `EXIT`

So by this description could I simply say:

REPEAT EXIT.

and be done? or do I absolutely have to use the UNTIL and/or the full StatementSequence EBNF description?

TL;DR is the rest of the EBNF statement void if I just say EXIT?

Upvotes: 2

Views: 348

Answers (2)

trijezdci
trijezdci

Reputation: 5474

Since this is homework, I believe the real question here is

"How do I figure out the EBNF for Modula-2?"

I would like to recommend to visualise the syntax rule by drawing a railroad diagram and then compare that to the EBNF. This will gradually give you an understanding of what the EBNF expresses.

This is the diagram for the REPEAT statement:

http://modula-2.net/m2r10/pmwiki.php?n=SyntaxDiagrams.NonTerminals#repeatStatement

Some of Wirth's PIM (Programming in Modula-2) editions did have the railroad diagrams in the annex of the book. If yours does not, then you may want to find syntax diagrams online.

Our project wiki has EBNF and syntax diagrams for Modula-2 R10 side by side (cross-linked) at

http://modula-2.net/m2r10/pmwiki.php?n=Spec.Modula-2Syntax

However, keep in mind that this is a new revised Modula-2 dialect, there are differences between that and the dialect you will be using at your university. Nevertheless, comparing the diagrams with their corresponding EBNF will help you gain a better understanding of the EBNF notation.

Last but not least, you can use the script below to draw your own Modula-2 syntax diagrams, using the dialect of your choice:

https://bitbucket.org/trijezdci/m2r10/src/tip/_GRAMMAR/modula2_syntax_diagrams.tcl

The script contains a detailed explanation how to convert EBNF into the list notation the script uses for diagram descriptions.

hope this helps

UPDATE:

I have now generated diagrams specifically for PIM Modula-2 and put it up on the Modula-2 Info Wiki at:

http://modula-2.info/m2pim/pmwiki.php/SyntaxDiagrams/PIM4NonTerminals

Note, if you are using ISO M2 at school, there will still be differences.

Upvotes: 0

marc_s
marc_s

Reputation: 754993

Well, the EBNF is pretty clear:

RepeatStatement = `REPEAT` StatementSequence `UNTIL` Expression.

So you must have a REPEAT keyword, a statement sequence (which can be just EXIT), the UNTIL keyword and an expression. Only those four parts together make up a valid repeat statement ...

So I guess something like this would be the shortest you can be:

REPEAT EXIT UNTIL TRUE

Upvotes: 3

Related Questions