Grammar design Associativity l Plus and minus are

  • Slides: 18
Download presentation
Grammar design: Associativity l Plus and minus are left associative, parse trees should grow

Grammar design: Associativity l Plus and minus are left associative, parse trees should grow to the left L : : = L + number | L - number | number l If parse tree grows to the right it is farther from the abstract syntax R : : = number + R | number - R | number l However, right associatively is appropriate for right-associative operators such as assignment and exponentiation

BNF for Arithmetic Exp l Expression with precedence (only) l 2 * 3 –

BNF for Arithmetic Exp l Expression with precedence (only) l 2 * 3 – 5 * 6 – 7 S F - S | F + S | F F T * F | T / F | T T (S) | number -17 6 23 30 The answer should be – 31, but this gives -17 7

BNF for Arithmetic Exp l Associativity taken into account l Derivation of 2 *

BNF for Arithmetic Exp l Associativity taken into account l Derivation of 2 * 3 – 5 * 6 – 7 S S - F | S + F F F * T | F / T T (S) | number S S S F F T 2 2 2 2 – – – * * * * * F F F T T T 3 3 3 3 – F – F – S – S – F – T – 5 – 5 – F – F – F * T * T * 6 – T – T – 7 by by by by | F | T S S-F S F F F*T F T T number T number NOTE: Colors updated, but rule usage was correct as originally shown in class. See usage on the right column Gives “correct” answer, and as a leftmost derivation: “leftmost nonterminal is replaced at each step”

Grammar Design -- Precedence l Conventional arithmetic such as a * b + c

Grammar Design -- Precedence l Conventional arithmetic such as a * b + c * d + e l Grammar: A E T F : : = E : = A | E [right associative & recursive] E + T | E - T | T [left associative & recursive] T * F | T / F | F number | name | ( E ) l In a top-down parse, expand E first; this expands into T, which places higher precedence operators * and / closer to the number, name or parenthesized expression. Viewed bottom-up, the * and / are recognized first and hence have larger precedence l Pblm – topdown cannot parse left recursion

Derivation l l Start with the unique distinguished symbol Derivation Sequence of strings where

Derivation l l Start with the unique distinguished symbol Derivation Sequence of strings where a non-terminal on is replaced by a production rule in the next step of the derivation – Top-down selects a rule where the left side matches a nonterminal of the sentential form. It replaces the non-terminal with the rule’s right side. This is generation – Bottom-up selects a rule where the right side matches a nonterminal of the sentential form, or a string that includes a nonterminal. It replaces the matched string with the rule’s left side. This is reduction l l Sentential Form Any step in the derivation Sentence Sentential form with no non-terminals

Leftmost Derivation A derivation in which only the leftmost nonterminal in any sentential form

Leftmost Derivation A derivation in which only the leftmost nonterminal in any sentential form is replaced at each step. l Unique derivation for a string for a non ambiguous grammar l For an ambiguous grammar thee may be multiple productions that can replace the nonterminal, thus giving multiple derivations (and resulting parse trees) l

Rightmost Derivation The rightmost non-terminal is replaced in the derivation process in each step.

Rightmost Derivation The rightmost non-terminal is replaced in the derivation process in each step. l Also referred to as Canonical Derivation l

Parse Tree Abstracts out the information of the derivation process. l Usually the parse

Parse Tree Abstracts out the information of the derivation process. l Usually the parse tree is the same, even if the derivations for a string are different. l

Ambiguity Characteristic of grammar l If a grammar has two parse trees or two

Ambiguity Characteristic of grammar l If a grammar has two parse trees or two derivations for a particular string in the language it represents, then ambiguous l If all grammars for a particular language are ambiguous then the language is called Inherently Ambiguous l

Ambiguous Grammars Has more than one leftmost derivation. l Has more than one parse

Ambiguous Grammars Has more than one leftmost derivation. l Has more than one parse tree. l. S S + S | S - S | a | l b

Left Recursive A grammar is left recursive if there is a derivation as follows:

Left Recursive A grammar is left recursive if there is a derivation as follows: A (+) A<string> l Some parsing methods cannot handle these grammars (top-down parsing methods) l

Immediate Left Recursiveness A A B A <alpha> <beta> B <alpha> B A A<alpha

Immediate Left Recursiveness A A B A <alpha> <beta> B <alpha> B A A<alpha 1> | …. | A<alpha_m> | <beta 1> | …. . | <beta_n> | <beta> | null Replace the leading nonterminals A <beta 1>B | …| <beta_n>B B <alpha 1>B |…| <alpha_m>B | null

Left Recursion “A grammar is left recursive if it has a nonterminal A such

Left Recursion “A grammar is left recursive if it has a nonterminal A such that there is a derivation A A for some string ” l Topdown parsers cannot handle immediate left recursion since they do not see a leftmost nonterminal until the recursion is complete l Fortunately, can rewrite the left recursive form to begin with leading nonterminals l – Left recursion can be replaced with non-left-recursive productions. A A | becomes two productions A A’ and A’ A’| (page 176)

Eliminating Left Recursion (Alg. ) Order the non-terminals from A 1 to An l

Eliminating Left Recursion (Alg. ) Order the non-terminals from A 1 to An l Next, replaced productions l for ( i=1; i<=n; I++ ) { for (j=1; j<=i-1; j++) { replace A_i A_j <gamma> with productions A_i <delta_1> <gamma> | … | <delta k> <gamma> where A_j <delta_1> | <delta_2> | … <delta_k> are the A_j productions l Then, remove immediate left recursion among A_i productions

Left Factoring l Modifying the grammar in order to be able to expand a

Left Factoring l Modifying the grammar in order to be able to expand a non-terminal without nondeterminism.

Left Factoring a Grammar For all A, find the longest common prefix <alpha> common

Left Factoring a Grammar For all A, find the longest common prefix <alpha> common to more than one of its rhs l Replace A <a><b 1>| … |<a><bn>| <c> l A B l S <a>B | <c> <b 1> | …. . | <bn> i. Et. S | i. Et. Se. S | a , S i. Et. SS’ | a S’ e. S | null, E b

Classic if-then-else problem l Grammar S: : = if E then S 1 else

Classic if-then-else problem l Grammar S: : = if E then S 1 else S 2 l How to parse this? if E 1 then if E 2 then S 1 else S 2 <-----S-----> if E 1 then if E 2 then S 1 else S 2 <-----S 1 -----> <S 2>

Left Factoring The ambiguity of the if/then/else is shown as figures 4. 5 and

Left Factoring The ambiguity of the if/then/else is shown as figures 4. 5 and 4. 6 in the Compilers text by Aho, Sethi and Ullman l They analyze the difficulty and find it is due to left recursion l “A grammar is left recursive if it has a nonterminal A such that there is a derivation A A for some string ” l – Left recursion can be replaced with non-left-recursive productions. A A | becomes two productions A A’ and A’ A’| (page 176)