Number Grammars Note that many of these grammars could be written as regular grammars, but here we'll practice writing them in standard Backus-Naur Form - BNF (note: do not use Extended BNF). NUM1 = the set of all integers, including leading and optional leading '#' signs. Give parse trees for O and -128 NUM2 = the set of even negative integers (-2,-4,-6, ...). Give parse trees for -16, and -378. NUM3 = the set of signed decimal numbers, with at least one digit (possibly zero) both before and after the decimal point. Give parse trees for +1.2, and -33.03. Identifier Grammars ID1 = the set of strings beginning with an upper-case letter, followed by any number of lower case letters or digits. Give parse trees for Sumi and R. ID2 = the set of strings of letters, digits, underscores and dashes, provided that (1) the first character in the string is an upper-case letter, and (2) the final character is neither an underscore nor a dash. Give parse trees for BB-8 and case_1. ID3 = the set of strings axbx -- any number of a's followed by an equal number of b's. Give parse trees for the empty string (i.e. O a's and Ob's) and aabb. Boolean Expression Grammar Write a grammar for Boolean expressions
might be simple literals like 3 or 97 or may be more complex syntactic units like (3+x)/4. They evaluate to a number. You needn't concern yourself with their structure; just that they can be used as below. Boolean expressions include the literals TRUE and FALSE; they can be combined with the unary operator ! (meaning NOT) and binary operators && and II (meaning AND and OR). Also, arithmetic expressions can be combined with the following binary operators to form new Boolean expressions: ,, !=. When your grammar is complete, you should be able to derive parse trees for expressions like the following: TRUE FALSE FALSE && !TRUE 27= TRUE FALSE Number Grammars Note that many of these grammars could be written as regular grammars, but here we'll practice writing them in standard Backus-Naur Form - BNF (note: do not use Extended BNF). NUM1 = the set of all integers, including leading and optional leading '#' signs. Give parse trees for O and -128 NUM2 = the set of even negative integers (-2,-4,-6, ...). Give parse trees for -16, and -378. NUM3 = the set of signed decimal numbers, with at least one digit (possibly zero) both before and after the decimal point. Give parse trees for +1.2, and -33.03. Identifier Grammars ID1 = the set of strings beginning with an upper-case letter, followed by any number of lower case letters or digits. Give parse trees for Sumi and R. ID2 = the set of strings of letters, digits, underscores and dashes, provided that (1) the first character in the string is an upper-case letter, and (2) the final character is neither an underscore nor a dash. Give parse trees for BB-8 and case_1. ID3 = the set of strings axbx -- any number of a's followed by an equal number of b's. Give parse trees for the empty string (i.e. O a's and Ob's) and aabb. Boolean Expression Grammar Write a grammar for Boolean expressions might be simple literals like 3 or 97 or may be more complex syntactic units like (3+x)/4. They evaluate to a number. You needn't concern yourself with their structure; just that they can be used as below. Boolean expressions include the literals TRUE and FALSE; they can be combined with the unary operator ! (meaning NOT) and binary operators && and II (meaning AND and OR). Also, arithmetic expressions can be combined with the following binary operators to form new Boolean expressions: ,, !=. When your grammar is complete, you should be able to derive parse trees for expressions like the following: TRUE FALSE FALSE && !TRUE 27= TRUE FALSE