Programming languages assignment 2
Put a README file in the assignment directory with instructions for compiling and running the program for the programming exercises. You may also include some test input data for which your program works. Your program should compile on the computers in the CSC lab.
You are required to submit source code and data files. Executable files or any other binary files will not be accepted. Please write your name on all documents!
Exercise 2.1
Write EBNF descriptions for the following: [5 points]
- A Java class definition header statement
{`<class_head> ® {<modifier>} class <id> [extends class_name]
[implements <interface_name> {, <interface_name>}]
- A Java method call statement
<for> -> for ‘(‘ [[<type>] <id> = <expr> {, [<type>] <id> = <expr>}] ; [<expr>] ; [<expr>{, <expr>}] ‘)’ ‘{‘ <stmt_list> ‘}’
- A C switch statement
<switch_stmt> ® switch ( <expr> ) {case <literal> : <stmt_list>
{case <literal> : <stmt_list> } [default : <stmt_list>] }
- A C union statement
<union_defn> -> union <var_list> <union_identifier>;
<var_list> -> <list_of_data-type specifier> <var>
<list_of_data-type specifier> -> int | float | long |char | double
<union_identifier> -> <var>
- C float literals
<float-literal> –> <real> <suffix>
| <real> <exponent> <suffix>
| <integer> <exponent> <suffix>
Exercise 2.2*
- Given the following EBNF declarations (based on Modula-2)
<simple expression> ::= [ + | - ] <term> { + <term> | - <term> | OR <term> }*
<term> ::= <factor> { * <factor> | / <factor> | DIV <factor> | MOD <factor> | AND <factor> | & <factor> }*
<factor> ::= <number> | <ident> | (<simple expression>) | NOT<factor>
<ident> ::= <letter> { <letter> | <digit> }*
`}
Simplify the expressions for <simple expression> and <term> by adding other nonterminal definitions so that you can reduce the number of options in the braces.
[2 points]
- b) Construct five examples of a <simple expression>, using only terminals in the language. Try to make each example significantly different from the others so as to illustrate the expressions which this definition Assume simple definitions for <letter> (i.e., a..z, A..Z), <digit> (i.e., 0..9), and <number> (i.e., <digit>+) although the actual definitions in Modula-2 are more complex.
[5 points]
Exercise 2.3*
- Here are more EBNF declarations:
{`<case statement> ::= CASE <expression> OF <case> { '|' <case> }* [ ELSE <statement sequence> ] END
<case> ::= [ <case label list> ':' <statement sequence> ]
<case label list> ::= <case label> { ',' <case label> }*
<case label> ::= <const expr> [ '..' <const expr> ]
According to the EBNF definitions given in this exercise, indicate whether each of the fol- lowing examples is or is not a legal case statement. For those that are not, explain why they are illegal according to the grammar. Assume that integers and single characters en- closed in single quotation marks are constant expressions (i.e., <const expr>), that a vari- able name qualifies as an <expression>, that a <statement sequence> is a sequence of semicolon-separated statements, and that a procedure call (indicated by a simple identifi- er, not followed by parentheses) and an assignment statement (where':=' is the assignment operator) are examples of statements. Also, 'DIV' is a legal division operator in this example language (Modula-2).
[8 points]
`}
- CASE nextChar OF
'a' : ScanIdentifier | 'b' : ScanIdentifier;
bold := 1 | '0' : ScanNumber
ELSE Error END
- CASE nextChar OF
'a' .. 'z' : ScanIdentifier ELSE
'0' .. '1' : ScanNumber END
- CASE nextChar OF
'a' .. 'z' : ScanIdentifier ELSE
ScanNumber ELSE
ScanOperator END
- CASE nextChar OF
'a' .. 'z', 'A' .. 'Z' : ScanIdentifier | '0' .. '9' : ScanNumber |
'+', '-', '*', '/', '^' : ScanOperator | END
- CASE nextChar OF
'a', 'b', 'c', .. 'z' : ScanIdentifier | '0' .. '9' : ScanNumber |
'+', '-', '*', '/', '^' : ScanOperator END
- CASE op OF
| '+' : total := total + operand
| '-' : total := total - operand
| '*' : total := total * operand
| '/' : total := total DIV operand END
- CASE op1, op2 OF
'+' : total := total + operand | '-' : total := total - operand | '*' : total := total * operand |
'/' : total := total DIV operand | END
- CASE ident OF END
*both exercises have been adopted from Jonathan Mohr