CS 496 - Assignment 1

Due: Thursday, February 7th by 11:59 PM

Expression Grammars

Part I

Your task is to write a context-free grammar for the expression grammar described below.

The grammar should generate the following terminal symbols:

the digits 0 through 9

the identifiers a, b, c, and d

the binary arithmetic operators +, -, *, and /

the binary relational operators =, !=, <, <=, >, and >=

the binary logical operators, AND, and OR

the unary (one operand) logical operator NOT

There are four precedence levels for binary (two operand) operators:

0 (lowest): the binary logical operators AND and OR

1: the relational operators =, !=, <, <=, >, and >=

2: the additive operators + and -

3 (highest): the multiplicative operators * and /

All of the binary (two-operand) operators are left-associative.

The NOT operator has a higher precedence than any binary operator.  It should "bind" to a primary expression immediately to its right.  A primary expression is an identifier (a, b, c, or d), a digit (0 through 9), or a parenthesized expression.

Example expressions and abstract syntax trees:

Expression Abstract Syntax Tree
a + b > c AND d >= 9
NOT a OR b
NOT ( a OR b )
a + b * c > 4

Note: make sure your grammar is unambiguous!

Part II

Using your grammar, find derivations and construct full parse trees (not abstract syntax trees) for the following expressions:

a - b - c * 4 > d AND d != 6

NOT ( NOT a AND b > c / d )

Submitting

Don't forget to include both your context-free grammar (Part I) and also your derivations/parse trees for the example expressions (Part II).

Format your solution as a MS word document or PDF file and submit to the marmoset server as Project 1:

https://camel.ycp.edu:8443/

You should have received an email with your username and password.