Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci320] [Search ]
[Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Grading] [Contact]
Sessions: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
04q.html (HTML) [04q.txt(Text)] Thu Apr 5 17:48:57 PDT 2007

Contents


    CS320/04 Questions on Defining the Syntax of Languages

      Question 1 syntax, semantics, BNF XBNF

      a. Define the following terms: syntax, semantics, grammar, 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

      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 3

      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 4

      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 5

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

      Question 6

      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))

      Project Hints for Writing Syntax

      [ hints.syntax.html ]

End