|
Homework #2 - Individual
1.(30 points) Construct a grammar for while statements of each of the following
languages:
Prolog is a logic language and does not have a while statement, however it is possible to emulate it if needed.
<WhileStmt> ::= while (<Expr>) <statements>
<WhileStmt> ::= while ( <Expr> ) <statements>
In order to keep language simple Eiffel provides single loop construct http://cs.anu.edu.au/student/eiffel/SyntaxGram/index.html#Features Loop Initialization [Invariant] [Variant] Loop_body End
LISP has a general iteration macro loop which is defined in the standard: http://www.xanalys.com/software_tools/reference/HyperSpec/Body/mac_loop.html The ``simple'' loop form: loop compound-form* => result* The ``extended'' loop form: loop [name-clause] {variable-clause}* {main-clause}* => result* Macro while is not standard but could be defined as: http://www.paulgraham.com/lib/onlisp.lisp
`(do () ((not ,test)) ,@body))
Cobol uses PERFORM statement for tasks similar to while : http://cis01.bloomu.edu/courses/92252/html/cobol/perform.html PERFORM [ procedure-name-1 [ { THROUGH or THRU } procedure-name-2 ] ] [ imperative-statement-1 END-PERFORM ]
2. (10 points) Given the following English language grammar for constructing
sentences:
Introduce a new start symbol <paragraph> that will allow you to generate any number of sentences. Show how the grammar has to be modified to accomplish this.
<paragraph>::<sentence>|<paragraph><sentence> <sentence>::<subject><verb><object>
3.(15 points) Give a BNF definition (do not use EBNF) of a language of balanced parentheses. The only two tokens in the language are ( and ). For example, ()() and ((()))()((()())) are in this language, while (((() is not.
Examples: Rule Result Rule Result 3 S(S) 4 S() 4 S()(S) 1 ()() 2 (S)()(S) 2 ((S))()(S) 1 ((()))()(S) 2 ((()))()((S)) 4 ((()))()((S())) 1 ((()))()((()()))
|
e-mail Mikhail Viron |