[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] /04q.html [04q.txt(Text)] [Search ]
Thu Apr 12 14:24:25 PDT 2012
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]

Contents


    CS320/04 Questions on Defining Languages

      Question 1 syntax, semantics, BNF XBNF

      a. Define the following terms: syntax, semantics, grammar, lexer, parser, and ambiguity.

      b. Discussion: Which of the above should you have (or not have) in your Language Reference Manual? Why?

      c. Who needs to read language descriptions? List names of types of people.

      d. What was BNF used for? What does BNF stand for? Who developed it?

      e. What was EBNF used for? What does the E in EBNF stand for?

      f. Give an example defining the same simple piece of syntax in BNF and EBNF that shows how they differ.

      Question 2 Practice of Syntax Description

      In C and C++ there are shift expressions which move data to the left and right. Examples
       		2  << 3
       		16 >> 2
       		cout << 3
       		cin  >> x
      Using the books BNF express this description:
    1. A shift expression has an expression, a shift operator, and another expression.
    2. A shift operator is either "<<" or ">>".

      More TBA...

      Question 3

      a. Give 4 different examples of syntactically correct examples of while statements in C/ C++/Java. (Not complete programs, just single complete while-statements)

      b. Give an XBNF definition* of a C/C++/Java while statement in terms of expression and statement.

      c. Translate your previous answer into BNF definitions in terms of <expression> and <statement>.

      d. Show parse trees for each of your examples using your BNF. If any don't fit, either revise the example of the XBNF and BNF.

      Question 4

      a. Give 4 examples of a syntactically valid for a C/C++/Java if statement. Include both if and if-else examples.

      b. Give some XBNF definitions that define a C/C++/Java if statements in terms of expression and statement. The definitions must define the syntax of both the if and if-else alternative forms.

      c. Translate your previous answer into BNF definitions using <expression> and <statement>.

      d. Draw the parse trees for each of your 5 examples using your BNF.

      e. If any don't fit, either revise the example of the XBNF and BNF.

      Question 5

      Express the following informal syntax rules in XBNF:
      1. An expression is the sum of a number of terms like this term1+term2+term3...
         	   1*2
         	   1*2+3
         	   1*2+X*Y+4*a*c
         	   2+1*(1+2*3)
      2. A term is the product of factors like this: factor1*factor2*factor3...
         	   4*a*c
      3. A factor is a constant, a variable, or an expression in parentheses.
         	   4
         	   a
         	   (1+2*3)
         	   (1*2+X*Y+4*a*c)
         	   (2+1*(1+2*3))

      Note: this is a very common pattern in syntax descriptions that defines the precedence of operators.

      See [ Answer 4 ] below.

      Question 6

      You are designing a new language. In what ways can you define its syntax? Which do you choose to use? Why?

      Question 7 found in finals

      Give an example of how your project team described the syntax of its language.

      (I have put this in previous finals.... however you won't be able to answer it until the end of your project)

      Answers

        You can find lots of samples of syntax description on my web site. For example [ lookup.php?search=if_statement ] finds half-a-dozen links to definitions of if statements.

        Answer 4


        1. An expression is the sum of a number of terms like this term1+term2+term3...
        2. expression::= term #( "+" term).
           	   1*2
           	   1*2+X*Y+4*a*c
           	   1+2*3
           	   2+1*(1+2*3)

        3. A term is the product of factors like this: factor1*factor2*factor3...
           	   4*a*c
        4. term::=factor #( "*" factor).

        5. A factor is a constant, a variable, or an expression in parentheses.
        6. factor::= constant | variable | "(" expression ")".
           	   4
           	   a
           	   (1+2*3)
           	   (1*2+X*Y+4*a*c)
           	   (2+1*(1+2*3))

      . . . . . . . . . ( end of section Answers) <<Contents | End>>

      Project Hints for Writing Syntax

      [ hints.syntax.html ]

    . . . . . . . . . ( end of section CS320/04 Questions on Defining Languages) <<Contents | End>>

End