Vous êtes sur la page 1sur 21

PARSING BOOLEAN EXPRESSIONS

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

PARSING
In 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.

CONTEXT FREE GRAMMAR


A 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 nonterminal). S belongs to N. If not specified, then it is the nonterminal that appears on the left-hand side of the first production.

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.

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.

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.

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

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

And here's the grammar:


stmt IF LPAREN bexp RPAREN stmt stmt IF LPAREN bexp RPAREN stmt ELSE stmt stmt ID ASSIGN bexp SEMICOLON bexp TRUE bexp FALSE bexp bexp OR bexp bexp bexp AND bexp bexp NOT bexp bexp LPAREN bexp RPAREN

CONTEXT FREE GRAMMAR FOR BOOLEAN EXPRESSIONS


The following is a context-free grammar (CFG)

for Boolean expressions:


E E ^ E E E ^ E E ~ E E (E) E t E f

E is a nonterminal and the start symbol, ^,

_, :, (, ), t, and f are terminals. Show that this grammar is ambiguous.

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:

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 ^ t The second one, corresponding to the second tree: E => E ^ E => t ^ E => t ^ E ^ E => t ^ t ^ E => t ^ t ^ t

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.

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

Parse tree for ~t v f:

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

ASSOCIATIVITY
The 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 tvt

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 recursive:
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.

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 | T T T and F | F F not F | true | false | ( B )

Parse tree

Vous aimerez peut-être aussi