Académique Documents
Professionnel Documents
Culture Documents
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.
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.
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.
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
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
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
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:
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
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
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.
non- associative.
E E1 v E1 | E1 E1 E2 ^ E2 | E2 E2 ~E2 | (E ) | t | f
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 recursive:
E -> E1 v E E1 -> E2 ^ E1
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