boolean expressions

21
GROUP MEMBERS: B.ANITHA(10MX02) N.JAYAKUMARI(10MX16) P.KANAKA(10MX21) G.REVATHI(10MX39) S.M.TAMILARASU(10MX50) PARSING BOOLEAN EXPRESSIONS

Upload: sunderthomas

Post on 28-Nov-2014

445 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Boolean Expressions

GROUP MEMBERS:

B.ANITHA(10MX02)

N.JAYAKUMARI(10MX16)

P.KANAKA(10MX21)

G.REVATHI(10MX39)

S.M.TAMILARASU(10MX50)

PARSING BOOLEAN EXPRESSIONS

Page 2: Boolean Expressions

PARSINGIn the design of a compiler the second

stage after lexical analysis is parsing. It is also called as syntax analysis.

Parser will take the stream of tokens generated by the lexical analyzer , check if it is grammatically correct and generate a parse tree.

The fundamental theory behind parsing is grammar theory.

Page 3: Boolean Expressions

CONTEXT FREE GRAMMARA CFG is a 4-tuple (N, T, P, S) where:

N is a set of non-terminals. T is a set of terminals. P is a set of productions (or rules) which are given by

A->α

where A denotes a single non-terminal.

α denotes a set of terminals and non-terminals.

S is the start non-terminal (sometimes called the goal non-terminal). S belongs to N. If not specified, then it is the non-terminal that appears on the left-hand side of the first production.

Page 4: Boolean Expressions

AMBIGUITY AND UNAMBIGUITY :A word is said to be ambiguously derivable

if there are more than one derivations existing for the word, that is if there are more than one distinct parse tree generated for that word.

A grammar is said to be ambiguous if there exists at least one word which is ambiguously derivable.

A grammar is said to be unambiguous if all the words derived from it are unambiguous.

Page 5: Boolean Expressions

AMBIGUITY AND UNAMBIGUITY A language L is said to be unambiguous if there exists at

least one grammar which is unambiguous. A language L is said to be ambiguous if all the grammar of

the language are ambiguous. Programming language grammars must be unambiguous.

RIGHTMOST AND LEFTMOST DERIVATIONS: There are several kinds of derivations that are important. A

derivation is a leftmost derivation if it is always the leftmost non-terminal that is chosen to be replaced. It is a rightmost derivation if it is always the rightmost one.

Page 6: Boolean Expressions

PARSE TREES

A pictorial representation of the derivation is known as parse tree or syntax tree or generation tree. The steps to generate a parse tree are:

Start with the start non-terminal. Repeat:

choose a leaf non-terminal X choose a production X alpha the symbols in alpha become the children

of X in the tree until there are no more leaf non-terminals left. The derived string is formed by reading the leaf

nodes from left to right.

Page 7: Boolean Expressions

BOOLEAN EXPRESSIONS The language of boolean expressions can be defined in

English as follows: true" is a boolean expression. "false" is a boolean expression. If exp1 and exp2 are boolean expressions, then so are the

following: exp1 || exp2 exp1 && exp2 ! exp1 ( exp1 )

Here is the corresponding CFG: bexp TRUE bexp FALSE bexp bexp OR bexp bexp bexp AND bexp bexp NOT bexp bexp LPAREN bexp RPAREN

Page 8: Boolean Expressions

Here is a CFG for a language of very simple assignment statements (only statements that assign a boolean value to an identifier):

stmt --> ID ASSIGN bexp SEMICOLON

The word "if", followed by a boolean expression in parentheses, followed by a statement, or

The word "if", followed by a boolean expression in parentheses, followed by a statement, followed by the word "else", followed by a statement

Page 9: Boolean Expressions

And here's the grammar: stmt IF LPAREN bexp RPAREN stmtstmt IF LPAREN bexp RPAREN stmt ELSE

stmtstmt ID ASSIGN bexp SEMICOLONbexp TRUEbexp FALSEbexp bexp OR bexpbexp bexp AND bexpbexp NOT bexpbexp LPAREN bexp RPAREN

Page 10: Boolean Expressions

CONTEXT FREE GRAMMAR FOR BOOLEAN EXPRESSIONSThe following is a context-free grammar

(CFG) for Boolean expressions:E E ^ EE E ^ EE ~ EE (E)E tE f

E is a nonterminal and the start symbol, ^, _, :, (, ), t, and f are terminals.

Show that this grammar is ambiguous.

Page 11: Boolean Expressions

A CFG is ambiguous if at least one word in the described language has more than one parse tree. To show that a grammar is ambiguous pick a word in the language that has two parse trees and show these two trees. For the given language, t ^ t ^ t is a word that has two parse trees:

Page 12: Boolean Expressions

An equivalent way is to show that the word in question either has two leftmost derivations or two rightmost derivations. Here are two different leftmost derivations. The first one, corresponding to the first tree:

E => E ^ E => E ^ E ^ E => t ^ E ^ E => t ^ t ^ E => t ^ t ^ tThe second one, corresponding to the second tree: E => E ^ E => t ^ E => t ^ E ^ E => t ^ t ^ E => t ^ t ^ t

Page 13: Boolean Expressions

We construct an unambiguous version of the context-free grammar for Boolean expressions by making it reflect the following operator precedence conventions: : has the highest precedence ^ has the next highest precedence _ has the lowest precedence

For example, t v ~f ^ t should be interpreted as t v ((~f)^t). As long as the grammar is unambiguous, you can choose whether or not to accept expressions that would need conventions about operator associativity to disambiguate them, like t ^ t ^ t.

Page 14: Boolean Expressions

Here is a version that assumes that the binary operators are non- associative. E E1 v E1 | E1 E1 E2 ^ E2 | E2 E2 ~E2 | (E ) | t | f

Draw the derivation trees according to your unambiguous grammar for the following two expressions: (i) ~t v f (ii) ~ (f v t) v ~f ^ t

Page 15: Boolean Expressions

Parse tree for ~t v f:

Page 16: Boolean Expressions

Parse tree for ~(f v t) v ~ f ^ t:

Page 17: Boolean Expressions

ASSOCIATIVITYThe binary operators ^ and v can be

considered to be: left-associative; i.e. an expression like t v t v

t would be interpreted as (t v t) v t right-associative; i.e. an expression like t v t

v t would be inter-preted as t v (t v t) non-associative; i.e. ruling out expressions

like t v t v t

Page 18: Boolean Expressions

Explain what is the case for your grammar and why, and how to change

your grammar for the other possibilities. Answer: Left-associative: make the productions for the binary operators

left re- cursive:

E -> E v E1 E1 -> E1 ^ E2

Right-associative: make the productions for the binary operators right

recursive: E -> E1 v E E1 -> E2 ^ E1

Non-associative: do not make the productions for the binary operators

directly recursive, as in the original grammar.

Page 19: Boolean Expressions

Example of a parse tree for a boolean expression:

Write a grammar that generates all of boolean expressions consisting of operators not, and, or. not has higher precedence than and while and does higher than or. and and or are left-associative. Construct the corresponding parse tree for not (true or false).

Answer:B → B or T | TT → T and F | FF → not F | true | false | ( B )

Page 20: Boolean Expressions

Parse tree

Page 21: Boolean Expressions