Syntax != Semantics. Grammars. Help to. determine ??. Determine. Ambiguities. Ambiguity in English. I saw an airplane with a telescope. I saw a girl with her boyfriend. I saw a boy scout with a telescope on the mountain top.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Grammars
Help to
determine ??
Determine
Ambiguities
IT 327
I saw an airplane with a telescope.
I saw a girl with her boyfriend.
I saw a boy scout with a telescope on the mountain top.
I saw a pretty girl with a telescope on the mountain top.
IT 327
if (a > 2)
if (b > 1)
b++;
else
a++;
Dangling else
if (a > 2)
if (b > 1)
b++;
else
a++;
IT 327
G1:<subexp> ::= <subexp>  <subexp>  a  b  c
G2:<subexp>::= <var>  <subexp>  <var><var> ::= a  b  c
G3:<subexp> ::= <subexp>  <var>  <var><var> ::= a  b  c
They are equivalent! (what does that mean?)
IT 327
G1:<subexp> ::= <subexp>  <subexp>  a  b  c
<subexp>
<subexp>
<subexp>

<subexp>
<subexp>

<subexp>
a
<subexp>

<subexp>
<subexp>

<subexp>
c
b
c
a
b
G1 is an ambiguous grammar
IT 327
Right associative rule:
abcd = a–(b(cd))
Left associative rule:
abcd = ((a–b)c)d
IT 327
G3: <subexp> ::= <subexp>  <var>  <var><var> ::= a  b  c
G2: <subexp> ::= <var>  <subexp>  <var><var> ::= a  b  c
<subexp>
<subexp>
<subexp>

<var>
<var>

<subexp>
<subexp>

<var>
c
a
<var>

<subexp>
b
<var>
b
<var>
c
a
Right associative rule:
abc = a(bc)
Left associative rule:
abc = (ab)c
IT 327
<exp> ::= <exp> + <exp> 
<exp> * <exp> 
( <exp> ) 
a  b  c
Example: ((a+b)*c)
IT 327
We prefer this
Using leftmost derivation
<exp>
<exp>
<exp> + <exp>
<exp> * <exp>
c
a
<exp> * <exp>
<exp> + <exp>
b
c
a
b
Using rightmost derivation doesn’t help
IT 327
<exp>
<exp>
Call this a term
<exp> * <exp>
<exp> + <exp>
c
And term does not generate exp directly
<exp> + <exp>
a
<exp> * <exp>
a
b
b
c
Solution: restricting <exp> from generating * directly.
A term is an exp: a*bAn exp may not be a term: a+b
<exp> ::= <term>
<term> ::= <exp>
<term> ::= (<exp> )
IT 327
<exp>
<exp> ::= <term> + <exp>  <term>
<term> ::= <fact> * <term>  <fact>
<fact> ::=( <exp> ) 
a  b  c
<term> + <exp>
<fact>
<term>
<fact> * <term>
Example: a+b*c
a
<fact>
b
c
IT 327
<exp> ::= <term> + <exp>  <term>
<term> ::= <fact> * <term>  <fact>
<fact> ::=( <exp> ) 
a  b  c
<exp>
<term> + <exp>
<fact>
<term> + <exp>
<term>
a
<fact>
Example: a+b+c
<fact>
Right association
b
a+b+c = a+(b+c)
c
IT 327
<exp>
<exp> ::= <term> + <exp>  <term>
<term> ::= <fact> * <term>  <fact>
<fact> ::=( <exp> ) 
a  b  c
<term> + <exp>
<term>
<fact>
<fact>
( <exp> )
Example: (a+b)+c
<term> + <exp>
c
a
b
IT 327
No such thing called left associative grammar
<exp>
A better way to say this:
A grammar for left associative arithmetic exp.
<exp> + <term>
<exp> ::= <exp> + <term>  <term>
<term> ::= <term> * <fact>  <fact>
<fact> ::=( <exp> ) 
a  b  c
<exp> + <term>
<fact>
<term>
<fact>
c
Example: a+b+c
<fact>
b
a
IT 327
Too much freedom to generate if statements from stmt
<stmt> ::= <ifstmt>  s1  s2
<ifstmt> ::= if <expr> then <stmt> else <stmt>  if <expr> then <stmt>
<expr> ::= e1  e2
if e1 thenif e2 then s1 else s2
if (a > 2)
if (b > 1)
b++;
else
a++;
if (a > 2)
if (b > 1)
b++;
else
a++;
IT 327
if (a > 2)
if (b > 1)
b++;
else
a++;
if (a > 2)
if (b > 1)
b++;
else
a++;
Most languages: elsegoes withnearest then
IT 327
Numbers of then and else are the same
<stmt> ::= <ifstmt>  s1  s2
<ifstmt> ::= if <expr> then <fullstmt> else <stmt>  if <expr> then <stmt>
<expr> ::= e1  e2
<fullstmt> ::= <fullif>  s1  s2
<fullif> ::= if <expr> then <fullstmt> else <fullstmt>
IT 327
IT 327
Algol: then part can’t be another if (a block is allowed)
Ada: if statement must be terminated with an end if
IT 327
IT 327
Most systems construct an AST instead
IT 327
parse tree
abstract syntax tree
IT 327
IT 327
IT 327
IT 327
The priority in In the competition of getting operands.
Most languages put * at a higher precedence level than +.
a+b*c = a+(b*c)
(sign, as unary operators)
+ 
^
* / %
+ 
:=
IT 327
a + b * c
a = b < c ? * p + b * c : 1 << d ()
a <= 0  100 <= a
IT 327
with (my view)
IT 327