







































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








































Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Chapter Three Modern Programming Languages, 2nd ed. 2
G1: < subexp > ::= a | b | c | < subexp > - < subexp >
G2: < subexp > ::= < var > - < subexp > | < var > < var > ::= a | b | c
G3: < subexp > ::= < subexp > - < var > | < var > < var > ::= a | b | c
These grammars all define the same language: the language of strings that contain one or more a s, b s or c s separated by minus signs. But...
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 4
Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 5
Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 7
Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 8
Chapter Three Modern Programming Languages, 2nd ed. 10
G4: < exp > ::= < exp > + < exp > | < exp > ***** < exp > | ( < exp > ) | a | b | c
This generates a language of arithmetic expressions using parentheses, the operators + and ***** , and the variables a , b and c
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. 11
Our grammar generates this tree for a+bc*. In this tree, the addition is performed before the multiplication, which is not the usual convention for operator precedence.
< exp >
< exp > ***** < exp >
a
< exp > + < exp >
b
c
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. 13
a = b < c? * p + b * c : 1 << d ()
a <= 0 or 100 <= a
a + b * c
Error!
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. 14
To fix the precedence problem, we modify the grammar so that it is forced to put ***** below + in the parse tree.
G5: < exp > ::= < exp > + < exp > | < mulexp > < mulexp > ::= < mulexp > ***** < mulexp > | ( < exp > ) | a | b | c
G4: < exp > ::= < exp > + < exp > | < exp > ***** < exp > | ( < exp > ) | a | b | c
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 16
Chapter Three Modern Programming Languages, 2nd ed. 17
Our grammar G5 generates both these trees for a+b+c. The first one is not the usual convention for operator associativity.
< exp >
< exp > + < exp >
c
< mulexp >
< exp > + < exp >
b
a^ < mulexp >
< mulexp >
< exp >
< exp > + < exp >
b
< mulexp >
< exp > + < exp >
a
< mulexp > (^) c
< mulexp >
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. 19
a<<b<<c — most operators are left-associative a=b=0 — right-associative (assignment)
3-2-1 — most operators are left-associative 1::2::nil — right-associative (list builder)
a/bc* — most operators are left-associative abc — right-associative (exponentiation)
Docsity.com
Chapter Three Modern Programming Languages, 2nd ed. 20
To fix the associativity problem, we modify the grammar to make trees of + s grow down to the left (and likewise for ***** s)
G5: < exp > ::= < exp > + < exp > | < mulexp > < mulexp > ::= < mulexp > ***** < mulexp > | ( < exp > ) | a | b | c
G6: < exp > ::= < exp > + < mulexp > | < mulexp > < mulexp > ::= < mulexp > ***** < rootexp > | < rootexp > < rootexp > ::= ( < exp > ) | a | b | c
Docsity.com